Spring 云 Feign/Ribbon 使用公司代理
Spring Cloud Feign/Ribbon with corporate proxy
我想使用带有身份验证的公司代理背后的外部世界的 REST 服务。
如何配置 Spring Boot + Spring Cloud Feign/Ribbon 以使用我们的代理?
我相信您正在寻找这样的东西:
import feign.Feign;
import okhttp3.OkHttpClient;
import java.net.InetSocketAddress;
import java.net.Proxy;
...
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-url", 1234));
OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(proxy).build();
Feign.builder()
.client(new feign.okhttp.OkHttpClient(okHttpClient))
.target(...);
您只需将 compile 'io.github.openfeign:feign-okhttp:9.5.0'
添加到您的项目中。
target
子句包含您定义的接口。进一步参考:https://github.com/OpenFeign/feign
事实证明实际上有一个更简单的解决方案。
以下信息会有所帮助(也适用于更高级的用例):
OpenFeign 客户端可以 运行 与多个 HTTP 客户端。
默认情况下它使用 java.net.URLConnection
,但您也可以使用 ApacheHttpClient
或 OkHttpClient
.
使用 Apache Http 客户端
以下是使用 ApacheHttpClient
设置代理的方法:
- 将以下两个依赖项添加到您的 pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Dependency to switch HttpClient implementation from java.net.URLConnection to Apache HTTP Client -->
<!-- See also: FeignAutoConfiguration for details. -->
<!-- See also: https://cloud.spring.io/spring-cloud-commons/reference/html/#http-clients -->
<!-- See also: https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-overriding-defaults -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
在您的应用程序中公开以下 bean:
// see: https://cloud.spring.io/spring-cloud-commons/reference/html/#http-clients
@Bean
public HttpClientBuilder proxiedHttpClient() {
String proxyHost = "client-envoy";
Integer proxyPort = 80
String proxyScheme = "http";
return HttpClientBuilder.create()
.setProxy(new HttpHost(proxyHost, proxyPort, proxyScheme));
}
就是这样 - application.yaml
中不需要配置任何其他内容,因为如果 ApacheHttpClient
在类路径中,默认情况下将使用它。
使用 Ok Http 客户端
要使用 OkHttpClient
设置代理,您需要做类似的事情:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
在您的 application.yml
中确保启用 OkHttpClient 并禁用 ApacheHttpClient:
spring:
cloud:
httpclientfactories:
ok:
enabled: true
apache:
enabled: false
feign:
okhttp:
enabled: true
httpclient:
enabled: false
而不是 HttpClientBuilder
公开类型为 OkHttpClient.Builder
的 bean。
Spring cloud feign支持三种底层实现:
- 默认
- Apache HttpClient
- OkHttpClient
如果使用默认值:
创建这个 spring bean(比如通过在 class 内部定义 @Configuration
注释),不需要在应用程序中进行更改 properties/yml:
@Bean
public Client feignClient() {
return new Client.Proxied(
null, null, new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
}
如果使用 Apache HttpClient:
that means you have feign.httpclient.enabled: true
in application.yml
and below in your pom.xml
or build.gradle
:
pom.xml
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
build.gradle
implementation 'io.github.openfeign:feign-httpclient'
创建这个 spring bean(比如通过在 class 中定义 @Configuration
注释):
@Bean
public CloseableHttpClient feignClient() {
return HttpClientBuilder.create().setProxy(new HttpHost(proxyHost, proxyPort)).build();
}
如果使用 OkHttpClient:
that means you have feign.okhttp.enabled: true
in application.yml
and below in your pom.xml
or build.gradle
:
pom.xml
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
build.gradle
implementation 'io.github.openfeign:feign-okhttp'
创建这个 spring bean(比如通过在 class 中定义 @Configuration
注释):
@Bean
public OkHttpClient feignClient() {
return new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.build();
}
我想使用带有身份验证的公司代理背后的外部世界的 REST 服务。
如何配置 Spring Boot + Spring Cloud Feign/Ribbon 以使用我们的代理?
我相信您正在寻找这样的东西:
import feign.Feign;
import okhttp3.OkHttpClient;
import java.net.InetSocketAddress;
import java.net.Proxy;
...
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-url", 1234));
OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(proxy).build();
Feign.builder()
.client(new feign.okhttp.OkHttpClient(okHttpClient))
.target(...);
您只需将 compile 'io.github.openfeign:feign-okhttp:9.5.0'
添加到您的项目中。
target
子句包含您定义的接口。进一步参考:https://github.com/OpenFeign/feign
事实证明实际上有一个更简单的解决方案。
以下信息会有所帮助(也适用于更高级的用例):
OpenFeign 客户端可以 运行 与多个 HTTP 客户端。
默认情况下它使用 java.net.URLConnection
,但您也可以使用 ApacheHttpClient
或 OkHttpClient
.
使用 Apache Http 客户端
以下是使用 ApacheHttpClient
设置代理的方法:
- 将以下两个依赖项添加到您的 pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Dependency to switch HttpClient implementation from java.net.URLConnection to Apache HTTP Client -->
<!-- See also: FeignAutoConfiguration for details. -->
<!-- See also: https://cloud.spring.io/spring-cloud-commons/reference/html/#http-clients -->
<!-- See also: https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-overriding-defaults -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
在您的应用程序中公开以下 bean:
// see: https://cloud.spring.io/spring-cloud-commons/reference/html/#http-clients
@Bean
public HttpClientBuilder proxiedHttpClient() {
String proxyHost = "client-envoy";
Integer proxyPort = 80
String proxyScheme = "http";
return HttpClientBuilder.create()
.setProxy(new HttpHost(proxyHost, proxyPort, proxyScheme));
}
就是这样 - application.yaml
中不需要配置任何其他内容,因为如果 ApacheHttpClient
在类路径中,默认情况下将使用它。
使用 Ok Http 客户端
要使用 OkHttpClient
设置代理,您需要做类似的事情:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
在您的 application.yml
中确保启用 OkHttpClient 并禁用 ApacheHttpClient:
spring:
cloud:
httpclientfactories:
ok:
enabled: true
apache:
enabled: false
feign:
okhttp:
enabled: true
httpclient:
enabled: false
而不是 HttpClientBuilder
公开类型为 OkHttpClient.Builder
的 bean。
Spring cloud feign支持三种底层实现:
- 默认
- Apache HttpClient
- OkHttpClient
如果使用默认值:
创建这个 spring bean(比如通过在 class 内部定义 @Configuration
注释),不需要在应用程序中进行更改 properties/yml:
@Bean
public Client feignClient() {
return new Client.Proxied(
null, null, new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
}
如果使用 Apache HttpClient:
that means you have
feign.httpclient.enabled: true
inapplication.yml
and below in yourpom.xml
orbuild.gradle
:
pom.xml
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
build.gradle
implementation 'io.github.openfeign:feign-httpclient'
创建这个 spring bean(比如通过在 class 中定义 @Configuration
注释):
@Bean
public CloseableHttpClient feignClient() {
return HttpClientBuilder.create().setProxy(new HttpHost(proxyHost, proxyPort)).build();
}
如果使用 OkHttpClient:
that means you have
feign.okhttp.enabled: true
inapplication.yml
and below in yourpom.xml
orbuild.gradle
:
pom.xml
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
build.gradle
implementation 'io.github.openfeign:feign-okhttp'
创建这个 spring bean(比如通过在 class 中定义 @Configuration
注释):
@Bean
public OkHttpClient feignClient() {
return new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.build();
}