问题描述,测试环境将需要访问的外网地址加入了白名单,但是docker的IP是不固定的,所以造成了在宿主机上面可以访问该地址,但是docker里面是访问不到外网的地址,所使用feign的时候加上ip代理,代理宿主机ip来对外网地址进行访问!为什么不直接对docker设置网络代理,测试环境里面基本都是内部服务调用,如果设置则会导致其网络不一致,并且开发测试正式环境较为复杂,如果不需要的时候直接在配置文件配置为null就行
1.依赖
org.apache.httpcomponents httpclient4.5.10 //可能还需要feign相关依赖 feign-okhttp主要用来做网络代理,依赖需要自行百度 io.github.openfeign 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; @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) ListgetEfdcCodeByProjectName( @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; @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 SetdomainList; @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 proxyList = new ArrayList<>(1); proxyList.add(proxy); builder.proxySelector(new ProxySelector() { @Override public List 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)