使用feign配置網絡ip代理
feign配置網絡ip代理
問題描述
測試環境將需要訪問的外網地址加入瞭白名單,但是docker和宿主機網絡不一樣(試過掛載宿主機網絡也不行,但是掛載宿主機網絡會打亂原有的網絡環境),所以造成瞭在宿主機上面可以訪問該地址,但是docker裡面是訪問不到外網的地址,所使用feign的時候加上ip代理,代理宿主機ip來對外網地址進行訪問!
為什麼不直接對docker設置網絡代理,測試環境裡面基本都是內部服務調用,如果設置則會導致其網絡不一致,並且開發測試正式環境較為復雜,如果不需要的時候直接在配置文件配置為null就行
1.依賴
<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主要用來做網絡代理,依賴需要自行百度
2.feignclinet接口
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); }
3.Config
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) { } }); } } } }
4.yml
使用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
調用指定ip的feign接口
@FeignClient(value = “center-educational-server”,url=“http://127.0.0.1:10005”)
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例
- SpringBoot使用Feign調用其他服務接口
- Spring Cloud Feign 使用對象參數的操作
- springcloud 整合 openfeign的方法
- FeignClient服務器拋出異常客戶端處理方案