java - Hystrix:如何封装多路呼叫
问题描述
我正在使用 Hystrix 来改进我的服务。如何将服务调用解封装到 Hystrix 中。我知道你可以为每个调用创建一个特殊的 hystrix 类,但是如果不使用 Spring,这将是太多的工作!
我尝试用伪代码描述我的问题:
public class HystrixController extends HystrixCommand {
public static void main(String[] args) throws Exception {
HystrixController hystrixController = new HystrixController();
System.out.print(hystrixController.execute());
}
private final ExampleService exampleService;
protected HystrixController() throws Exception {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.exampleService = new ExampleService();
}
// Call 1
public List getItemsAsList() {
return exampleService.getItemsByContractId(contractID);
}
// Call 2
public List getItemsByName() {
return exampleService.getItemsByName(contractID);
}
// How can I isolate the two calls ? The run() only allows me to use one.
@Override
protected List run() throws Exception {
return getItemsAsList();
}
}
在示例中,您可以看到只能执行一次调用。我想要这样的东西:
public class HystrixController extends HystrixCommand {
public static void main(String[] args) throws Exception {
HystrixController hystrixController = new HystrixController();
System.out.print(hystrixController.execute(1));
System.out.print(hystrixController.execute(2));
}
private final ExampleService exampleService;
protected HystrixController() throws Exception {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.exampleService = new ExampleService();
}
// Call 1
public List getItemsAsList() {
return exampleService.getItemsByContractId(contractID);
}
// Call 2
public List getItemsByName() {
return exampleService.getItemsByName(contractID);
}
// Multi Threads
@Override
protected List run_getItemsAsList() throws Exception {
return getItemsAsList();
}
@Override
protected List run_getItemsByName() throws Exception {
return getItemsByName();
}
}
提前谢谢你,我很抱歉我的英语不好
解决方案
推荐阅读
- apache-poi - 如何在 apache poi 中为 DataBar 添加负值?
- wpf - 如何在 xaml 中使用用户控件的多个实例
- reactjs - React 传递其他 Pages 中输入的值
- java - 有没有办法在 JAVA 中使用 AXIS2.0 从肥皂头中提取多个 cookie?
- python - BeautifulSoup 找不到来自 .select_one 的字符串
- server - 错误:在 NextJS 中连接 ECONNREFUSED 127.0.0.1:9001
- typescript - 如何在浏览器堆栈中使用多远程配置 wdio 功能
- javascript - 如何在自定义元素下拉列表中添加新的上下文堆叠
- python - 返回今天我的 django 模型对象被检索的次数
- google-chrome - 带有清单版本 3 的 CORS Chrome 扩展程序