1.同步调用
同步调用,在学习笔记2就是同步调用
2.异步调用
修改service
package com.study.cloud.consumer.services;
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.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.Future;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloFallBack")
public String helloConsumer() {
return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
}
@HystrixCommand(fallbackMethod = "helloFallBack")
public Future<String> helloConsumerAsyn() {
return new AsyncResult<String>() {
@Override
public String invoke() {
return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
}
};
}
public String helloFallBack() {
return "error";
}
}
修改controller
package com.study.cloud.consumer.controllers;
import com.study.cloud.consumer.services.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.ExecutionException;
@RestController
public class ConsumerController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return helloService.helloConsumer();
}
@RequestMapping(value = "/ribbon-consumerSyn", method = RequestMethod.GET)
public String helloConsumerSyn() throws ExecutionException, InterruptedException {
return helloService.helloConsumerAsyn().get();
}
}
调用http://localhost:9002/ribbon-consumerSyn,则是异步调用
3.响应式调用
pom文件增加依赖
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.1.8</version>
</dependency>
修改service
package com.study.cloud.consumer.services;
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.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import rx.Subscriber;
import java.util.concurrent.Future;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloFallBack")
public String helloConsumer() {
return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
}
@HystrixCommand(fallbackMethod = "helloFallBack")
public Future<String> helloConsumerAsyn() {
return new AsyncResult<String>() {
@Override
public String invoke() {
return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
}
};
}
@HystrixCommand(fallbackMethod = "helloFallBack")
public Observable<String> helloConsumerResponse() {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
String body = restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
subscriber.onNext(body);
subscriber.onCompleted();
}
});
}
public String helloFallBack() {
return "error";
}
}
修改controller
@RequestMapping(value = "/ribbon-consumerResponse", method = RequestMethod.GET)
public String helloConsumerResponse() throws ExecutionException, InterruptedException {
helloService.helloConsumerResponse().subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
System.out.println("完成");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
System.out.println("接收到了:"+s);
}
});
return "ok";
}
调用http://localhost:9002/ribbon-consumerResponse
后台日志: