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

同步调用

1
2
3
4
5
6
7
8
9
@Configuration
public class WebConfig {

@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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";
}
}
1
2
3
4
5
6
7
8
9
10
11
@RestController
public class HelloController {

@Autowired
HelloService helloService;

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

异步调用

修改Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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文件增加依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava -->
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.19</version>
</dependency>

修改Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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";
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :