zoukankan      html  css  js  c++  java
  • 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");
        }
    }
  • 相关阅读:
    tomcat部署web应用的4种方法以及部署多个应用
    LeetCode算法题目解答汇总(转自四火的唠叨)
    16、手把手教你Extjs5(十六)Grid金额字段单位MVVM方式的选择
    15、手把手教你Extjs5(十五)各种Grid列的自定义渲染
    14、手把手教你Extjs5(十四)模块字段和Grid列的定义[2]
    13、手把手教你Extjs5(十三)模块字段和Grid列的定义[1]
    12、手把手教你Extjs5(十二)执行菜单命令在tabPanel中显示模块
    11、手把手教你Extjs5(十一)模块界面的总体设计
    10、手把手教你Extjs5(十)自定义模块的设计
    9、手把手教你Extjs5(九)使用MVVM特性控制菜单样式
  • 原文地址:https://www.cnblogs.com/duanxz/p/15708773.html
Copyright © 2011-2022 走看看