SpringCloud学习(三):Hystrix 断路器

菜鸟学渣接触spring cloud 系列...

公司也上微服务了,再不学习下就凉了,所以来踩坑吧...

版本:

  spring-boot:  2.0

  spring-cloud: Finchley.SR1

已有项目:

  [eureka-server]              # 注册中心

  [eureka-client-one]       #  微服务1

  [eureka-client-two]       #  微服务2

能上图绝不BB

  

当请求抛异常,超时,线程/信号量reject、短路时,不能直接返回错误或则一直卡着...即需要提供降级方案...

 

一、method方法上 断路

  在 [eureka-client-two]上测试...

  eureka-client-two  pom.xml  添加依赖 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

  eureka-client-two application.yml  添加配置 

# 还有另外一种不配置,添加 ‘HystrixMetricsStreamServlet’ Bean 的方式
# spring boot 2.0 的endpoint -=-https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide
management:
  endpoint:
    hystrix:
      stream:
        enabled: true  # 启用hystrix
  endpoints:
    web:
      exposure:
        include: hystrix.stream, *  # 添加hystrix.stream路径
#      base-path: /                 # 不设置则默认为 /actuator/hystrix.stream

  eureka-client-two EurekaClientTwoApplication.java 启用hystrix

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient  // 开启eureka客户端
@EnableFeignClients(basePackages = {"com.renzku.eurekaClientTwo.feign"})  // 开启feign
@EnableCircuitBreaker  // 开启断路器-和 @HystrixCommand 组合使用
public class EurekaClientTwoApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientTwoApplication.class, args);
    }
}

  eureka-client-two  FeignTest.java 添加断路配置

@RestController
public class FeignTest {

    @Autowired
    EurekaClientOneFeign eurekaClientOneFeign;  // 如果提示“No Beans Named***" 其实没问题,恶...

    @RequestMapping("/test/home")
    public String testHome(){
        // 调用feign,feign调用 eureka-client-one的api
        return eurekaClientOneFeign.home();
    }

    @HystrixCommand(fallbackMethod = "someBoom")
    @RequestMapping("/")
    public String hello(){
        int a = 0, c = 1;
        int d = c/a;  // 抛出异常触发
        return "hello world -- client two";
    }    
    
    /**
     * 断路回调方法
     */
    public String someBoom(){
        return "wokao, 魂淡---client two";
    }
}

 

  依次启动项目 [eureka-server]、[eureka-client-two]

  访问 http://localhost:8502/  返回了设置的默认值

  

二、feign端上 断路

  在上面基础上继续...

  eureka-client-two   application.yml  添加配置 

feign:
  hystrix:
    enabled: true  # 启用feign的hystrix

  eureka-client-two   fallback 和 fallbackFactory

/**
 *  eureka-client-one注册时的 spring.application.name
 */
/* @FeignClient(value = "eureka-client-one", fallback = HystrixFallback.class) */
@FeignClient(value = "eureka-client-one", fallbackFactory = HystrixFallbackFactory.class)
public interface EurekaClientOneFeign {

    @RequestMapping("/")
    String home();
}

/**
 * feign 的短路回调: 用 fallback
 */
@Component
class HystrixFallback implements EurekaClientOneFeign{
    @Override
    public String home() {
        return "feign fallback,魂淡";
    }
}

/**
 * feign 的短路回调--带上获取失败抛出的异常: 用 fallbackFactory
 */
@Component
class HystrixFallbackFactory implements FallbackFactory<EurekaClientOneFeign>{
    @Override
    public EurekaClientOneFeign create(Throwable throwable) {
        return ()-> ("feign fallback,魂淡: " + throwable.toString());
    }
}

  重启 [eureka-client-two]

  因为现在[eureka-client-one]未启动,所以feign肯定会调用失败...

  访问 http://localhost:8502/test/home,fallback和fallbackFactory下返回的结果

  

 

三、zuul网关上断路

  zuul网关基于hystrix也提供了微服务断路,后面zuul再说。

 

四、监控

  现在访问 http://localhost:8502/actuator/hystrix.stream ,将会返回状态信息

  这些信息可以在hystrixDashboard断路器面板中使用,后面说

  

posted on 2018-09-09 22:16  江湖吹风  阅读(332)  评论(2编辑  收藏  举报

renzku