欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五)

发布时间:2025/3/18 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
  • 在微服务架构中,根据业务来拆分成一个个的服务,在SpringCloud中用RestTemplate+Ribbon和Feign来实现互相调用。为了保证其高可用,单个服务通常会集群部署。
  • 由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。
  • 服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
  • 为了解决这个问题,业界提出了断路器模型。
  • 一、什么是断路器

    Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls。 -------来自官网

    当底层服务出现故障时会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

    二、这里我们直接说Feign集成Hystrix

    Feign本身就集成了Hystrix,只是默认为关闭状态,我们需要在配置文件中将其打开

    feign:hystrix:enabled: true 复制代码

    下面我们来看看代码如何写,其实很简单,使用上一篇文章中的cloud-demo-feign项目,application.yml内容如下:

    server:port: 8811 # 你的端口 spring:application:name: feign eureka:client:service-url:defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/ feign:hystrix:enabled: true service-eureka-client:ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #com.netflix.loadbalancer.RandomRule #配置规则 随机 #com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询 #com.netflix.loadbalancer.RetryRule #配置规则 重试 #com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重 #com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略 复制代码

    项目结构如下:

    新建feign断路器接口SchedualClientNameWithFallbackFactory和断路器的实现类HystrixClientFallbackFactory

    package com.hewl.hystrix;import com.hewl.feign.SchedualCilentName;public interface SchedualClientNameWithFallbackFactory extends SchedualCilentName {}复制代码package com.hewl.hystrix;import java.util.HashMap; import java.util.Map;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;import com.hewl.feign.SchedualCilentName;import feign.hystrix.FallbackFactory;@Component public class HystrixClientFallbackFactory implements FallbackFactory<SchedualCilentName> {private static final Logger log = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);@Overridepublic SchedualCilentName create(Throwable cause) {// TODO Auto-generated method stubreturn new SchedualClientNameWithFallbackFactory() {@Overridepublic String testFromClientOne(String name) {log.error("fallback ,the result is : " + cause);// TODO Auto-generated method stubMap<String, Object> map = new HashMap<String, Object>();map.put("ID", -1L);map.put("NAME","");return map.toString();}};}}复制代码

    SchedualCilentName类做如下修改,feignclient注解添加fallbackfactory键值对

    package com.hewl.feign;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;import com.hewl.hystrix.HystrixClientFallbackFactory;@FeignClient(value="service-eureka-client",fallbackFactory = HystrixClientFallbackFactory.class) public interface SchedualCilentName {@GetMapping(value = "/test")public String testFromClientOne(@RequestParam("name") String name);} 复制代码

    下面启动项目测试

  • 启动eureka-server服务
  • 启动eureka-client服务
  • 启动feign服务
  • 访问http://localhost:8811/test

    熔断成功!!!

    转载于:https://juejin.im/post/5d01f057f265da1bd04edac8

    总结

    以上是生活随笔为你收集整理的Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。