Spring Cloud Hystrix

Spring Cloud Hystrix


创建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
<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-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 "----执行降级策略----";
    }
}

Hystrix Dashboard仪表盘

  • 仪表盘就是为了监控得,方便服务接口进行测试排查,架构如下:

创建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可看到流量监控台,在下面输入要监控得服务地址即可。

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-03-hystrix-request-a项目为b,启动spring-cloud-03-hystrix-request-b项目,启动Turbine,在dashboard中输入http://localhost:3000/turbine.stream,当a,b同时访问时,下面会出现
  • GitHub地址

  • 下节声明式服务调用组件Fegin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值