简介
1、Hystrix简介
该项目的依赖在 SpringCloud 父项目中已经不包含了,需要我们自己引入对应的版本,不能依赖于 SpringCloud 的版本了。
2、服务降级
服务降级是先会去正常地走业务,然后当业务出错之后再去走那个降级的补救措施,所以服务降级时候的响应速度一定是比服务熔断时候的要慢的,并且服务降级会调用出错的业务,会给业务提供压力。
服务降级是一种比较温柔的解决方案,虽然服务本身的不可用,但是能够保证正常响应数据。
2.1、pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.10.RELEASE</version> </dependency>
2.2、启动类
@SpringBootApplication // 启用Hystrix @EnableHystrix public class BorrowApplication { public static void main(String[] args) { SpringApplication.run(BorrowApplication.class, args); } }
2.3、Controller
@RestController public class BorrowController { @Resource BorrowService service; // 使用 @HystrixCommand 来指定备选方案 @HystrixCommand(fallbackMethod = "onError") @RequestMapping("/borrow/{uid}") UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){ return service.getUserBorrowDetailByUid(uid); } // 备选方案,这里直接返回空列表了。注意参数和返回值要和上面的一致 UserBorrowDetail onError(int uid){ System.out.println("执行补救措施"); return new UserBorrowDetail(null, Collections.emptyList()); } }
3、服务熔断
服务熔断是 Hystrix项目帮我们自动建立在服务降级的基础之上的,它会定期侦测失败次数或未响应时间等条件是否满足打开熔断器的程度。当打开熔断器之后,并不是拦截所有的请求然后直接执行补救措施,而是会定期放行一两个请求去执行正常的服务调用。服务降级期间,会定期地侦测服务是否能正常调用,若可以正常调用则关闭服务降级或服务熔断。
熔断机制是应对雪崩效应的一种微服务链路保护机制,当检测出链路的某个微服务不可用或者响应时间太长时,然后会进行服务的降级,再然后满足一定条件后进而熔断该节点微服务的调用,快速返回”错误”的响应信息。当检测到该节点微服务响应正常后恢复调用链路。
3.1、Hystrix 配合 OpenFeign 实现服务降级
我们可以对应接口中定义的远程调用单独进行降级操作。
3.1.1、application.yml
// 开启熔断支持 feign: circuitbreaker: enabled: true
3.1.2、Feign接口
// fallback参数指定为我们编写的实现类 @FeignClient(value="userservice",fallback=UserFallbackClient.class) public interface UserClient { @RequestMapping("/user/{uid}") User getUserById(@PathVariable("uid") int uid); }
3.1.3、实现 Feign接口进行服务降级
// 需要将其注册为 Bean,Feign 才能自动注入 @Component public class UserFallbackClient implements UserClient{ @Override public User getUserById(int uid) { User user = new User(); user.setName("我是补救措施"); return user; } }
4、监控页面
新起一个新的模块,这个模块用来展示监控数据。
监控页面地址:http://localhost:8900/hystrix/,在里面填写 http://localhost:8301/actuator/hystrix.stream,即要监控的服务的地址拼接上 /actuator/hystrix.stream。
4.1、pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> <version>2.2.10.RELEASE</version> </dependency>
4.2、application.yml
server: port: 8900 hystrix: dashboard: # 将 localhost 添加到白名单,默认是不允许的 proxy-stream-allow-list: "localhost"
4.3、启动类
@SpringBootApplication @EnableHystrixDashboard public class HystrixDashBoardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashBoardApplication.class, args); } }
4.4、在需要监控的服务中添加依赖
想要监控哪个服务,就在这个服务的模块的 pom.xml 中加入这个依赖。
Actuator 是 SpringBoot程序的监控系统,可以实现健康检查,记录信息等。在使用之前需要引入 spring-boot-starter-actuator,并做简单的配置即可。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
4.5、在需要监控的服务中添加配置
想要监控哪个服务,就在这个服务的模块的 application.yml 中加入这些配置。
management: endpoints: web: exposure: include: '*'