Spring Cloud Hystrix
来源:互联网 发布:阿拉伯菜 知乎 编辑:程序博客网 时间:2024/06/05 23:00
Spring Cloud Hystrix
创建spring-cloud-03-hystrix-server服务中心 端口为8088
创建spring-cloud-03-hystrix-client服务单元
- HelloControl类
@RestControllerpublic class HelloControl { @RequestMapping(value = "/hello",method = {RequestMethod.GET}) public String hello() throws InterruptedException { Thread.sleep(3000); System.err.println("hello hystrix ...."); return "hello hystrix ..."; } @RequestMapping(value = "/action",method = {RequestMethod.GET}) public String action() throws InterruptedException { Thread.sleep(5000); System.err.println("action hystrix ...."); return "action hystrix ..."; }}
创建spring-cloud-03-hystrix-request-a 消费端
- pom.xml引入jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
- application.properties
spring.application.name=hystrix-requestserver.context-path=/server.port=2003eureka.client.service-url.defaultZone=http://eureka1:8008/eureka/#启动重试机制spring.cloud.loadbalancer.retry.enabled=true ##断路器超时时间hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000req.fact.connect-timeout=1000req.fact.connection-request-timeout=1000req.fact.read-timeout=30000#是否对所有请求都进行重试client-service.ribbon.OKToRetryOnAllOperations=true#重试切换实例得次数client-service.ribbon.MaxAutoRetriesNextServer=1#重试切次数client-service.ribbon.MaxAutoRetries=5
- Application类
@EnableCircuitBreaker //开启断路器@EnableDiscoveryClient@SpringBootApplicationpublic class HystrixRequestAApplication { @Bean @ConfigurationProperties(prefix = "req.fact") public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){ return new HttpComponentsClientHttpRequestFactory(); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(httpComponentsClientHttpRequestFactory()); } public static void main(String[] args) { SpringApplication.run(HystrixRequestAApplication.class, args); }}
- HystrixC类
@RestControllerpublic class HystrixC { @Autowired private HelloService helloService; @RequestMapping(value = "/hello",method = {RequestMethod.GET}) public String hello(){ return helloService.hello(); } @RequestMapping(value = "/hystrix-handler",method = {RequestMethod.GET}) public String handler(){ return helloService.handler(); } @RequestMapping(value = "/hystrix-action",method = {RequestMethod.GET}) public String action(){ return helloService.action(); }}
- HelloService类
@Servicepublic class HelloService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloFailback") public String hello() { return restTemplate.getForObject("http://client-service/hello",String.class); } public String helloFailback(){ System.out.println("----执行降级策略----"); return "----执行降级策略----"; } @HystrixCommand(fallbackMethod = "handlerFailback", ignoreExceptions = {BadRequestException.class}) public String handler() { throw new RuntimeException("运行时异常"); } public String handlerFailback(Throwable e){ System.err.println("异常信息:"+ e.getMessage()); return "获取异常信息并可以做具体的降级处理"; } @HystrixCommand( commandKey = "actionKey", commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", /*KEY*/ value = "8000" /*VALUE*/)}, fallbackMethod = "actionFailback") public String action() { return restTemplate.getForObject("http://client-service/action",String.class); } public String actionFailback(){ System.out.println("----执行降级策略----"); return "----执行降级策略----"; }}
- 全部启动 http://localhost:2003/hello访问时,将产生降级策略,因为设置的熔断器得时间为2ms,在client端睡眠了3ms,就会产生降级策略,当访问 http://localhost:2003/hystrix-action时,不会产生降级策略,因为Hystrix熔断器可以指定在特定方法中。
Hystrix Dashboard仪表盘
- 仪表盘就是为了监控得,方便服务接口进行测试排查,架构如下:
创建spring-cloud-03-hystrix-dashboard 项目
- 引入响应得jar包,spring-cloud-starter-hystrix-dashboard,并且要监控得服务必须依赖spring-boot-starter-actuator jar包
- Application类
@EnableHystrixDashboard //启动断路器监控@EnableDiscoveryClient@SpringBootApplicationpublic class HystrixDashboardApplication { // 监控台地址: http://localhost:8011/hystrix // 查看[我们要监控哪一个断路器服务]什么样的数据,写上具体的地址: http://localhost:2003/hystrix.stream public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); }}
- 启用仪表盘注解配置:@EnableHystrixDashboard通过访问http://localhost:8011/hystrix可看到流量监控台,在下面输入要监控得服务地址即可。
Hystrix Turbine 集群监控
- 对于集群得情况,dashbroad是不行得,需要引用一个收集组件Turbine进行汇总数据,最后流入到我们得监控台(dashboard)中去,引入spring-cloud-starter-turbine依赖,并添加注解@EnableTurbine,对application.properties中turbine进行配置
spring.application.name=hystrix-turbineserver.port=3000server.context-path=/management.port=3001#指定要收集集群控制信息得服务名turbine.app-config=hystrix-request turbine.cluster-name-expression="default"turbine.combine-host-port=trueeureka.client.service-url.defaultZone=http://eureka1:8008/eureka/
- Application类
@EnableTurbine //启用收集断路器集群服务功能@EnableDiscoveryClient@SpringBootApplicationpublic class HystrixTurbineApplication { // 要监控的turbine地址[监控hystrix-request服务]: http://localhost:3000/turbine.stream // turbine是集群监控 dashboard中输入得是turbine得地址 turbine是集群得集合 public static void main(String[] args) { SpringApplication.run(HystrixTurbineApplication.class, args); }}
- Turbine与dashboard整合架构如下:
- 复制spring-cloud-03-hystrix-request-a项目为b,启动spring-cloud-03-hystrix-request-b项目,启动Turbine,在dashboard中输入http://localhost:3000/turbine.stream,当a,b同时访问时,下面会出现
GitHub地址
下节声明式服务调用组件Fegin
阅读全文
0 0
- Spring Cloud Hystrix--熔断器
- spring cloud-hystrix
- spring cloud Hystrix
- spring-cloud-hystrix(一)
- Spring Cloud Hystrix Dashboard
- spring cloud hystrix
- spring cloud hystrix dashboard
- Spring Cloud Hystrix
- Spring-Cloud 熔断器 Hystrix
- spring-cloud【Hystrix】
- spring cloud: Hystrix断路器(熔断器)
- spring cloud: Hystrix断路器(熔断器)
- Spring Cloud 断路器Hystrix实战
- Spring Cloud 中的断路器 hystrix
- spring cloud: Hystrix断路器(熔断器)
- spring-cloud-consumer-hystrix(六)
- Spring Cloud Eureka+Ribbon+Hystrix
- spring cloud: Hystrix断路器(熔断器)
- js中时间比较
- ubuntu 16.04 配置java环境变量
- Java 五大框架之间的对比
- Gym-101177B 递归想法题
- IO多路复用机制详解
- Spring Cloud Hystrix
- JPA-style positional param was not an integral ordinal错误解决办法
- collect2: ld terminated with signal 9 错误解决办法
- tomcat中server.xml配置3个端口号的作用
- JAVA连接MDB----连接问题及中文乱码问题
- 前端html页面内容的加载顺序
- 【Java基础09_2】抽象类
- C语言操作符
- 001_JavaWeb简介