背景

还是先说下做这个的背景,开发环境上了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. 【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器

    问题描述 在App Service for linux上创建一个PHP应用,通过 phpinfo() 查看PHP的扩展设置,发现JIT没有被开启, jit_buffer_size 大小为0. 那么,在 ...

  2. 《CTFshow-Web入门》06. Web 51~60

    @ 目录 web51 题解 web52 题解 原理 web53 题解 原理 web54 题解 原理 web55 题解 原理 web56 题解 原理 web57 题解 原理 web58 题解 原理 we ...

  3. 搭建LNMP 架构

    搭建LNMP 架构 环境准备 lnmp 需要 安装 nginx mysql php 软件 #关闭防火墙 systemctl disable --now firewalld #临时禁用SELinux的强 ...

  4. nmcli 命令设置网络

    nmcli 命令设置网络 设置静态 IP 地址 sudo nmcli connection modify "连接名称" ipv4.addresses IP地址/子网掩码 设置网关 ...

  5. 【Python爬虫实战】爬虫封你ip就不会了?ip代理池安排上

    前言 在进行网络爬取时,使用代理是经常遇到的问题.由于某些网站的限制,我们可能会被封禁或者频繁访问时会遇到访问速度变慢等问题.因此,我们需要使用代理池来避免这些问题.本文将为大家介绍如何使用IP代理池 ...

  6. ​python爬虫——爬虫伪装和反“反爬”

    前言爬虫伪装和反"反爬"是在爬虫领域中非常重要的话题.伪装可以让你的爬虫看起来更像普通的浏览器或者应用程序,从而减少被服务器封禁的风险:反"反爬"则是应对服务器 ...

  7. 宏观上理解blazor中的表单验证

    概述 表单验证的最终效果大家都懂,这里不阐述了,主要从宏观角度说说blazor中表单验证框架涉及到的类,以及它们是如何协作的,看完这个,再看官方文档也许能更轻松点. blazor中的验证框架分为两部分 ...

  8. 分布式与微服务——Iaas,Paas和Saas、单体应用和缺点、微服务概念、传统 分布式 SOA 架构与微服务架构的区别、微服务实战、什么是RPC、CAP定理和BASE理论、唯一ID生成、实现分布式

    文章目录 1-什么是Iaas,Paas和Saas 一 IaaS基础设施服务 二 paas平台即服务 三saas软件即服务 四 总结 2-单体应用和缺点 一 单体应用 二 单体应用的缺陷 3-微服务概念 ...

  9. TTS背后的技术原理——前端和后端系统

        就解锁了一个温柔又风趣的「女朋友」萨曼萨.不过,在现实生活中,和语音助手谈恋爱还是一件十分遥远的事情--刨去现阶段的语音助手们双商水平还有限,语音助手的语言表达能力还远远达不到我们理想状态. ...

  10. 造轮子之asp.net core identity

    在前面我们完成了应用最基础的功能支持以及数据库配置,接下来就是我们的用户角色登录等功能了,在asp.net core中原生Identity可以让我们快速完成这个功能的开发,在.NET8中,asp.ne ...