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}&lttd={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
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值