HyStrilx服务熔断降级方式

2019-02-27  本文已影响0人  昨日已逝去

HyStrilx服务熔断降级方式

原文在github,有些相对路径连接不能跳转,如想看原文项目地址 spingboot2.1.3加springcloud G版本,如果觉的不错给个star 谢谢!

HyStrilx简介

断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,
防止发生过载、发热、甚至起火等严重后果。

hystrilx不单独使用,此组件一般和ribbonfeign结合使用

前期准备工作

HyStrilx服务搭建(结合ribbon)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
spring.application.name=spring-cloud-hystrix
server.port=8005
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
@SpringBootApplication
@EnableCircuitBreaker
@EnableEurekaClient
public class SpringCloudHystrixApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

启动项目spring-cloud-hystrix

访问 http://localhost:8005/ribbon ,可以从服务a控制台看到调用成功。

关闭服务spring-cloud-clientA
再次访问 http://localhost:8005/ribbon ,返回结果是被熔断降级后的回调。

Feign整合

feign自带hystrix,所以不需要添加,任何依赖,只需要配置fallback属性即可。

@FeignClient(value = "eureka-client-a",fallback = ConsumerServiceHystrix.class)
public interface ConsumerService {

    @GetMapping(value = "hello/{name}")
    String hello(@PathVariable("name") String name);

}

创建回调类ConsumerServiceHystrix

@Component
public class ConsumerServiceHystrix implements ConsumerService{
    @Override
    public String hello(String name) {
        return "error";
    }
}
上一篇 下一篇

猜你喜欢

热点阅读