随笔 - 2228  文章 - 4  评论 - 371  阅读 - 1109万

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");
    }
}
复制代码

 

posted on   duanxz  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· 从 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共享下的坑与建议
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示