My task is to use Spring Boot to check several URLs/Endpoints and to run a fallback method if this endpoint is not available. So I start an hystrix instance by using the endpoint name as key.
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");
Now I'm facing that I can not commit any incoming request or class to this hystrix instance. Maybe somebody know how to solve that or has found a good tutorial to do this. kind regards
Markus
I have seen that several programmers are looking for a solution to have hystrix decoupled from the method. They want to have Hystrix related to a parameter or an object.
I struggled by using the annotation style but I utilized the more old fashion way to get this goal.
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);
}
}