重写Nacos服务发现逻辑动态修改远程服务IP地址
背景
还是先说下做这个的背景,开发环境上了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地址的更多相关文章
- SpringCloud使用Nacos服务发现实现远程调用
本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...
- SpringBoot使用Nacos服务发现
本文介绍SpringBoot应用使用Nacos服务发现. 上一篇文章介绍了SpringBoot使用Nacos做配置中心,本文介绍SpringBoot使用Nacos做服务发现. 1.Eureka闭源 相 ...
- Oracle LISTENER 主机名修改为IP地址后LISTENER无法监听到实例 oracle监听错误与hosts文件配置
为什么listener.ora文件里面HOST后面到底应该输入IP地址还是主机名.我的经验告诉我,这边最好使用主机名.很多的时候,一个机器绑定的不只一个IP地址,如HOST后面是IP地址,那么ORAC ...
- CentOS 修改固定IP地址
CentOS 修改固定IP地址 参考地址:https://www.cnblogs.com/technology-huangyan/p/9146699.htmlhttps://blog.csdn.net ...
- Shell 脚本修改 Mac IP地址
本篇文章由:http://xinpure.com/shell-script-to-modify-the-mac-ip-address/ 麻烦事 最近在笔记本 WIFI 网络上遇到一个麻烦事, 在公司需 ...
- CentOS 网络设置修改 指定IP地址 DNS 网关(转)
CentOS 网络设置修改 指定IP地址 DNS 网关(实测 笔记) 环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Centos-6.5-x86_64 ...
- Oracle 11g RAC 修改各类IP地址
Oracle 11g RAC 修改各类IP地址 首先,我们都知道Oracle 11g RAC中的IP主要有:Public IP.VIP.SCAN VIP.Private IP这几种. 一般这类改IP地 ...
- C# — 动态获取本地IP地址及可用端口
1.在VS中动态获取本地IP地址,代码如下: 2.获取本机的可用端口以及已使用的端口:
- Win10系统修改电脑IP地址
方法/步骤 1.首先,打开控制面板 2.接着,点开“网络和Internet”,再点开“网络和共享中心” 3.点击"无线网络连接IT4822",可以看到下图 4.然后点击开“属性”, ...
- centos修改静态Ip地址
centos修改静态Ip地址 待办 昨天待办 https://blog.csdn.net/johnnycode/article/details/40624403 centos修改静态ip地址
随机推荐
- 1、MyBatis简介
1.1.MyBatis历史 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁 移到了Google Code. ...
- TCP 粘包
TCP(Transmission Control Protocol,传输控制协议)是一种传输层协议. TCP提供了以下主要功能: 可靠性:TCP使用确认.重传和校验等机制来确保数据的可靠传输.它能够检 ...
- Maven关联本地已有仓库的方法
1. 将本地仓库目录localwarehouse进行压缩为localwarehouse.rar. 2. 将localwarehouse.rar解压到D:\maven文件夹中,然后在setting.xm ...
- 领域驱动设计(DDD):DDD落地问题和一些解决方法
欢迎继续关注本系列文章,下面我们继续讲解下DDD在实战落地时候,会具体碰到哪些问题,以及解决的方式有哪些. DDD 是一种思想,主要知道我们方向,具体如何做,需要我们根据业务场景具体问题具体分析. 充 ...
- HTML一键打包APK工具1.9.5更新,新增一机一码功能
HMTL网址打包APK,可以把本地HTML项目, Egret游戏,网页游戏,或者网站打包为一个安卓应用APK文件,无需编写任何代码,也无需配置安卓开发环境,支持在最新的安卓设备上安装运行. 打包软件会 ...
- Jenkins持续集成入门到精通(进阶篇)
视频参考:https://www.bilibili.com/video/BV1Vp4y1b7ZN?p=51 1. Jenkins+Docker+SpringCloud持续集成流程说明 大致流程说明: ...
- 痞子衡嵌入式:MCUBootUtility v5.3发布,利用XMCD轻松使能外部RAM
-- 痞子衡维护的 NXP-MCUBootUtility 工具距离上一个大版本(v5.0.0)发布过去4个多月了,期间痞子衡也做过三个小版本更新,但不足以单独介绍.这一次痞子衡为大家带来了全新重要版本 ...
- Oracle:字符串的拼接、截取、查找、替换
一.拼接:1.使用"||"来拼接字符串: select '拼接'||'字符串' as Str from dual; 2.使用concat(param1,param2)函数实现: s ...
- 若依(ruoyi)开源系统-多数据源问题踩坑实录
内容概要 上一节内容 介绍了用开源系统若依(ruoyi)搭建页面的过程.在实际项目中,经常遇到多数据源后者主从库的情况.本节记录若依多数据源配置过程中遇到的问题排查过程. 背景描述 1.上一节在r ...
- C#基数排序算法
前言 基数排序是一种非比较性排序算法,它通过将待排序的数据拆分成多个数字位进行排序. 实现原理 首先找出待排序数组中的最大值,并确定排序的位数. 从最低位(个位)开始,按照个位数的大小进行桶排序,将元 ...