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

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 helloConsumerAsyn() {
        return new AsyncResult() {
            @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文件增加依赖

 
            io.reactivex.rxjava2
            rxjava
            2.1.8
        
修改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 helloConsumerAsyn() {
        return new AsyncResult() {
            @Override
            public String invoke() {
                return restTemplate.getForEntity("http://HI-SERVICE/hello?name=youlangta", String.class).getBody();
            }
        };
    }

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public Observable helloConsumerResponse() {
        return Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(Subscriber 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() {
            @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

后台日志:

spring cloud学习笔记3(同步调用、异步调用、响应式调用)_第1张图片


你可能感兴趣的:(java)