RestTemplate+Hystrix
项目中调用第三方采购的项目,采用的是restTemplate,但这样无法做线程隔离。
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.*; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.Map; /** * @author duanxz * 2019年6月1日 下午3:41:34 */ @Component public class RestTemplateHystrixCombination { private Logger logger = LoggerFactory.getLogger(RestTemplateHystrixCombination.class); @Resource @Qualifier("getRestTemplate2") RestTemplate restTemplate; /** * * @param url * @param name * @return */ @HystrixCommand(groupKey="CrmGroup", commandKey = "getTest", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),//指定多久超时,单位毫秒。超时进fallback @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//判断熔断的最少请求数,默认是10;只有在一个统计窗口内处理的请求数量达到这个阈值,才会进行熔断与否的判断 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10"),//判断熔断的阈值,默认值50,表示在一个统计窗口内有50%的请求处理失败,会触发熔断 }, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "1"), @HystrixProperty(name = "maximumSize", value = "1"), @HystrixProperty(name = "maxQueueSize", value = "0"), @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"), @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")}) public Object getTest(String url, String name) { Object origResult = restTemplate.getForEntity(url, String.class); return origResult; } }
groupKey和commandKey都是自定义的,取一下能代表业务含义的就好。
timeoutInMilliseconds、coreSize和maximumSize是我要的
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/restTemplate") public class RestTemplateHystrixCombinationController { @Autowired RestTemplateHystrixCombination restService; @GetMapping("/test") public Object testabc(){ return restService.getTest("http://ip:port/service/sync", "duanxz"); } }
分类:
springcloud
标签:
hystrix
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2013-12-19 二、jdk命令之javah命令(C Header and Stub File Generator)
2013-12-19 JVM参数汇总
2013-12-19 -Xbootclasspath参数、java -jar参数运行应用时classpath的设置方法
2013-12-19 Java中的ReentrantLock和synchronized两种锁定机制的对比--删除
2013-12-19 分表分库之二:唯一ID的生成方法
2013-12-19 Java8 @FunctionalInterface
2013-12-19 Spring Session实现Session共享下的坑与建议