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<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

后台日志:




  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值