hystrix的熔断降级

结合Feign 使用

1.A服务通过B服务的唯—标识,从Nacos获取到可调用列表。

2.使用feigh中的Http发起远程请求。

3.超过默认配置的时限,抛出异常,结束该线程。

调用方

依赖
        <!-- 熔断降级的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
在调用方的启动类中
//开启断路器的界面(调用方)
@EnableCircuitBreaker
对Feign 中的clients文件的ProducerController进行改造
package com.james.clients;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * name: 被调方的唯一标识
 * path: 被调方的controller层的路径
 */
@FeignClient(name = "dashan-first",path = "/user",fallback = ConsumerClientFallback.class)
public interface ConsumerClient {

    /**
     * feignClient中的方法与被调方要保持完全一致
     * 返回值,注解,路径,参数,方法名 一致
     * @return
     */
    @RequestMapping("/first")
    public String first();
}
在clients创建 ConsumerClientFallback 自定义的fallback处理类
import org.springframework.stereotype.Component;

@Component
public class ConsumerClientFallback implements ConsumerClient {
    @Override
    public String first() {
        return "被调方不可以用了,请稍后再次尝试";
    }
}

yml

ribbon:
  #设置超时时间为5秒
  ConnectTimeout: 5000
  ReadTimeout: 5000

#feign开启熔断 默认是 false
feign:
  hystrix:
    enabled: true

#配置hystrix的默认超时时长.默认为1秒,当1秒钟被调方没有返回结果内容,则直接进行熔断处理
hystrix:
  command:
    default:  #default全局有效,service id指定应用有效
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
#            被调方最大超时的时间
            timeoutInMilliseconds: 5000

断路器

1、针对于被调方的服务已经出现了问题,例如调用了10词,8都出现了问题。那么我们认为被调方的接口是不稳定的,后续请求我们应该直接降级掉,不在尝试继续请求真正的接口。注这个时候我们就可以开启断路器,后续的请求不再真正的访问:口,而是直接降级。注意断路器开启后,是可以进行自动状态变换。断路器有三种状态:

1.close.

2.open。

3.halfopen(半开)

被调方

1.添加依赖
<!-- 熔断降级的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

<!-- 断路器的管理界面--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
2.配置文件更改
#允许dashboard访问的内容 注意 hystrix 2.0版本以上进行开放
hystrix:
  dashboard:
    proxy-stream-allow-list: "*"
#配置允许访问所有的接口路径内容
management:
  endpoints:
    web:
      exposure:
        include: "*"
3.对于某个接口进行断路器的配置
    @RequestMapping("/first")
    //1.当该接口出现异常,走的降级方法 注意:降级方法要与本方法的返回值以及参数保持一致
    //2. 当该接口的处理速度过慢,也需要走预定好的退路方法。使用的是tomcat的信号量来进行管理每个线程的超时时间
    @HystrixCommand(fallbackMethod = "firstFallback",
            commandProperties = {
                    //开启tomcat的线程
                    @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
                    //设置每个线程的最大的超时时间
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
                    //配置开启断路器
                    @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
                    //代表该接口如果出现异常的次数达到10次,则开启断路器
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "4"),
                    //代表总请求数达到50%的请求出现异常,则开启断路器
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
                    //代表断路器多少秒后更改我们的断路器的状态,由 open状态改为halfopen状态
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")
            })
    public String first() throws InterruptedException {
        //休眠时间
//        Thread.sleep(2990);
        return  "成功" + name +":"+port;
    }

    public String firstFallback(){
        return name +":"+port+"接口异常!";
    }
4.在config文件中配置hystrix.straem流
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

import javax.servlet.annotation.WebServlet;

@WebServlet("/hystrix.stream")
public class HystrixServlet extends HystrixMetricsStreamServlet {
}
5.在启动类中加入注解
@SpringBootApplication
@EnableDiscoveryClient

//被调方的降级处理
@EnableHystrix
//开启断路器的界面
@EnableHystrixDashboard
//扫描我们自己配置的servlet路径
@ServletComponentScan("com.qf.config")
public class SpringCloudConsumerApplication {

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

 

posted on 2023-03-19 23:35  鲤斌  阅读(35)  评论(0)    收藏  举报