1、概述
避免注册中心获取错误的ip地址,我们可以用过以下的配置来进行限制。
2、服务IP配置
(1)配置
下面四个配置选择其一即可。
spring:
cloud:
nacos:
discovery:
# 如果选择固定Ip注册可以配置
ip: 192.168.1.12
# 如果选择固定网卡配置项
networkInterface: wlan0
inetutils:
# 使用正则表达式忽略网卡,具体代码逻辑:interfaceName.matches(regex)
ignored-interfaces: VMware Virtual Ethernet Adapter for VMnet.*
# 使用正则或前缀来匹配ip,具体代码逻辑:hostAddress.matches(regex) || hostAddress.startsWith(regex)
preferred-networks: 192\.168\.1\.(\d+)
ignored-interfaces
的网卡名称可以使用ipconfig/all
查看,networkInterface
的查看不到。preferred-networks: ${discovery.server-ip} # 选择符合前缀的IP作为服务注册IP, discovery.server-ip就是你的公网IP
(2)networkInterface 和 ignored-interfaces 获取
执行下面的代码查看网卡名称,注意 networkInterface
和 ignored-interfaces
配置的名称不一致。
public static void main(String[] args) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
System.out.println(networkInterfaces.toString());
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
// 这里的getDisplayName值即是 ignored-interfaces
System.out.println(networkInterface.getDisplayName());
// 这里的name值即是 networkInterface
System.out.println(networkInterface.getName() + "\n");
}
} catch (SocketException e) {
e.printStackTrace();
}
}
结果:
VMware Virtual Ethernet Adapter for VMnet8
eth0
Intel(R) Wireless-AC 9560 160MHz
wlan0
......
3、Nacos更多配置信息
# 如果选择固定Ip注册可以配置
spring.cloud.nacos.discovery.ip = 10.2.11.11
spring.cloud.nacos.discovery.port = 9090
# 如果选择固定网卡配置项
spring.cloud.nacos.discovery.networkInterface = eth0
# 如果想更丰富的选择,可以使用spring cloud 的工具 InetUtils进行配置
# 具体说明可以自行检索: https://github.com/spring-cloud/spring-cloud-commons/blob/master/docs/src/main/asciidoc/spring-cloud-commons.adoc
spring.cloud.inetutils.default-hostname
spring.cloud.inetutils.default-ip-address
spring.cloud.inetutils.ignored-interfaces[0]=eth0 # 忽略网卡,eth0
spring.cloud.inetutils.ignored-interfaces=eth.* # 忽略网卡,eth.*,正则表达式
spring.cloud.inetutils.preferred-networks=10.34.12 # 选择符合前缀的IP作为服务注册IP
spring.cloud.inetutils.timeout-seconds
spring.cloud.inetutils.use-only-site-local-interfaces
spring.cloud.nacos.discovery.server-addr #Nacos Server 启动监听的ip地址和端口
spring.cloud.nacos.discovery.service #给当前的服务命名
spring.cloud.nacos.discovery.weight #取值范围 1 到 100,数值越大,权重越大
spring.cloud.nacos.discovery.network-interface #当IP未配置时,注册的IP为此网卡所对应的IP地址,如果此项也未配置,则默认取第一块网卡的地址
spring.cloud.nacos.discovery.ip # 优先级最高
spring.cloud.nacos.discovery.port # 默认情况下不用配置,会自动探测
spring.cloud.nacos.discovery.namespace # 常用场景之一是不同环境的注册的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。
spring.cloud.nacos.discovery.access-key # 当要上阿里云时,阿里云上面的一个云账号名
spring.cloud.nacos.discovery.secret-key # 当要上阿里云时,阿里云上面的一个云账号密码
spring.cloud.nacos.discovery.metadata #使用Map格式配置,用户可以根据自己的需要自定义一些和服务相关的元数据信息
spring.cloud.nacos.discovery.log-name # 日志文件名
spring.cloud.nacos.discovery.enpoint # 地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
ribbon.nacos.enabled # 是否集成Ribbon 默认为true
4、参考:
Nacos微服务注册地址为内网IP的解决办法
http://www.wouuz.com/recordshare/notedetail/366.html
Nacos微服务注册地址为内网IP的解决办法
https://blog.csdn.net/zimou5581/article/details/91041239
解决springcloud preferred-networks配置失效问题
https://blog.csdn.net/Q1sjjj/article/details/118707056