您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用feign配置網絡ip代理的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
測試環境將需要訪問的外網地址加入了白名單,但是docker和宿主機網絡不一樣(試過掛載宿主機網絡也不行,但是掛載宿主機網絡會打亂原有的網絡環境),所以造成了在宿主機上面可以訪問該地址,但是docker里面是訪問不到外網的地址,所使用feign的時候加上ip代理,代理宿主機ip來對外網地址進行訪問!
為什么不直接對docker設置網絡代理,測試環境里面基本都是內部服務調用,如果設置則會導致其網絡不一致,并且開發測試正式環境較為復雜,如果不需要的時候直接在配置文件配置為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相關依賴 feign-okhttp主要用來做網絡代理,依賴需要自行百度
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遠程調用的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代理設置 * @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 如果多個 用逗號分割
不使用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配置網絡ip代理”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。