hystrix熔断和降级

注册服务
pom 配置文件

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

application.yml 配置文件

spring:
  application: 
    name: eureka-server

server:
  port: 8001
    
eureka:
  instance:
    hostname: eureka1
    lease-renewal-interval-in-seconds: 10   ##客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 ##服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
    service-url:
      defaultZone: http://eureka1:8001/eureka/  
    

启动类

@EnableEurekaServer     //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {
    
    
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
    
}

pom 配置文件

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

application.yml 配置文件

spring:
  application: 
    name: client-service

server:
  context-path: /
  port: 7001
  
eureka:
  client:
    service-url:
      ##单点配置:
      defaultZone: http://eureka1:8001/eureka/

启动类

@EnableDiscoveryClient  //表示我是一个服务,注册到服务中心上
@SpringBootApplication
public class Application {

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

服务类


@RestController
public class HelloController {


    @RequestMapping(value = "/hello", method = {RequestMethod.GET})
    public String hello() throws InterruptedException{
        Thread.sleep(3000);
        System.err.println("hello hystrix ... ");
        return "hello hystrix!";
    }
    
    @RequestMapping(value = "/hi", method = {RequestMethod.GET})
    public String hi() throws InterruptedException{
        Thread.sleep(5000);
        System.err.println("hi hystrix ... ");
        return "hi hystrix!";
    }
    
    @RequestMapping(value = "/request", method = {RequestMethod.GET})
    public String request() throws InterruptedException{
        System.err.println("request ... ");
        return "request!";
    }

    
}

客户端调用方
pom.xml

<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>

启动类

@EnableCircuitBreaker //开启hystrix熔断器
@EnableEurekaClient
@SpringBootApplication
public class HystrixApplicationClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixApplicationClientApplication.class, args);
    }
    
}
@Bean
    @ConfigurationProperties(prefix = "custom.rest")
    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        return new HttpComponentsClientHttpRequestFactory();
    }
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate(httpComponentsClientHttpRequestFactory());
    }
    

application.yml

spring:
  application: 
    name: hystrix-request
    
server: 
  context-path: /
  port: 6002
  
eureka:
  client:
    service-url:
      ##单点配置:
      defaultZone: http://eureka1:8001/eureka/

##断路器超时时间: 
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 2000
## 后者采用 HttpURLConnection 的默认超时配置
custom: 
  rest:
    ## 指客户端和服务器建立连接的timeout
    connect-timeout: 2000
    ##指从连接池获取连接的timeout超出预设时间
    connection-request-timeout: 2000
    ## 指的是建立连接后从服务器读取到可用资源所用的时间
    read-timeout: 30000 

服务类

@RestController
public class HystrixController {

    
    @Autowired
    private HelloService helloService;
    
    @RequestMapping(value = "/hystrix-hello", method = {RequestMethod.GET})
    public String hello() {
        return helloService.callHello();
    }
    
    
    @RequestMapping(value = "/hystrix-handler", method = {RequestMethod.GET})
    public String handler() {
        return helloService.handler();
    }
    
    
    @RequestMapping(value = "/hystrix-hi", method = {RequestMethod.GET})
    public String hi() {
        return helloService.callhi();
    }
        
    
    @RequestMapping(value = "/hystrix-request", method = {RequestMethod.GET})
    public String request() {
        return helloService.callrequest();
    }
        
    
    

hystrix熔断和降级实现类

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;
    
    @HystrixCommand(fallbackMethod = "callHelloFailback")
    public String callHello() {
        return restTemplate.getForObject("http://client-service/hello", String.class);
    }
    
    public String callHelloFailback(){
        System.err.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 = "hiKey",
            commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", /*KEY*/ value = "8000" /*VALUE*/)},
            fallbackMethod = "callHiFailback")
    public String callhi() {
        return restTemplate.getForObject("http://client-service/hi", String.class);
    }
    
    public String callHiFailback(){
        System.err.println("----------执行降级策略------------");
        return "----------执行降级策略------------";
    }
    
    
    
    @HystrixCommand(fallbackMethod = "callrequestFailback")
    public String callrequest() {
        return restTemplate.getForObject("http://client-service/request", String.class);
    }
    
    public String callrequestFailback(){
        System.err.println("----------执行降级策略------------");
        return "----------执行降级策略------------";
    }
    
    
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容