温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用Springcloud怎么熔断hystrix服务

发布时间:2020-12-07 14:30:41 来源:亿速云 阅读:155 作者:Leah 栏目:开发技术

这篇文章给大家介绍利用Springcloud怎么熔断hystrix服务,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1.主启动类加上新的注解。

@EnableCircuitBreaker

2.service写入新的熔断控制方法

@Service
public class PaymentHystrixService {
 @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
      @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),       //是否开启断路器
      @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),  //请求数达到后才计算
      @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //休眠时间窗
      @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"), //错误率达到多少跳闸
  })
  public String paymentCirtuitBreaker(@PathVariable("id")Integer id){
   if(id<0){
     throw new RuntimeException("****id不能为负数");
   }
   String randomNum= IdUtil.simpleUUID();

   return Thread.currentThread().getName()+"\t"+"调用成功,编号"+randomNum;

  }
  public String paymentCircuitBreaker_fallback(@PathVariable("id")Integer id){
    return "id不能为负数,请稍后重试,o(╥﹏╥)o+"+id;
  }

此处hystrixCommand注解即是对熔断的一些限制,一般是在10秒内进行10次有60%的访问错误率就会进行熔断,自动启动备用的方法,默认5秒后有 正确的执行结果就会慢慢恢复正常状态,关闭断路器。

3.dashboard

为了能够更加直观的看见服务访问的一些情况,配置下可视化的网页观察熔断。

新建dashboard工程。

pom文件依赖

<dependencies>
    <dependency>
      <groupId>com.bai</groupId>
      <artifactId>cloud-api-common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <!--监控-->
    <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-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

主启动类

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard9001 {
  public static void main(String[] args) {
    SpringApplication.run(HystrixDashboard9001.class,args);
  }
}

yml配置下端口即可。

访问地址

http://localhost:9001/hystrix/

利用Springcloud怎么熔断hystrix服务

对于被监控的服务需要额外的配置。新版本会有报错需要在启动类加上如下配置。

/**
     * 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
     * ServletRegistrationBean因为SpringBoot的默认路径不是 “/hystrix.stream"
     * 只要在自己的项目里配置上下的servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet() {
      HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet() ;
      ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
      registrationBean.setLoadOnStartup(1);
      registrationBean.addUrlMappings("/hystrix.stream");
      registrationBean.setName("HystrixMetricsStreamServlet");
      return registrationBean;
    }

关于利用Springcloud怎么熔断hystrix服务就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI