3

我想在具有身份验证的公司代理后面使用来自外部世界的 REST 服务。

如何配置 Spring Boot + Spring Cloud Feign/Ribbon 以使用我们的代理?

4

3 回答 3

8

我相信你正在寻找这样的东西:

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

于 2017-05-29T15:05:42.393 回答
1

事实证明,实际上有一个更简单的解决方案。

以下信息将有所帮助(也适用于更高级的用例):

OpenFeign Client 可以与多个 HTTP 客户端一起运行。默认情况下它使用java.net.URLConnection,但您也可以使用ApacheHttpClientor OkHttpClient

使用 Apache Http 客户端

您可以使用以下方法设置代理ApacheHttpClient

  1. 将以下两个依赖项添加到您的 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公开一个 bean 类型OkHttpClient.Builder

于 2020-10-01T17:32:53.520 回答
0

Spring Cloud feign 支持三种底层实现:

  1. 默认
  2. Apache HttpClient
  3. OkHttpClient

如果使用默认值:

创建这个spring bean(比如通过使用@Configuration注释定义内部类),应用程序属性/yml不需要更改:

  @Bean
  public Client feignClient() {
    return new Client.Proxied(
        null, null, new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
  }

如果使用 Apache HttpClient:

这意味着您的or中有feign.httpclient.enabled: trueinapplication.yml和 below :pom.xmlbuild.gradle

pom.xml
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>

build.gradle
implementation 'io.github.openfeign:feign-httpclient'

创建这个spring bean(例如通过使用@Configuration注释定义内部类):

  @Bean
  public CloseableHttpClient feignClient() {
    return HttpClientBuilder.create().setProxy(new HttpHost(proxyHost, proxyPort)).build();
  }

如果使用 OkHttpClient:

这意味着您的or中有feign.okhttp.enabled: trueinapplication.yml和 below :pom.xmlbuild.gradle

pom.xml
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

build.gradle
implementation 'io.github.openfeign:feign-okhttp'

创建这个spring bean(例如通过使用@Configuration注释定义内部类):

  @Bean
  public OkHttpClient feignClient() {
    return new OkHttpClient.Builder()
        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
        .build();
  }
于 2021-03-12T01:23:23.243 回答