Created
October 24, 2020 06:09
-
-
Save gexiangdong/7ec22fe99961eefc15fd46d7a1f68196 to your computer and use it in GitHub Desktop.
SpringGateWay中处理请求的几个例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a Mono that just return an empty list | |
Mono<List> mono = Mono.create( | |
(callback) -> { | |
callback.success(Collections.emptyList()); | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 访问另外一个服务,获取结果,此例返回值被转为Map类型 | |
Mono<Map> monoA = WebClient.builder() | |
.build() | |
.get() | |
.uri(uriA) | |
.header("X-Real-Ip", request.getRemoteAddress().getAddress().toString()) | |
.retrieve(); | |
Mono<Map> monoB = WebClient.builder() | |
.build() | |
.get() | |
.uri(uriB) | |
.header("X-Real-Ip", request.getRemoteAddress().getAddress().toString()) | |
.retrieve(); | |
//合并2个请求的返回 | |
Mono<Map> mergedResult = Mono.zip(monoA, monoB).map( | |
r -> { | |
Map m1 = r.getT1(); | |
Map m2 = r.getT2(); | |
//处理2个请求的结果,合并 | |
Map result = new HashMap(); | |
result.putAll(m1); | |
result.putAll(m2); | |
return result; | |
}); | |
DataBufferFactory dataBufferFactory = exchange.getResponse().bufferFactory(); | |
return exchange | |
.getResponse() | |
.writeWith( | |
mergedResult.map( | |
r -> { | |
byte[] bin = objectMapper.writeValueAsBytes(r); | |
return dataBufferFactory.wrap(bin) | |
} | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修改request | |
@Override | |
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | |
ServerHttpRequest request = exchange.getRequest(); | |
ServerHttpRequest newRequest = exchange.getRequest().mutate().header("cus_header", "some value").build(); | |
return chain.filter(exchange.mutate().request(newRequest).build()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 在路由中直接返回内容 | |
public GatewayFilter apply(CashierJournalGatewayFilterFactory.Config config) { | |
return (exchange, chain) -> { | |
String content = "要返回的内容"; | |
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(content.getBytes()); | |
return exchange.getResponse().writeWith(Flux.just(buffer)); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment