背景
- 使用springmvc实现一个通用网关代理
- 使用webClient作为请求被代理服务的http客户端
- 代码大致如下:
@RequestMapping("/proxy/{path}/**")
public void execute(@PathVariable("path") String path,
HttpServletRequest request, HttpServletResponse response) throws IOException {
// 1. 准备请求, 得到webClient的WebClient.RequestBodyUriSpec proxyRequest
// 2. 处理一些业务逻辑, 确定被代理服务的请求地址
// 3. 执行请求
ResponseEntity<byte[]> proxyResponse = proxyRequest
.retrieve()
.toEntity(byte[].class)