Spring Cloud学习笔记(Hystrix):execute,queue,observe,toObservable样例和特性
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import rx.Observable;
public class CommandHelloWorld extends HystrixCommand<String> {
private final String name;
protected CommandHelloWorld(String name) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")));
this.name = name;
}
@Override
protected String run() {
return "Hello " + name + "!";
}
@Override
protected String getFallback() {
return "Hello Fallback!";
}
// 同步执行
public static void main(String[] args) {
CommandHelloWorld command = new CommandHelloWorld("World");
String result = command.execute(); // 执行命令,同步阻塞
System.out.println(result);
}
}
这个例子展示了如何使用HystrixCommand
的execute
方法来同步执行命令,并处理了回退逻辑。execute
方法在请求失败或线程被阻塞时调用回退逻辑。
评论已关闭