【问题标题】:Connect Class to specific Hystrix Command Key / Parameter specific Hystrix instance将类连接到特定的 Hystrix 命令键/参数特定的 Hystrix 实例
【发布时间】:2020-11-06 06:53:06
【问题描述】:

我的任务是使用 Spring Boot 检查多个 URL/端点,并在此端点不可用时运行回退方法。所以我使用 endpoint name 作为键来启动一个 hystrix 实例。

HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(key);
    HystrixCommandProperties commandProperties = HystrixPropertiesFactory.getCommandProperties(commandKey, null);
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey(key);
    HystrixCommandMetrics hystrixCommandMetrics = HystrixCommandMetrics.getInstance(commandKey,
            HystrixCommandGroupKey.Factory.asKey("transfer"), threadPoolKey, commandProperties);

    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.enabled", "true");

    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.timeout.enabled",
            "false");

    ConfigurationManager.getConfigInstance()
            .setProperty("hystrix.command.default.circuitBreaker.requestVolumeThreshold", "5");

    ConfigurationManager.getConfigInstance()
            .setProperty("hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds", "500");

现在我面临着我无法向这个 hystrix 实例提交任何传入的请求或类。 也许有人知道如何解决这个问题,或者找到了一个很好的教程来做到这一点。 亲切的问候

马库斯

【问题讨论】:

    标签: spring-boot hystrix netflix


    【解决方案1】:

    我已经看到几个程序员正在寻找一种解决方案来让 hystrix 与方法解耦。他们希望 Hystrix 与参数或对象相关。

    我在使用注释样式时遇到了困难,但我使用了更老式的方式来实现这个目标。

    import java.util.Date;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.ParameterizedTypeReference;
    import org.springframework.http.HttpMethod;
    import org.springframework.web.client.RestTemplate;
    
    import com.netflix.config.ConfigurationManager;
    import com.netflix.hystrix.HystrixCircuitBreaker;
    import com.netflix.hystrix.HystrixCommand;
    import com.netflix.hystrix.HystrixCommandGroupKey;
    import com.netflix.hystrix.HystrixCommandKey;
    
    public class StudentServiceDelegate extends HystrixCommand<StudentService> {
        private String key = "default";
        private String schoolname = "";
        private HystrixCircuitBreaker check;
    
        public StudentServiceDelegate(String schoolname) {
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("default"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(schoolname)));
            this.schoolname = schoolname;
            ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.enabled", "true");
            ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.timeout.enabled",
                    "false");
    
            ConfigurationManager.getConfigInstance()
                    .setProperty("hystrix.command.default.circuitBreaker.requestVolumeThreshold", "5");
    
            ConfigurationManager.getConfigInstance()
                    .setProperty("hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds", "500");
    
        }
    
        @Override
        protected StudentService getFallback() {
            /*
             * first 3 come from the HttpCookie next 3 are stubbed defaults
             */
    
            return callStudentServiceAndGetData_Fallback(schoolname);
        }
    
        public String getKey() {
            return key;
        }
    
        public void setKey(String newkey) {
            key = newkey;
        }
    
        // @Autowired
        RestTemplate restTemplate;
    
        public StudentService callStudentServiceAndGetData(String schoolname) {
    
            System.out.println("Getting School details for " + schoolname);
            restTemplate = new RestTemplate();
            String endpoint = "http://localhost:8098/getStudentDetailsForSchool/{schoolname}";
            if (schoolname.equalsIgnoreCase("allwrong"))
                endpoint = "http://localhost:9999/getStudentDetailsForSchool/{schoolname}";
            String response = restTemplate
                    .exchange(endpoint, HttpMethod.GET, null, new ParameterizedTypeReference<String>() {
                    }, schoolname).getBody();
    
            System.out.println("Response Received as " + response + " -  " + new Date());
    
            StudentService ss = new StudentService(schoolname);
    
            String out = "NORMAL FLOW !!! - School Name -  " + schoolname + " :::  Student Details " + response + " -  " + new Date();
            ss.setResult(out);
    
            return ss;
        }
    
        @SuppressWarnings("unused")
        private StudentService callStudentServiceAndGetData_Fallback(String schoolname) {
            System.out.println("Student Service is down!!! fallback route enabled...");
            StudentService ss = new StudentService(schoolname);
            ss.setResult(
                    "CIRCUIT BREAKER ENABLED!!!No Response From Student Service at this moment. Service will be back shortly - "
                            + new Date());
            return ss;
        }
    
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        @Override
        protected StudentService run() throws Exception {
            return callStudentServiceAndGetData(schoolname);
        }
    }
     
    

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 2018-09-12
      • 2018-07-15
      • 2017-03-30
      • 2019-05-12
      • 2018-12-13
      • 2018-04-15
      • 2019-01-21
      • 2015-03-16
      相关资源
      最近更新 更多