我的任务是使用Spring检查多个URL/端点,并在此端点不可用时运行回退方法。因此,我使用端点名作为键启动了一个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实例提交任何传入的请求或类。也许有人知道如何解决这个问题,或者找到了一个很好的教程来解决这个问题。亲切问候
马库斯
发布于 2020-07-22 06:19:12
我已经看到,有几个程序员正在寻找一个解决方案,使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);
}
}https://stackoverflow.com/questions/62937486
复制相似问题