okhttp---feign另一个http客户端,方便使用代理
同一个k8s直接掉,在一个集群里用http://imp-www.-test.svc.cluster.local:1398这种,不在一个集群用location(先到宿主机,再由宿主访问k8s内部)框架自动装配内部的思路,和我们手动写过程一致,当没有自动装配的例子,可以通过手动写的例子知道大致过程,即可知道。impTask: http://10.1.1.1:8080服务地址(宿主机+端口)---内网可
同一个k8s直接掉,在一个集群里用http://imp-www.-test.svc.cluster.local:1398这种,不在一个集群用location(先到宿主机,再由宿主访问k8s内部)
imp-www.-test.svc.cluster.local:1398 这种地址本地处理不了,可以用宿主机ip
feign的http客户端有两种:
1,httpclient
2,okhttp
自动带有连接池,连接复用减少延迟
自动giz压缩
缓存响应数据减少请求
支持http2
共享scoket
便于设置代理访问
框架自动装配内部的思路,和我们手动写过程一致,当没有自动装配的例子,可以通过手动写的例子知道大致过程,即可知道
在哪里设置自动装配
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.0</version> </dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
impTask: http://10.1.1.1:8080 服务地址(宿主机+端口)---内网可以直接访问
feign:
okhttp:
enabled: true -开启okhttp
proxy:
host: 10.43.75.154 ---代理服务器地址
port: 49909
domains: 10.1.1.1:8080 ---需要访问的地址(不需要端口,可以是域名)
username: admin
password: admin
package com..config;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
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 javax.annotation.Nullable;
@Configuration
@EnableFeignClients(basePackages = "com.***")
public class FeignClientConfig {
@Configuration
static class OkHttpClientConfiguration {
@Value("${proxy.host}")
private String proxyHost;
@Value("${proxy.port}")
private Integer proxyPort;
@Value("#{'${proxy.domains}'.split(',')}")
private Set<String> domainList;
@Value("${proxy.username}")
private String username;
@Value("${proxy.password}")
private String password;
@Bean
public OkHttpClientFactory okHttpClientFactory(OkHttpClient.Builder builder) {
return new ProxyOkHttpClientFactory(builder);
}
public class ProxyOkHttpClientFactory extends DefaultOkHttpClientFactory {
public ProxyOkHttpClientFactory(OkHttpClient.Builder builder) {
super(builder);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
//代理服务器认证 builder.authenticator(o3)这种是https认证
builder.proxyAuthenticator((route, response) -> {
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
});
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())) {
System.out.println("uri.getHost():" + uri.getHost());
return Collections.singletonList(Proxy.NO_PROXY);
}
return proxyList;
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
}
}
}
}
参看:
更多推荐
所有评论(0)