在项目开发中经常需要配置代理,特别是当需要外网访问的时候,从安全和运维规范来看,都要求部署应用的服务器在网络上有比较严格的隔离,流量进来使用反向代理,访问外网需要正向代理。
feign在使用http client的时候没有提供配置代理的配置项。但是可以通过替换HttpClientFactory来实现。一般有2中替换方式,一种是在初始化FeignClient的时候通过builder传入配置类代理的HttpClientFactory,另一种是通过替换Spring自动配置初始化的HttpClientFactory来实现。下面用okHttp3举例,ApacheHttpClient类似。
方法一(手动)
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809));
OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(proxy).build();
TestClient manualTestClient = new Feign.Builder()
.client(new feign.okhttp.OkHttpClient(okHttpClient))
.target(TestClient.class, "https://www.google.com");
String result = manualTestClient.search("feign");
这种方式需要手动初始化FeignClient,没办法自动注入,在使用的时候很不方便,代码可读性也比较差,远离了feign原来的那种只需要定义interface的优雅方式。
从spring cloud的openfeign包FeignAutoConfiguration类可以看到feign的http client是复用OkHttpClientFactory这个bean
@Bean
public okhttp3.OkHttpClient client(OkHttpClientFactory httpClientFactory, ConnectionPool connectionPool, FeignHttpClientProperties httpClientProperties) {
Boolean followRedirects = httpClientProperties.isFollowRedirects();
Integer connectTimeout = httpClientProperties.getConnectionTimeout();
Boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
this.okHttpClient = httpClientFactory.createBuilder(disableSslValidation)
.connectTimeout((long)connectTimeout,TimeUnit.MILLISECONDS)
.followRedirects(followRedirects).connectionPool(connectionPool).build();
return this.okHttpClient;
}
方法二(推荐)
从spring cloud包的HttpClientConfiguration看到OkHttpClientFactory有注解@ConditionalOnMissingBean,只要自定义一个OkHttpClientFactory就能替换掉这个默认的bean
@Bean
@ConditionalOnMissingBean
public OkHttpClientFactory okHttpClientFactory(Builder builder) {
return new DefaultOkHttpClientFactory(builder);
}
因此需要构造一个OkHttpClient的builder并加入代理。然后使用这个builder构造OkHttpClientFactory bean,替换的是spring默认的okHttpClient,如果需要精细地管理代理的话,可以使用ProxySelector而不是Proxy
首先将代理地址、端口以及需要代理的域名配置到配置文件
feign.okhttp.enabled=true
proxy.host=127.0.0.1
proxy.port=10809
proxy.domains=www.google.com,www.youtube.com
@Configuration
@EnableFeignClients(basePackages = "com.example.hello.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);
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) {
}
});
}
}
}
这时候所有的feign client只要域名配置在了proxy.domains就会通过代理访问外网,而其他域名则还是不通过代理访问,既不需要侵入feign的实例化过程,也实现了代理的精细化管理,同时也能在restTemplate等依赖HttpClientFactory的组件中配置入了代理。