Spring Cloud同步调用、异步调用、响应式调用

同步调用

@Configuration
public class WebConfig {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public String hello() {
        return restTemplate.getForEntity("http://HI-SERVICE/hello?name=Joe", String.class).getBody();
    }

    public String helloFallBack() {
        return "error";
    }
}
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/ribbon-hello", method = RequestMethod.GET)
    public String hello() {
      return helloService.hello();
    }
}

异步调用

修改Service

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public String hello() {
        return restTemplate.getForEntity("http://HI-SERVICE/hello?name=Joe", String.class).getBody();
    }

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public Future<String> helloAsync() {
        return new AsyncResult<String>() {
            @Override
            public String invoke() {
                return restTemplate.getForEntity("http://HI-SERVICE/hello?name=Joe", String.class).getBody();
            }
        };
    }

    public String helloFallBack() {
        return "error";
    }
}

修改Controller

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/ribbon-hello", method = RequestMethod.GET)
    public String hello() {
      return helloService.hello();
    }

    @RequestMapping(value = "/ribbon-hello-async", method = RequestMethod.GET)
    public String helloAsync() throws ExecutionException, InterruptedException {
        return helloService.helloAsync().get();
    }
}

响应式调用

pom文件增加依赖

<!-- https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava -->
<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxjava</artifactId>
    <version>2.2.19</version>
</dependency>

修改Service

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public String hello() {
        return restTemplate.getForEntity("http://HI-SERVICE/hello?name=Joe", String.class).getBody();
    }

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public Future<String> helloAsync() {
        return new AsyncResult<String>() {
            @Override
            public String invoke() {
                return restTemplate.getForEntity("http://HI-SERVICE/hello?name=Joe", String.class).getBody();
            }
        };
    }

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public Observable<String> helloResponse() {
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                String body = restTemplate.getForEntity("http://HI-SERVICE/hello?name=Joe", String.class).getBody();
                subscriber.onNext(body);
                subscriber.onCompleted();
            }
        });
    }

    public String helloFallBack() {
        return "error";
    }
}

修改Controller

@RequestMapping(value = "/ribbon-response", method = RequestMethod.GET)
public String helloResponse() 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";
}
上一篇 MySQL之mysqld_safe启动详解
下一篇 Eureka状态变更的接口
目录
文章列表
1 设计模式(18)代理模式
设计模式(18)代理模式
2
Android引用动态链接so库踩坑记录
Android引用动态链接so库踩坑记录
3
注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途
注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途
4
Java服务端开发记录
Java服务端开发记录
5
解决strings.xml格式化占位符错误:Multiple substitutions specified in non-positional format
解决strings.xml格式化占位符错误:Multiple substitutions specified in non-positional format
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。