SpringBoot利用RestTemplate實(shí)現(xiàn)反向代理
之前發(fā)過(guò)一篇SpringBoot利用Undertow實(shí)現(xiàn)高可用的反向代理配置,有同學(xué)反饋tomcat用習(xí)慣了,擔(dān)心切換Undertow有一定的風(fēng)險(xiǎn)。
今天分享另一種利用RestTemplate客戶端工具來(lái)實(shí)現(xiàn)簡(jiǎn)單而高效的反向代理功能,底層無(wú)論使用哪種web服務(wù)均可使用。
什么是反向代理
反向代理是指以代理服務(wù)器接收客戶端請(qǐng)求,然后將請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)部服務(wù)器,并將內(nèi)部服務(wù)器的響應(yīng)返回給客戶端。
客戶端只與反向代理服務(wù)器通信,不直接訪問(wèn)內(nèi)部服務(wù)器。
為什么選擇RestTemplate實(shí)現(xiàn)反向代理
集成便捷:RestTemplate是Spring框架的核心組件,在SpringBoot項(xiàng)目中使用非常方便 功能豐富:支持各種HTTP方法、請(qǐng)求頭處理、響應(yīng)類型轉(zhuǎn)換等 可定制性強(qiáng):可以通過(guò)配置ClientHttpRequestFactory來(lái)自定義連接池、超時(shí)等參數(shù) 便于擴(kuò)展:可以結(jié)合攔截器實(shí)現(xiàn)更復(fù)雜的代理邏輯
實(shí)現(xiàn)步驟
1. 添加依賴
首先在pom.xml中添加必要的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
2. 配置RestTemplate
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);
return new RestTemplate(factory);
}
}
3. 實(shí)現(xiàn)代理控制器
@RestController
@RequestMapping("/proxy")
public class ProxyController {
@Autowired
private RestTemplate restTemplate;
// 目標(biāo)服務(wù)器基礎(chǔ)URL
private static final String TARGET_SERVER = "http://target-service.com";
@RequestMapping("/**")
public ResponseEntity<String> proxyRequest(HttpServletRequest request,
@RequestBody(required = false) String body) {
try {
// 構(gòu)建目標(biāo)URL
String requestUri = request.getRequestURI();
String proxyPath = requestUri.substring("/proxy".length());
String queryString = request.getQueryString();
StringBuilder targetUrl = new StringBuilder(TARGET_SERVER);
targetUrl.append(proxyPath);
if (queryString != null) {
targetUrl.append("?").append(queryString);
}
// 復(fù)制請(qǐng)求頭
HttpHeaders headers = new HttpHeaders();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
// 排除一些不需要轉(zhuǎn)發(fā)的頭部
if (!headerName.equalsIgnoreCase("host")) {
headers.set(headerName, request.getHeader(headerName));
}
}
// 創(chuàng)建請(qǐng)求實(shí)體
HttpEntity<String> httpEntity = new HttpEntity<>(body, headers);
// 確定HTTP方法
HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
// 發(fā)送請(qǐng)求并獲取響應(yīng)
ResponseEntity<String> responseEntity =
restTemplate.exchange(targetUrl.toString(), httpMethod, httpEntity, String.class);
return responseEntity;
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("代理請(qǐng)求失敗: " + e.getMessage());
}
}
}
4. 處理文件上傳和下載
對(duì)于需要處理文件上傳和下載的情況,可以這樣實(shí)現(xiàn):
@PostMapping("/upload/**")
public ResponseEntity<byte[]> proxyUpload(HttpServletRequest request,
MultipartHttpServletRequest multipartRequest) {
try {
// 獲取文件部分
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
multipartRequest.getFileMap().forEach((name, file) -> {
try {
ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
parts.add(name, resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
// 獲取其他表單參數(shù)
multipartRequest.getParameterMap().forEach((name, values) -> {
for (String value : values) {
parts.add(name, value);
}
});
// 構(gòu)建URL
String requestUri = request.getRequestURI();
String proxyPath = requestUri.substring("/proxy/upload".length());
String targetUrl = TARGET_SERVER + "/upload" + proxyPath;
// 設(shè)置請(qǐng)求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// 發(fā)送請(qǐng)求
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(parts, headers);
ResponseEntity<byte[]> response = restTemplate.exchange(
targetUrl, HttpMethod.POST, requestEntity, byte[].class);
return response;
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
5. 增強(qiáng)功能:實(shí)現(xiàn)請(qǐng)求攔截和轉(zhuǎn)換
通過(guò)添加攔截器,我們可以在請(qǐng)求前后進(jìn)行處理:
@Configuration
@Slf4j
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);
RestTemplate restTemplate = new RestTemplate(factory);
// 添加攔截器
restTemplate.setInterceptors(
Collections.singletonList((request, body, execution) -> {
log.info("請(qǐng)求URL: {}", request.getURI());
// 請(qǐng)求前處理
HttpHeaders headers = request.getHeaders();
headers.add("X-Forwarded-By", "Spring-Proxy");
// 執(zhí)行請(qǐng)求
ClientHttpResponse response = execution.execute(request, body);
// 響應(yīng)后處理
// 這里可以修改響應(yīng),例如添加頭部、修改內(nèi)容等
return response;
})
);
return restTemplate;
}
}
6. 連接池配置
@Configuration
@Slf4j
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
/*HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);*/
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(Timeout.of(5000, TimeUnit.MILLISECONDS))
.setResponseTimeout(Timeout.of(5000, TimeUnit.MILLISECONDS))
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.build();
HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
// 添加攔截器
restTemplate.setInterceptors(
Collections.singletonList((request, body, execution) -> {
log.info("請(qǐng)求URL: {}", request.getURI());
// 請(qǐng)求前處理
HttpHeaders headers = request.getHeaders();
headers.add("X-Forwarded-By", "Spring-Proxy");
// 執(zhí)行請(qǐng)求
ClientHttpResponse response = execution.execute(request, body);
// 響應(yīng)后處理
// 這里可以修改響應(yīng),例如添加頭部、修改內(nèi)容等
return response;
})
);
return restTemplate;
}
}
安全考慮
請(qǐng)求驗(yàn)證:確保只轉(zhuǎn)發(fā)合法請(qǐng)求 敏感信息過(guò)濾:過(guò)濾敏感頭部或請(qǐng)求內(nèi)容 限流措施:防止過(guò)多請(qǐng)求導(dǎo)致目標(biāo)服務(wù)過(guò)載
// 示例:請(qǐng)求驗(yàn)證
private boolean validateRequest(HttpServletRequest request) {
// 實(shí)現(xiàn)驗(yàn)證邏輯,例如檢查認(rèn)證信息、IP白名單等
String authToken = request.getHeader("Authorization");
return authToken != null && authService.isValidToken(authToken);
}
總結(jié)
通過(guò)SpringBoot和RestTemplate,我們可以快速實(shí)現(xiàn)一個(gè)功能完備的反向代理。
相比于專門(mén)的代理服務(wù)器如Nginx,這種方式更加靈活,可以與業(yè)務(wù)邏輯緊密結(jié)合,適合實(shí)現(xiàn)特定的代理需求。
但對(duì)于大規(guī)模的代理場(chǎng)景,還是推薦使用專門(mén)的代理軟件。
到此這篇關(guān)于SpringBoot利用RestTemplate實(shí)現(xiàn)反向代理的文章就介紹到這了,更多相關(guān)SpringBoot RestTemplate反向代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot同一接口多個(gè)實(shí)現(xiàn)類配置的實(shí)例詳解
這篇文章主要介紹了SpringBoot同一接口多個(gè)實(shí)現(xiàn)類配置的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Spring IOC相關(guān)注解運(yùn)用(上篇)
這篇文章主要介紹了Spring?IOC相關(guān)注解的運(yùn)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
解決IDEA上循環(huán)依賴報(bào)錯(cuò)問(wèn)題Error:java: Annotation processing&n
這篇文章主要介紹了解決IDEA上循環(huán)依賴報(bào)錯(cuò)問(wèn)題Error:java: Annotation processing is not supported for module cycles,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2023-10-10
SpringCloud集成Micrometer Tracing的代碼工程
Micrometer Tracing 是一個(gè)用于微服務(wù)架構(gòu)的追蹤庫(kù),它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)收集和報(bào)告分布式系統(tǒng)中的性能和調(diào)用鏈信息,Micrometer Tracing 旨在幫助開(kāi)發(fā)者和運(yùn)維人員理解微服務(wù)之間的交互,本文給大家介紹了如何在 Spring Cloud 集成 Micrometer Tracing2024-12-12

