SpringBoot設(shè)置HTTP代理訪問(wèn)
遇到這樣的一個(gè)場(chǎng)景,代碼部署到私有服務(wù)器上去之后,這臺(tái)私有服務(wù)器a無(wú)法直接訪問(wèn)公網(wǎng),需要通過(guò)代理轉(zhuǎn)發(fā)到另外一臺(tái)專門(mén)訪問(wèn)公網(wǎng)的服務(wù)器b, 讓服務(wù)器b去請(qǐng)求對(duì)應(yīng)的公網(wǎng)ip,于是就需要設(shè)置Http代理。
通常使用springboot會(huì)用到兩種不同的httpclient
- spring-boot-starter-webflux 這個(gè)包下面的WebClient
- Spring-web 這個(gè)包下面的RestTemplate
WebClient
通過(guò)設(shè)置Httpclinet添加綁定的代理地址,然后給WebClient里添加這個(gè)Httpclient
HttpClient httpClient = HttpClient.create()
.proxy(proxy -> proxy
.type(ProxyProvider.Proxy.HTTP)
.address(new InetSocketAddress("192.168.0.1", 8080)) // 你的代理IP和端口
);
?
WebClient webClient = webClientBuilder
.baseUrl("https://www.baidu.com")
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
?
?
String result = webClient.get()
.retrieve()
.bodyToMono(String.class)
.block();RestTemplate
// 服務(wù)器代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.1", 8080));
// 配置 request factory
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setProxy(proxy);
?
RestTemplate restTemplate = new RestTemplate();
ProxySetting proxySetting = SpringContextHolder.getBean(ProxySetting.class);
// This allows us to read the response more than once - Necessary for debugging.
// 如果開(kāi)啟代理,則配置走服務(wù)器代理。否則走本地調(diào)試
if (proxySetting.isEnable()) {
restTemplate.setRequestFactory(requestFactory);
} else {
restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
}
?
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(); uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
restTemplate.setUriTemplateHandler(uriBuilderFactory);
?
String url = "https://www.baidu.com"
String response = restTemplate.getForObject(url, String.class);到此這篇關(guān)于SpringBoot設(shè)置HTTP代理訪問(wèn)的文章就介紹到這了,更多相關(guān)SpringBoot設(shè)置HTTP代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java異常:java.net.UnknownHostException產(chǎn)生的原因和解決方案
這篇文章主要給大家介紹了關(guān)于Java異常:java.net.UnknownHostException產(chǎn)生的原因和解決方案,這個(gè)異常是java.net包中的一部分,具體說(shuō)它是類的一個(gè)實(shí)例,異常通常是由主機(jī)名無(wú)法解析為IP地址引起的,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Flowable數(shù)據(jù)庫(kù)表分類及數(shù)據(jù)字典解析
這篇文章主要介紹了Flowable數(shù)據(jù)庫(kù)表分類及數(shù)據(jù)字典解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java實(shí)現(xiàn)線程同步的四種方式總結(jié)
Java線程同步屬于Java多線程與并發(fā)編程的核心點(diǎn),需要重點(diǎn)掌握,下面我就來(lái)詳解Java線程同步的4種主要的實(shí)現(xiàn)方式,需要的可以參考一下2022-09-09
spring boot與spring mvc的區(qū)別及功能介紹
這篇文章主要介紹了spring boot與spring mvc的區(qū)別是什么以及spring boot和spring mvc功能介紹,感興趣的朋友一起看看吧2018-02-02
SpringCloud中使用webclient(get和post)請(qǐng)求微服務(wù)接口數(shù)據(jù)
在SpringCloud項(xiàng)目中使用WebClient調(diào)用微服務(wù)時(shí),涉及配置WebClient、發(fā)起get和post請(qǐng)求等操作,如請(qǐng)求頭設(shè)置、服務(wù)地址配置、數(shù)據(jù)轉(zhuǎn)換處理、異常處理等,避免在循環(huán)中使用WebClient請(qǐng)求、路徑設(shè)置細(xì)節(jié)以及數(shù)據(jù)返回處理技巧,本文旨在幫助理解和應(yīng)用WebClient進(jìn)行微服務(wù)調(diào)用2024-10-10
Java關(guān)鍵字finally_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
java關(guān)鍵字finally不管是否出現(xiàn)異常,finally子句總是在塊完成之前執(zhí)行。下面通過(guò)實(shí)現(xiàn)代碼給大家介紹Java關(guān)鍵字finally相關(guān)知識(shí),需要的的朋友參考下吧2017-04-04

