首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将类连接到特定的Hystrix命令键/参数特定的Hystrix实例

将类连接到特定的Hystrix命令键/参数特定的Hystrix实例
EN

Stack Overflow用户
提问于 2020-07-16 14:55:31
回答 1查看 543关注 0票数 0

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

代码语言:javascript
复制
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实例提交任何传入的请求或类。也许有人知道如何解决这个问题,或者找到了一个很好的教程来解决这个问题。亲切问候

马库斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-22 06:19:12

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

我为使用注释风格而挣扎,但我使用了更老的时尚方法来实现这个目标。

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62937486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档