使用feign配置網(wǎng)絡(luò)ip代理
feign配置網(wǎng)絡(luò)ip代理
問(wèn)題描述
測(cè)試環(huán)境將需要訪(fǎng)問(wèn)的外網(wǎng)地址加入了白名單,但是docker和宿主機(jī)網(wǎng)絡(luò)不一樣(試過(guò)掛載宿主機(jī)網(wǎng)絡(luò)也不行,但是掛載宿主機(jī)網(wǎng)絡(luò)會(huì)打亂原有的網(wǎng)絡(luò)環(huán)境),所以造成了在宿主機(jī)上面可以訪(fǎng)問(wèn)該地址,但是docker里面是訪(fǎng)問(wèn)不到外網(wǎng)的地址,所使用feign的時(shí)候加上ip代理,代理宿主機(jī)ip來(lái)對(duì)外網(wǎng)地址進(jìn)行訪(fǎng)問(wèn)!
為什么不直接對(duì)docker設(shè)置網(wǎng)絡(luò)代理,測(cè)試環(huán)境里面基本都是內(nèi)部服務(wù)調(diào)用,如果設(shè)置則會(huì)導(dǎo)致其網(wǎng)絡(luò)不一致,并且開(kāi)發(fā)測(cè)試正式環(huán)境較為復(fù)雜,如果不需要的時(shí)候直接在配置文件配置為null就行
1.依賴(lài)
<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相關(guān)依賴(lài) feign-okhttp主要用來(lái)做網(wǎng)絡(luò)代理,依賴(lài)需要自行百度
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遠(yuǎn)程調(diào)用的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代理設(shè)置
?* @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 //需要訪(fǎng)問(wèn)的地址 host 如果多個(gè) 用逗號(hào)分割
不使用IP代理
feign: ?okhttp: ? enabled: true proxy: ?host: null ?port: null ?domains: null
調(diào)用指定ip的feign接口
@FeignClient(value = “center-educational-server”,url=“http://127.0.0.1:10005”)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中ExecutorService創(chuàng)建方法總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java中ExecutorService創(chuàng)建方法總結(jié),有興趣的朋友們可以參考下。2021-01-01
Java并發(fā)線(xiàn)程之線(xiàn)程池的知識(shí)總結(jié)
這篇文章主要介紹了Java并發(fā)線(xiàn)程之線(xiàn)程池的知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)Java并發(fā)線(xiàn)程的相關(guān)內(nèi)容,感興趣的朋友可以了解下2021-01-01
使用Spring初始化加載InitializingBean()方法
這篇文章主要介紹了使用Spring初始化加載InitializingBean()方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
詳解堆排序算法原理及Java版的代碼實(shí)現(xiàn)
如果將堆理解為二叉樹(shù),那么樹(shù)中任一非葉結(jié)點(diǎn)的關(guān)鍵字均不大于(或不小于)其左右孩子(若存在)結(jié)點(diǎn)的關(guān)鍵字,堆排序的時(shí)間復(fù)雜度為O(N*logN),這里我們就來(lái)詳解堆排序算法原理及Java版的代碼實(shí)現(xiàn)2016-06-06
SpringBoot讀取properties中文亂碼解決方案
本文主要介紹了在Spring?Boot中讀取帶有中文字符串的application.properties文件時(shí)遇到亂碼問(wèn)題的解決方案,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
springboot?vue測(cè)試平臺(tái)接口定義前后端新增功能實(shí)現(xiàn)
這篇文章主要介紹了springboot?vue測(cè)試平臺(tái)接口定義前后端新增功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
基于SpringCloudAlibaba+Skywalking的全鏈路監(jiān)控設(shè)計(jì)方案
這篇文章主要介紹了基于SpringCloudAlibaba+Skywalking的全鏈路監(jiān)控設(shè)計(jì)方案,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01

