spring cloud学习笔记3(同步调用、异步调用、响应式调用)

java 专栏收录该内容
4 篇文章 0 订阅

1.同步调用

 同步调用,在学习笔记2就是同步调用

2.异步调用

修改service

  1. package com.study.cloud.consumer.services;
  2. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  3. import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.client.RestTemplate;
  7. import java.util.concurrent.Future;
  8. @Service
  9. public class HelloService {
  10. @Autowired
  11. RestTemplate restTemplate;
  12. @HystrixCommand(fallbackMethod = "helloFallBack")
  13. public String helloConsumer() {
  14. return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
  15. }
  16. @HystrixCommand(fallbackMethod = "helloFallBack")
  17. public Future<String> helloConsumerAsyn() {
  18. return new AsyncResult<String>() {
  19. @Override
  20. public String invoke() {
  21. return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
  22. }
  23. };
  24. }
  25. public String helloFallBack() {
  26. return "error";
  27. }
  28. }
修改controller

  1. package com.study.cloud.consumer.controllers;
  2. import com.study.cloud.consumer.services.HelloService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. import java.util.concurrent.ExecutionException;
  9. @RestController
  10. public class ConsumerController {
  11. @Autowired
  12. HelloService helloService;
  13. @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
  14. public String helloConsumer() {
  15. return helloService.helloConsumer();
  16. }
  17. @RequestMapping(value = "/ribbon-consumerSyn", method = RequestMethod.GET)
  18. public String helloConsumerSyn() throws ExecutionException, InterruptedException {
  19. return helloService.helloConsumerAsyn().get();
  20. }
  21. }
调用http://localhost:9002/ribbon-consumerSyn,则是异步调用


3.响应式调用

pom文件增加依赖

  1. <dependency>
  2. <groupId>io.reactivex.rxjava2</groupId>
  3. <artifactId>rxjava</artifactId>
  4. <version>2.1.8</version>
  5. </dependency>
修改service

  1. package com.study.cloud.consumer.services;
  2. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  3. import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.client.RestTemplate;
  7. import rx.Observable;
  8. import rx.Subscriber;
  9. import java.util.concurrent.Future;
  10. @Service
  11. public class HelloService {
  12. @Autowired
  13. RestTemplate restTemplate;
  14. @HystrixCommand(fallbackMethod = "helloFallBack")
  15. public String helloConsumer() {
  16. return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
  17. }
  18. @HystrixCommand(fallbackMethod = "helloFallBack")
  19. public Future<String> helloConsumerAsyn() {
  20. return new AsyncResult<String>() {
  21. @Override
  22. public String invoke() {
  23. return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
  24. }
  25. };
  26. }
  27. @HystrixCommand(fallbackMethod = "helloFallBack")
  28. public Observable<String> helloConsumerResponse() {
  29. return Observable.create(new Observable.OnSubscribe<String>() {
  30. @Override
  31. public void call(Subscriber<? super String> subscriber) {
  32. String body = restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
  33. subscriber.onNext(body);
  34. subscriber.onCompleted();
  35. }
  36. });
  37. }
  38. public String helloFallBack() {
  39. return "error";
  40. }
  41. }
修改controller

  1. @RequestMapping(value = "/ribbon-consumerResponse", method = RequestMethod.GET)
  2. public String helloConsumerResponse() throws ExecutionException, InterruptedException {
  3. helloService.helloConsumerResponse().subscribe(new Subscriber<String>() {
  4. @Override
  5. public void onCompleted() {
  6. System.out.println("完成");
  7. }
  8. @Override
  9. public void onError(Throwable e) {
  10. }
  11. @Override
  12. public void onNext(String s) {
  13. System.out.println("接收到了:"+s);
  14. }
  15. });
  16. return "ok";
  17. }
调用http://localhost:9002/ribbon-consumerResponse

后台日志: