背景

还是先说下做这个的背景,开发环境上了K8S,所有的微服务都注册在K8S内的Nacos,注册地址为K8S内部虚拟IP,K8S内的服务之间相互调用没有问题,但是本机开发联调调用其他微服务就访问不到。

解决方案

1、KT Connect,可以理解为一个VPN工具,可以和K8S网络联通,缺点是配置繁琐,每次开发都需要启动KT Connect程序;

2、配置Ribbon的listOfServers,配置如下:

<nacosServiceName>.ribbon.listOfServers=<ip:port>

3、Nacos Client从Nacos Server获取服务列表时,修改远程服务对应的IP地址。

重写NacosNamingService

1、看了下nacos-client源码,发现有个NacosNamingService类,主要是服务发现的实现类,可以从这里入手修改远程服务注册IP;

2、NacosNamingService是在NacosDiscoveryClientAutoConfiguration通过注入NacosDiscoveryProperties初始化的,具体源码如下:

@Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosProperties() {
return new NacosDiscoveryProperties();
}

3、跟踪到NacosDiscoveryProperties,初始化NacosNamingService的核心代码如下:

    public NamingService namingServiceInstance() {
if (null != this.namingService) {
return this.namingService;
} else {
Properties properties = new Properties();
properties.put("serverAddr", this.serverAddr);
properties.put("namespace", this.namespace);
properties.put("com.alibaba.nacos.naming.log.filename", this.logName);
if (this.endpoint.contains(":")) {
int index = this.endpoint.indexOf(":");
properties.put("endpoint", this.endpoint.substring(0, index));
properties.put("endpointPort", this.endpoint.substring(index + 1));
} else {
properties.put("endpoint", this.endpoint);
} properties.put("accessKey", this.accessKey);
properties.put("secretKey", this.secretKey);
properties.put("clusterName", this.clusterName);
properties.put("namingLoadCacheAtStart", this.namingLoadCacheAtStart); try {
this.namingService = NacosFactory.createNamingService(properties);
} catch (Exception var3) {
log.error("create naming service error!properties={},e=,", this, var3);
return null;
} return this.namingService;
}
}

4、我们直接重新namingServiceInstance方法就可以了,具体实现代码如下:

@Slf4j
@Configuration
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(
name = {"spring.profiles.active"},
havingValue = "dev"
)
@AutoConfigureBefore({NacosDiscoveryClientAutoConfiguration.class})
public class DevEnvironmentNacosDiscoveryClient { @Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosProperties() {
return new DevEnvironmentNacosDiscoveryProperties();
} static class DevEnvironmentNacosDiscoveryProperties extends NacosDiscoveryProperties { private NamingService namingService; @Override
public NamingService namingServiceInstance() {
if (null != this.namingService) {
return this.namingService;
} else {
Properties properties = new Properties();
properties.put("serverAddr", super.getServerAddr());
properties.put("namespace", super.getNamespace());
properties.put("com.alibaba.nacos.naming.log.filename", super.getLogName());
if (super.getEndpoint().contains(":")) {
int index = super.getEndpoint().indexOf(":");
properties.put("endpoint", super.getEndpoint().substring(0, index));
properties.put("endpointPort", super.getEndpoint().substring(index + 1));
} else {
properties.put("endpoint", super.getEndpoint());
} properties.put("accessKey", super.getAccessKey());
properties.put("secretKey", super.getSecretKey());
properties.put("clusterName", super.getClusterName());
properties.put("namingLoadCacheAtStart", super.getNamingLoadCacheAtStart()); try {
this.namingService = new DevEnvironmentNacosNamingService(properties);
} catch (Exception var3) {
log.error("create naming service error!properties={},e=,", this, var3);
return null;
} return this.namingService;
}
} } static class DevEnvironmentNacosNamingService extends NacosNamingService { public DevEnvironmentNacosNamingService(Properties properties) {
super(properties);
} @Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy) throws NacosException {
List<Instance> instances = super.selectInstances(serviceName, clusters, healthy);
instances.stream().forEach(instance -> instance.setIp("10.101.232.24"));
return instances;
}
} }

重写Nacos服务发现逻辑动态修改远程服务IP地址的更多相关文章

  1. SpringCloud使用Nacos服务发现实现远程调用

    本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...

  2. SpringBoot使用Nacos服务发现

    本文介绍SpringBoot应用使用Nacos服务发现. 上一篇文章介绍了SpringBoot使用Nacos做配置中心,本文介绍SpringBoot使用Nacos做服务发现. 1.Eureka闭源 相 ...

  3. Oracle LISTENER 主机名修改为IP地址后LISTENER无法监听到实例 oracle监听错误与hosts文件配置

    为什么listener.ora文件里面HOST后面到底应该输入IP地址还是主机名.我的经验告诉我,这边最好使用主机名.很多的时候,一个机器绑定的不只一个IP地址,如HOST后面是IP地址,那么ORAC ...

  4. CentOS 修改固定IP地址

    CentOS 修改固定IP地址 参考地址:https://www.cnblogs.com/technology-huangyan/p/9146699.htmlhttps://blog.csdn.net ...

  5. Shell 脚本修改 Mac IP地址

    本篇文章由:http://xinpure.com/shell-script-to-modify-the-mac-ip-address/ 麻烦事 最近在笔记本 WIFI 网络上遇到一个麻烦事, 在公司需 ...

  6. CentOS 网络设置修改 指定IP地址 DNS 网关(转)

    CentOS 网络设置修改 指定IP地址 DNS 网关(实测 笔记)   环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Centos-6.5-x86_64 ...

  7. Oracle 11g RAC 修改各类IP地址

    Oracle 11g RAC 修改各类IP地址 首先,我们都知道Oracle 11g RAC中的IP主要有:Public IP.VIP.SCAN VIP.Private IP这几种. 一般这类改IP地 ...

  8. C# — 动态获取本地IP地址及可用端口

    1.在VS中动态获取本地IP地址,代码如下: 2.获取本机的可用端口以及已使用的端口:

  9. Win10系统修改电脑IP地址

    方法/步骤 1.首先,打开控制面板 2.接着,点开“网络和Internet”,再点开“网络和共享中心” 3.点击"无线网络连接IT4822",可以看到下图 4.然后点击开“属性”, ...

  10. centos修改静态Ip地址

    centos修改静态Ip地址 待办 昨天待办 https://blog.csdn.net/johnnycode/article/details/40624403 centos修改静态ip地址

随机推荐

  1. [ABC148F] Playing Tag on Tree

    2023-03-04 题目 题目传送门 翻译 翻译 难度&重要性(1~10):5 题目来源 AtCoder 题目算法 最短路 解题思路 考虑到 T 想活得久, A 想尽早追上 T ,所以我们就 ...

  2. 十年磨一剑的华为云GES,高明在哪

    本文分享自华为云社区<华为云GES:十年磨一剑,打造业界一流的云原生分布式图数据库>,作者:GES图引擎服务小图 . 1.浅谈云原生图数据库 图数据库(graph database)是一个 ...

  3. 从 DMAIC 方法论说起,记一个长链接 bug 的排查全过程

    引 本文是我在前端团队的第四次分享,在过去我曾做过一次关于 bug 排查的全流程的排查分析,文档:客户线上反馈:从信息搜集到疑难 bug 排查全流程经验分享,但这个文章更侧重我过去几年的排查经验,而非 ...

  4. C盘无法创建文件处理

    icacls c:\ /setintegritylevel M 打开终端执行上方命令,亲测可用

  5. 京东搜索EE链路演进

    导读 搜索系统中容易存在头部效应,中长尾的优质商品较难获得充分的展示机会,如何破除系统的马太效应,提升展示结果的丰富性与多样性,助力中长尾商品成长是电商平台搜索系统的一个重要课题.其中,搜索EE系统在 ...

  6. 2-MySQL基本语法

    上文说到,数据库的安装和连接,接下来将给大家讲解MySQL数据库的基本语法及数据的类型 1.基本语法 (1).查看当前所有数据库 : show databases; (2).创建数据库 create ...

  7. 织梦dede邮箱发信配置教程

    环境要求 主机465端口是开启和放行的 php扩展openssl是开启的 php扩展sockets是开启的 1.QQ邮箱或者163邮箱.126邮箱,开启SMTP服务,拿到授权码,根据自己的来 QQ邮箱 ...

  8. linux常用命令(八) tar 打包、压缩、解包、解压缩

    Linux 常用的压缩与解压缩命令有:tar.gzip.gunzip.bzip2.bunzip2.compress .uncompress. zip. unzip.rar.unrar 等. 首先要弄清 ...

  9. 一个Node.js的小爬虫

    爬虫其实就是对网页内特定id.class.标签内容的提取,多是循环出来的,对我们爬取非常便利. 1.安装node node官网下载安装包安装,后在命令行工具中输入node -v查看node安装的版本. ...

  10. Little Victor and Set 题解

    Little Victor and Set 题目大意 在 \([l,r]\) 中选不超过 \(k\) 个相异的数使得异或和最小,输出方案. 思路分析 分类讨论: 当 \(k=1\) 时: 显然选 \( ...