Circuit Breaker解决方案Spring Cloud Hystrix组件。
以 2.2.0.release 为例,教你如何入门该组件。
核心概念
引入的jar包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
搭建各Project
本例采用Maven进行project的build等工作。下面关于spring cloud的discover和config模块配置什么的被剔除了,为了减少篇幅内容,但实际的spring cloud方案中是一定有的。
cloud-service-hystrix-dashboard
这个项目就是用来展示监控统计Circuit Breaker的数据,也就是hystrix.stream报上来的各应用线程接口熔断统计数据。在微服务体系中,dashboard要独立搭建项目,因为后续可能需要增加定制功能。dashboard需要支持单节点的统计数据同时,还需要能支持集群统计数据,因为微服务里每个应用至少是部署2台的,这些熔断数据不可能一个一个节点单独查看,需要汇总在一起,这里就用到了turbine组件。
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies>
bootstrap.yml
spring:
application:
name: service-hystrix-dashbaord
profiles:
active: ${ENV:local}
server:
port: 8888
turbine:
app-config: service-demo3
cluster-name-expression: "'default'"
applicatin.java
@SpringBootApplication @EnableHystrixDashboard @EnableTurbine public class HystrixApp { public static void main( String[] args ) { SpringApplication.run(HystrixApp.class,args); } }
cloud-service-demo3
demo3是一个应用系统实际集成使用的样例。应用系统中只需要引入hystrix组件即可,但是对于springboot项目还需要引入actuator组件。另外openfeign组件的引入是为提供另一种使用hystrix方式的样例,应用系统中 有两种方式 使用hystrix组件。
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml
spring: application: name: service-demo3 profiles: active: ${ENV:local} management: endpoints: web: exposure: include: hystrix.stream
application.java
@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker @EnableFeignClients public class Demo3App { public static void main( String[] args ) { SpringApplication.run(Demo3App.class,args); } }
hystrix.stream
@Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
这个是hystrix2.0一个小BUG,需要添加@Bean把监控统计数据的接口暴露成 /actuator/hystrix.stream
接口路径方式。
使用hystrix
原生hystrix方式
@RestController public class UserController { @Autowired(required = false) ITestService iTestService; @Autowired(required = false) IUserService iUserService; @GetMapping("/user/users") public String getUsers(){ return "users is success" + iUserService.getUserId("13567890123"); } @HystrixCommand(fallbackMethod = "defaultFindUser") @GetMapping("/user/find") public String getFindUser(){ return iTestService.sayHello("test"); } public String defaultFindUser(){ return "hystrix default data"; } }
openFeign方式
OpenFeign作为一个前端路由的HTTP Client工具,自身已经集成了hystrix功能。
@FeignClient(value = "service-demo2",fallback = UserServiceFallbackImpl.class)
public interface IUserService {
@RequestMapping(value="/user/{mobile}", method= RequestMethod.GET)
@ResponseBody
Long getUserId(@PathVariable(name = "mobile") String mobile);
}
@Service
public class UserServiceFallbackImpl implements IUserService {
@Override
public Long getUserId(String mobile) {
System.out.println(mobile);
return 0L;
}
}
openfeign对hystrix的包依赖关系:
dashboard面板
指标的介绍
原文
https://blog.shareworld.vip/archives/hystrix1
本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 如何使用SpringCloud的Hystrix组件