博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springCloud的使用04-----熔断器hystrix的使用
阅读量:7070 次
发布时间:2019-06-28

本文共 6588 字,大约阅读时间需要 21 分钟。

1. restTemplate+ribbon使用hystrix

  1.1 引入依赖

org.springframework.cloud
spring-cloud-starter-hystrix

  1.2 在需要熔断的方法上添加注解

@Servicepublic class HiService {    @Autowired    RestTemplate restTemplate;        //需要熔断的方法    @HystrixCommand(fallbackMethod="hiError")//熔断后执行的方法    public String sayHi() {        return restTemplate.getForObject("http://SERVICE-HI/info", String.class);    }        //熔断后执行的方法    public String hiError() {        return "sorry hi error";    }}

  1.3 在启动类中声明使用hystrix

@SpringBootApplication@EnableDiscoveryClient//向服务中心注册@RestController@EnableHystrix//启用熔断机制public class ConsumerRibbon {     @Autowired    private HiService hiService;        public static void main(String[] args) {        SpringApplication.run(ConsumerRibbon.class, args);    }        @Bean    @LoadBalanced//使用这个restTemplate开启负载均衡    RestTemplate initRestTemplate(){        return new RestTemplate();    }        @RequestMapping("info")    public String hiConsumer() {        String response=hiService.sayHi();        return response;    }}

  1.4 启动注册中心和cloud-consumer-ribbon,访问http://localhost:8764/info 返回sorry hi error

    启动service-hi,访问http://localhost:8764/info 返回hello eureka client 8762

2 feign使用hystrix

  2.1 feign自带熔断器,无需导入hystrix的依赖,但是需要导入以下依赖,否则回报java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect错误

com.netflix.hystrix
hystrix-javanica

  2.2 在配置文件中启用hystrix,默认是关闭的

feign:  hystrix:   enabled: true

  2.3 指定熔断后要执行的类

@FeignClient(value="service-hi",fallback=HiServiceHystric.class)//指定调用哪个服务提供者,指定熔断后的执行的类public interface IHiService {        @RequestMapping(value="/info",method=RequestMethod.GET)//指定调用服务提供者的哪个接口    String info();        @RequestMapping(value="/info",method=RequestMethod.GET)//指定调用服务提供者的哪个接口    String hi();}

  2.4 指定熔断后要执行对应的方法

@Componentpublic class HiServiceHystric implements IHiService {    //熔断后执行相应的方法    public String info() {        return "sorry info feign";    }    public String hi() {        return "sorry hi feign";    }}

  2.5 在启动类中声明启动hystrix

@EnableHystrix

  2.6 启动注册中心和cloud-consumer-feign,访问http://localhost:8765/info 返回sorry info feign

    启动service-hi,访问http://localhost:8765/info 返回hello eureka client 8762

3 使用熔断器监控(hystrix dashboard)

  3.1 引入相应的jar依赖

org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
org.springframework.boot
spring-boot-starter-actuator

  3.2 在启动类中声明启动hystrix dashboard

@SpringBootApplication@EnableEurekaClient//向服务中心注册@EnableHystrix//启用熔断机制@EnableHystrixDashboard //启用熔断器监控页面public class ConsumerRibbonApp {    public static void main(String[] args) {        SpringApplication.run(ConsumerRibbonApp.class, args);    }        @Bean    @LoadBalanced//使用这个restTemplate开启负载均衡    RestTemplate initRestTemplate(){        return new RestTemplate();    }}

  3.3 启动项目

    

    

4 熔断器聚合监控

  如果多个项目都配置了hystrix和hystrix dashboard,想要在一个项目中的hystrix dashboard上看到其他项目的hystrix dashboard情况,就需要使用turbine进行聚合监控

  4.1 创建springboot项目,引入jar依赖

4.0.0
com.beifeng.hadoop
beifeng-spring-cloud-turbine
0.0.1-SNAPSHOT
jar
beifeng-spring-cloud-turbine
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
1.8
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-turbine
org.springframework.cloud
spring-cloud-netflix-turbine
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
org.springframework.boot
spring-boot-maven-plugin

  4.2 在配置文件中配置那些项目要进行聚合监控

spring: application:   name: service-turbineserver:  port: 8770security: basic:   enabled: falseturbine: aggregator:   clusterConfig: default #指定聚合那些集群,多个使用 , 分隔,默认为default appConfig: cloud-consumer-ribbon,cloud-consumer-feign #配置要监控的服务,多个使用 , 分隔 clusterNameExpression: new String("default")eureka:  client:   serviceUrl:    defaultZone: http://peer1:8761/eureka/

  4.3. 在启动类中声明启动turbine

@SpringBootApplication@EnableTurbine //开启turbine,该注解包含了@EnableDiscoveryClientpublic class TurbineApp {    public static void main(String[] args) {        SpringApplication.run(TurbineApp.class, args);    }}

   4.4 启动项目,在任何一个配置了hystrix dashboard的项目中查看聚合监控

    

    

 

转载于:https://www.cnblogs.com/lifeone/p/9010125.html

你可能感兴趣的文章
C语言双向链表
查看>>
Memcached在Windows下的配置和使用(转)
查看>>
中国国际服装服饰博览会 _百度百科
查看>>
设置tableView背景颜色
查看>>
c# 中的UserControl是什么 用户控件和自定义控件有什么区别
查看>>
漂亮的ActionBar效果
查看>>
32 脚本编程风格
查看>>
让低版本的 Android 项目显示出 Material 风格的点击效果
查看>>
来一篇新鲜的招聘笔试题(2014秋招版)
查看>>
HashMap工作原理(转载)
查看>>
hive0.13.1配置hwi
查看>>
CSS3 Filter的十种特效
查看>>
实用的eclipse adt 快捷键
查看>>
bootstrap 树
查看>>
Senparc.Weixin.MP SDK 微信公众平台开发教程(九):自定义菜单接口说明
查看>>
电容知识汇总
查看>>
【转】模块编译Android源码方法
查看>>
iOS8 CIGlassDistortion滤镜的使用
查看>>
windows运行打开服务命令的方法 :
查看>>
【C++】atoi与stoi
查看>>