nacos多网卡服务器引发的问题
创始人
2024-05-13 03:04:32
0

1、背景

        某一日晚上,公司的一个微服务上线重启后,双节点,只有一个节点注册到了nacos服务器,另外一个节点一直在nacos界面上无法看到,两个节点的服务启动也没有报错,api网关转发给服务时,时不时有告警日志,产生熔断。

下面就来看看,如何会诞生这个问题,如何解决这个问题。

2、问题排查

(1)代码没问题,都是同一份,启动没有报错

(2)nacos服务器没有问题,能够收到一个节点的注册

(3)两个服务器有何不一样?一个机器是单网卡,一个机器是双网卡

(4)寻找spring-cloud-starter-alibaba-nacos-discovery代码中,服务注册到nacos的配置,发现ip属性,顿时悟了

3、spring.cloud.nacos.discovery配置

        打开项目引用的spring-cloud-starter-alibaba-nacos-discovery的源码,我们看看NacosDiscoveryProperties类的属性。

从类中即可看到,加载我们项目yml配置文件中的属性:spring.cloud.nacos.discovery

/*** @author dungu.zpf* @author xiaojing* @author Mercy* @author lyuzb* @author eshun*/
@ConfigurationProperties("spring.cloud.nacos.discovery")
public class NacosDiscoveryProperties {private static final Logger log = LoggerFactory.getLogger(NacosDiscoveryProperties.class);/*** Prefix of {@link NacosDiscoveryProperties}.*/public static final String PREFIX = "spring.cloud.nacos.discovery";private static final Pattern PATTERN = Pattern.compile("-(\\w)");/*** nacos discovery server address.*/private String serverAddr;/*** the nacos authentication username.*/private String username;/*** the nacos authentication password.*/private String password;/*** the domain name of a service, through which the server address can be dynamically* obtained.*/private String endpoint;/*** namespace, separation registry of different environments.*/private String namespace;/*** watch delay,duration to pull new service from nacos server.*/private long watchDelay = 30000;/*** nacos naming log file name.*/private String logName;/*** service name to registry.*/@Value("${spring.cloud.nacos.discovery.service:${spring.application.name:}}")private String service;/*** weight for service instance, the larger the value, the larger the weight.*/private float weight = 1;/*** cluster name for nacos .*/private String clusterName = "DEFAULT";/*** group name for nacos.*/private String group = "DEFAULT_GROUP";/*** naming load from local cache at application start. true is load.*/private String namingLoadCacheAtStart = "false";/*** extra metadata to register.*/private Map metadata = new HashMap<>();/*** if you just want to subscribe, but don't want to register your service, set it to* false.*/private boolean registerEnabled = true;/*** The ip address your want to register for your service instance, needn't to set it* if the auto detect ip works well.*/private String ip;}

 从上面最后的属性ip看到,如果是多网卡的服务器,可以指定ip,这样可以解决不同服务器之间没有构成局域网导致服务之间无法调用的问题

yml配置文件如下:

spring:cloud:nacos:# 配置中心config:server-addr: 10.2.20.178:8848file-extension: txtnamespace: "e55d034a-b71f-4c55-a31a-a085d36e6a80"username: ""password: ""# 注册中心discovery:#指定本服务所在ip,避免多网卡导致服务注册的ip不一致,导致服务之间网络不通ip: 10.2.19.176server-addr: 10.2.20.178:8848namespace: "e55d034a-b71f-4c55-a31a-a085d36e6a80"username: ""password: ""

 

最终,discovery属性下加上ip配置,重启节点服务,刷新nacos节点面板即可看到数据。

4、nacos.config需不需要加IP?

我们一起来看看源码就知道,不需要加,因为里面没有ip的属性。

打开maven引入的spring-cloud-starter-alibaba-nacos-config文件。

class NacosConfigProperties.java中,没有IP的属性,因此不需要配置。

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...