1.hytrix
降级、熔断
1.1降级
加依赖、加注解、加降级代码、加降级方法
1.2添加依赖:hystrix

1.3加注解@EnableCircuitBreaker(在启动类上)
输入ECB有提示

1.4加降级代码(在sp06-ribbon的RibbonController类上)

加注解时有提示

1.5加降级方法
把上面的方法复制一份,改方法名和返回值

1.2熔断
过热保护
当访问量增大,后台一台服务器的故障会向其他服务器传播,早晨雪崩效应,为了避免出现雪崩效应,可以把故障服务断开。
hystrix断路器,自动打开,自动关闭
10秒内20次请求(必须首先满足,例如10秒内18此请求就不满足条件),50%调用失败执行降级代码,自动触发熔断
熔断打开几秒后,会进入“半开”状态,会尝试向后台服务器发送一次用户调用
如果成功,断路器会自动关闭,恢复正常
如果失败,继续保持打开状态几秒钟,之后再次进入半开状态
2.hystrix dashboard
对hystrix错误处理的情况进行监控。
通过actuator来暴露hystrix的错误日志。
actuator是spring提供的一个项目日志监控工具,可以暴露项目中多种日志数据。
2.1添加actuator依赖

2.2设置暴露的监控数据(编辑yml文件)


include: "*"暴露所有监控数据
include: hystrix.stream暴露一个监控数据
2.3重启sp06访问
http://localhost:3001/actuator

点击它,如果只有ping ping ......而没有数据,那是因为,没有访问后端。
启动商品、用户、订单对应的服务。然后访问他们
http://localhost:3001/item-service/35
http://localhost:3001/user-service/7/score?score=100
http://localhost:3001/order-service/

这样就对了
2.4搭建hystrix dashboard仪表盘
2.4.1新建项目sp08-hystrix-board
2.4.2添加依赖hystrix dashboard

2.4.3yml配置 允许抓取的服务器列表


2.4.4启动类加注解@EnableHystrixDashboard

有提示
2.4.5启动,访问
http://localhost:4001/hystrix
2.4.6在课前资料里找到这个

输入cmd进入窗口,然后输入如下代码运行,观察仪表盘状态
ab -n 20000 -c 100 http://localhost:3001/user-service/35

closed表示正常
3.Feign集成工具
集成了
1.远程调用-声明式客户端接口
2.ribbon(负载均衡、重试)
3.hystrix(降级、熔断)
3.1测试远程调用和负载均衡
删除sp06,新建sp09-feign
加依赖、加注解、配置yml、创建接口和类
3.2加依赖:
web/openfeign/eureka client/hystrix/actuator
3.3配置yml
spring:
application:
name: feign
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
3.4在启动类上加注解@EnableFeignClients
输入EF会有提示

3.5创建接口和类:

代码如下
/*
调用商品服务的声明式客户端接口
*/
@FeignClient(name="item-service")
public interface ItemClient {
@GetMapping("/{orderId}")
JsonResult<List<Item>> getItems(@PathVariable String orderId);
@PostMapping("/decreaseNumber")
JsonResult<?> decreaseNumber(@RequestBody List<Item> items);
}
注意不要写错名字

@RestController
@Slf4j
public class FeignController {
@Autowired
private ItemClient itemClient;
// localhost:3001/item-service/333
@GetMapping("/item-service/{orderId}")
public JsonResult<List<Item>> getItems(@PathVariable String orderId){
return itemClient.getItems(orderId);
}
@RequestMapping("/item-service/decreaseNumber")
public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
return itemClient.decreaseNumber(items);
}
}
3.6启动测试
http://localhost:3001/item-service/33

**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。