feign配置网络ip代理
问题描述
1.依赖
2.feignclinet接口
3.Config
4.yml
调用指定ip的feign接口
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”)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易知道(ezd.cc)。