当前位置:
首页 >
Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五)
发布时间:2025/3/18
46
豆豆
生活随笔
收集整理的这篇文章主要介绍了
Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一、什么是断路器
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);} 复制代码下面启动项目测试
访问http://localhost:8811/test
熔断成功!!!转载于:https://juejin.im/post/5d01f057f265da1bd04edac8
总结
以上是生活随笔为你收集整理的Eclipse(STS) 初次搭建Spring Cloud项目之断路器Hystrix(五)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 具有只读属性的ComboBox
- 下一篇: JS滚动条到网页底部自动加载更多内容