您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用feign配置網(wǎng)絡(luò)ip代理的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
測(cè)試環(huán)境將需要訪問的外網(wǎng)地址加入了白名單,但是docker和宿主機(jī)網(wǎng)絡(luò)不一樣(試過掛載宿主機(jī)網(wǎng)絡(luò)也不行,但是掛載宿主機(jī)網(wǎng)絡(luò)會(huì)打亂原有的網(wǎng)絡(luò)環(huán)境),所以造成了在宿主機(jī)上面可以訪問該地址,但是docker里面是訪問不到外網(wǎng)的地址,所使用feign的時(shí)候加上ip代理,代理宿主機(jī)ip來對(duì)外網(wǎng)地址進(jìn)行訪問!
為什么不直接對(duì)docker設(shè)置網(wǎng)絡(luò)代理,測(cè)試環(huán)境里面基本都是內(nèi)部服務(wù)調(diào)用,如果設(shè)置則會(huì)導(dǎo)致其網(wǎng)絡(luò)不一致,并且開發(fā)測(cè)試正式環(huán)境較為復(fù)雜,如果不需要的時(shí)候直接在配置文件配置為null就行
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency> //可能還需要feign相關(guān)依賴 feign-okhttp主要用來做網(wǎng)絡(luò)代理,依賴需要自行百度
import io.swagger.annotations.ApiOperation; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; /** * @ClassName * @Description url遠(yuǎn)程調(diào)用的url * @Author liuf * @Date 2021/10/29 16:19 * @Version 1.0 **/ @FeignClient(url = "http://xxx.xxx.xxx.xxx:8090" ,name = "slmodel-one") public interface SlModelOneClient { @ApiOperation("XXXXXXX") @RequestMapping( method = RequestMethod.GET, value = "/efdcserver/efdcserver/getEfdcCodeByProjectName", consumes = "application/json;charset=UTF-8", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) List<JsonAreaCode> getEfdcCodeByProjectName( @RequestParam("projectName") String projectName); @ApiOperation("XXXXXXX") @RequestMapping( method = RequestMethod.POST, value = "/efdcserver/hydro/getDepthMapByPost?efdcCode={efdcCode}&planName={planName}", consumes = "application/json;charset=UTF-8", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) DepthMap getDepthMapByPost( @PathVariable(name="efdcCode") String efdcCode, @PathVariable(name ="planName")String planName); @ApiOperation("XXXXXXX") @RequestMapping( method = RequestMethod.GET, value = "/efdcserver/hydro/getPoint?planName={planName}&efdcCode={efdcCode}&lgtd={lgtd}<td={lttd}", consumes = "application/json;charset=UTF-8", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) DepthMap getPointDepthByGet( @PathVariable(name ="planName")String planName, @PathVariable(name="efdcCode") String efdcCode , @PathVariable(name ="lotd")Double lgtd, @PathVariable(name ="lttd")Double lttd); }
import okhttp3.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.commons.httpclient.DefaultOkHttpClientFactory; import org.springframework.cloud.commons.httpclient.OkHttpClientFactory; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; import java.net.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; /** * @Description: feign代理設(shè)置 * @Author: liuf * @Date: * @Param: * @Return: **/ @Configuration @EnableFeignClients(basePackages = "com.ceshi..map.client") public class Config { @Value("${proxy.host}") private String proxyHost; @Value("${proxy.port}") private Integer proxyPort; @Value("#{'${proxy.domains}'.split(',')}") private Set<String> domainList; @Bean public OkHttpClientFactory okHttpClientFactory(OkHttpClient.Builder builder) { return new ProxyOkHttpClientFactory(builder); } class ProxyOkHttpClientFactory extends DefaultOkHttpClientFactory { public ProxyOkHttpClientFactory(OkHttpClient.Builder builder) { super(builder); //如果配置文件中的代理信息為null 則該代理ip配置不生效 if(proxyHost!=null&&proxyPort!=null&&domainList!=null) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); List<Proxy> proxyList = new ArrayList<>(1); proxyList.add(proxy); builder.proxySelector(new ProxySelector() { @Override public List<Proxy> select(URI uri) { if (uri == null || !domainList.contains(uri.getHost())) { return Collections.singletonList(Proxy.NO_PROXY); } return proxyList; } @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { } }); } } } }
使用IP代理
feign: okhttp: enabled: true proxy: host: 199.168.233.32 //需要代理的ip port: 4444 domains: 222.222.231.116,222.222.231.117 //需要訪問的地址 host 如果多個(gè) 用逗號(hào)分割
不使用IP代理
feign: okhttp: enabled: true proxy: host: null port: null domains: null
@FeignClient(value = “center-educational-server”,url=“http://127.0.0.1:10005”)
以上就是“怎么使用feign配置網(wǎng)絡(luò)ip代理”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。