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

Connect Class to specific Hystrix Command Key / Parameter specific Hystrix instance

我的任务是使用 Spring 启动来检查几个 URLs/Endpoints 并 运行 如果此端点不可用,则使用回退方法。因此,我使用 端点名称 作为键启动了一个 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 实例提交任何传入请求或 class 的情况。 也许有人知道如何解决这个问题,或者找到了一个很好的教程来做到这一点。 亲切的问候

马库斯

我看到有几个程序员正在寻找一种解决方案,使 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);
    }
}