Spring Cloud Hystrix

Spring Cloud Hystrix_第1张图片

Spring Cloud Hystrix

Spring Cloud Hystrix_第2张图片
Spring Cloud Hystrix_第3张图片

Spring Cloud Hystrix_第4张图片

创建spring-cloud-03-hystrix-server服务中心 端口为8088

创建spring-cloud-03-hystrix-client服务单元

  • HelloControl类
@RestController
public 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

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-eureka


    org.springframework.cloud
    spring-cloud-starter-ribbon


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


    org.springframework.boot
    spring-boot-starter-actuator

  • application.properties
spring.application.name=hystrix-request
server.context-path=/
server.port=2003
eureka.client.service-url.defaultZone=http://eureka1:8008/eureka/
#启动重试机制
spring.cloud.loadbalancer.retry.enabled=true 
##断路器超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000


req.fact.connect-timeout=1000
req.fact.connection-request-timeout=1000
req.fact.read-timeout=30000
#是否对所有请求都进行重试
client-service.ribbon.OKToRetryOnAllOperations=true
#重试切换实例得次数
client-service.ribbon.MaxAutoRetriesNextServer=1
#重试切次数
client-service.ribbon.MaxAutoRetries=5
  • Application类
@EnableCircuitBreaker //开启断路器
@EnableDiscoveryClient
@SpringBootApplication
public 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类
@RestController
public 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类
@Service
public 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 Hystrix_第5张图片

创建spring-cloud-03-hystrix-dashboard 项目

  • 引入响应得jar包,spring-cloud-starter-hystrix-dashboard,并且要监控得服务必须依赖spring-boot-starter-actuator jar包
  • Application类
@EnableHystrixDashboard //启动断路器监控
@EnableDiscoveryClient
@SpringBootApplication
public 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可看到流量监控台,在下面输入要监控得服务地址即可。
Spring Cloud Hystrix_第6张图片

Hystrix Turbine 集群监控

  • 对于集群得情况,dashbroad是不行得,需要引用一个收集组件Turbine进行汇总数据,最后流入到我们得监控台(dashboard)中去,引入spring-cloud-starter-turbine依赖,并添加注解@EnableTurbine,对application.properties中turbine进行配置
spring.application.name=hystrix-turbine
server.port=3000
server.context-path=/
management.port=3001
#指定要收集集群控制信息得服务名
turbine.app-config=hystrix-request 
turbine.cluster-name-expression="default"
turbine.combine-host-port=true

eureka.client.service-url.defaultZone=http://eureka1:8008/eureka/
  • Application类
@EnableTurbine          //启用收集断路器集群服务功能
@EnableDiscoveryClient
@SpringBootApplication
public 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 Hystrix_第7张图片
  • 复制spring-cloud-03-hystrix-request-a项目为b,启动spring-cloud-03-hystrix-request-b项目,启动Turbine,在dashboard中输入http://localhost:3000/turbine.stream,当a,b同时访问时,下面会出现
    Spring Cloud Hystrix_第8张图片

你可能感兴趣的:(Spring Cloud Hystrix)