feign-reactive

2023-09-30

java

晚饭少吃口,活到九十九。——钱大昕

https://github.com/openfeign/feign

目前feign不支持webfluxreactive

于是找到了一个三方库

https://github.com/kptfh/feign-reactive

基于Spring WebFlux的Reactive Feign客户端

安装:

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
<repositories>
<repository>
<id>bintray-kptfh-feign-reactive</id>
<name>bintray</name>
<url>https://dl.bintray.com/kptfh/feign-reactive</url>
</repository>
</repositories>
...
<dependencies>
...

<dependency>
<groupId>io.github.reactivefeign</groupId>
<artifactId>feign-reactor-cloud</artifactId>
<version>1.0.0</version>
</dependency>

or if you don't need cloud specific functionality

<dependency>
<groupId>io.github.reactivefeign</groupId>
<artifactId>feign-reactor-webclient</artifactId>
<version>1.0.0</version>
</dependency>

or if you tend to use Rx2 interfaces

<dependency>
<groupId>io.github.reactivefeign</groupId>
<artifactId>feign-reactor-rx2</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>

使用:

像往常一样编写假装 API,但接口的每个方法

  • 可以接受 org.reactivestreams.Publisher 作为正文
  • 必须返回 reactor.core.publisher.Mono 或 reactor.core.publisher.Flux 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Headers({ "Accept: application/json" })
public interface IcecreamServiceApi {

@RequestLine("GET /icecream/flavors")
Flux<Flavor> getAvailableFlavors();

@RequestLine("GET /icecream/mixins")
Flux<Mixin> getAvailableMixins();

@RequestLine("POST /icecream/orders")
@Headers("Content-Type: application/json")
Mono<Bill> makeOrder(IceCreamOrder order);

@RequestLine("GET /icecream/orders/{orderId}")
Mono<IceCreamOrder> findOrder(@Param("orderId") int orderId);

@RequestLine("POST /icecream/bills/pay")
@Headers("Content-Type: application/json")
Mono<Void> payBill(Publisher<Bill> bill);
}

构建客户端:

1
2
3
4
5
6
7
8
/* Create instance of your API */
IcecreamServiceApi client = ReactiveFeign
.builder()
.target(IcecreamServiceApi.class, "http://www.icecreame.com")

/* Execute nonblocking requests */
Flux<Flavor> flavors = icecreamApi.getAvailableFlavors();
Flux<Mixin> mixins = icecreamApi.getAvailableMixins();

cloud aware客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 IcecreamServiceApi client = CloudReactiveFeign.<IcecreamServiceApi>builder()
.setFallback(new TestInterface() {
@Override
public Mono<String> get() {
return Mono.just("fallback");
}
})
.setLoadBalancerCommand(
LoadBalancerCommand.builder()
.withLoadBalancer(AbstractLoadBalancer.class.cast(getNamedLoadBalancer(serviceName)))
.withRetryHandler(new DefaultLoadBalancerRetryHandler(1, 1, true))
.build()
)
.target(IcecreamServiceApi.class, "http://" + serviceName);

/* Execute nonblocking requests */
Flux<Flavor> flavors = icecreamApi.getAvailableFlavors();
Flux<Mixin> mixins = icecreamApi.getAvailableMixins();