Hystrix命令就是HystrixCommand,用于封装具体的依赖服务调用逻辑。
实现方式:继承实现
package com.ribbon;
import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;
/**
* Created by qhe on 2018/7/26.
*
* 创建请求命令
* 继承的方式实现封装具体的依赖服务调用逻辑
*
*/
public class UserCommand extends HystrixCommand<User> {
private RestTemplate restTemplate;
private Long id;
public UserCommand(Setter setter,RestTemplate restTemplate,Long id){
super(setter);
this.restTemplate = restTemplate;
this.id = id;
}
@Override
protected User run() throws Exception {
return restTemplate.getForObject("Http://user-service/hello/user/{1}",User.class,id);
}
}
通过继承,我们可以实现请求的同步和异步执行
同步执行:User u = new UserCommand(restTemplate,1L).execute;
异步执行:Future<User> f = new UserCommand(restTemplate,1L).queue;然后再通过get方法获得结果。
实现方式:注解
package com.ribbon;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.Future;
/**
* Created by wangjing on 2018/7/26.
*
*
* 创建请求命令
* 利用注解形式
*/
public class UserService {
@Autowired
private RestTemplate restTemplate;
/**
*同步
* @param id
* @return
*/
@HystrixCommand
public User getUserById(Long id){
return restTemplate.getForObject("http",User.class,id);
}
/**
* 异步
* @param id
* @return
*/
public Future<User> getUserByIdAsync(final Long id){
return new AsyncResult<User>() {
@Override
public User invoke() {
return restTemplate.getForObject("Http",User.class,id);
}
};
}
}
实现响应式执行方式
Observable<Stirng> ho = new UserCommand(restTemplate,1L).observe();//热执行,立即执行
Observable<Stirng> co = new UserCommand(restTemplate,1L).toObserve();//冷执行,所有订阅者订阅后执行
HystrixObservableCommand实现命令封装,发射多次的Observable.
继承方式:
package com.ribbon;
import com.netflix.hystrix.HystrixObservableCommand;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import rx.Subscriber;
/**
* Created by qhe on 2018/7/26.
*
* 继承方式实现响应式的封装命令
*/
public class UserObservableCommand extends HystrixObservableCommand<User> {
private RestTemplate restTemplate;
private Long id;
public UserObservableCommand(Setter setter,RestTemplate restTemplate,Long id){
super(setter);
this.restTemplate = restTemplate;
this.id = id;
}
@Override
protected Observable<User> construct() {
return Observable.create(new Observable.OnSubscribe<User>(){
@Override
public void call(Subscriber<? super User> observer) {
if(!observer.isUnsubscribed()){
User user = restTemplate.getForObject("http",User.class,id);
observer.onNext(user);
observer.onCompleted();
}
}
});
}
}
注解方式
package com.ribbon;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import rx.Subscriber;
/**
* Created by wangjing on 2018/7/26.
*
* 注解形式实现响应式的封装命令
*/
public class UserObservableService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand
public Observable<User> getUserById(final String id){
return Observable.create(new Observable.OnSubscribe<User>(){
@Override
public void call(Subscriber<? super User> observer) {
if(!observer.isUnsubscribed()){
User user = restTemplate.getForObject("http",User.class,id);
observer.onNext(user);
observer.onCompleted();
}
}
});
}
}