这篇文章主要介绍了使用feign配置网络ip代理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
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}<td={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
调用指定ip的feign接口
@FeignClient(value = “center-educational-server”,url=“http://127.0.0.1:10005”)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:使用feign配置网络ip代理
基础教程推荐
猜你喜欢
- Java实现线程插队的示例代码 2022-09-03
- Java实现查找文件和替换文件内容 2023-04-06
- java实现多人聊天系统 2023-05-19
- Java数据结构之对象比较详解 2023-03-07
- Java并发编程进阶之线程控制篇 2023-03-07
- Java文件管理操作的知识点整理 2023-05-19
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- java基础知识之FileInputStream流的使用 2023-08-11
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- springboot自定义starter方法及注解实例 2023-03-31