springcloud注册hostname或者ip的那些事

网友投稿 230 2023-06-02

springcloud注册hostname或者ip的那些事

SpringCloud简介

Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的

微服务:就是把一个单体项目,拆分为多个微服务,每个微服务可以独立技术选型,独立开发,独立部署,独立运维.并且多个服务相互协调,相互配合,最终完成用户的价值.

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署

五大重要组件

服务发现——Netflix Eureka

客服端负载均衡——Netflix Ribbon/Feign

服务网关——Netflix Zuul

断路器——Netflix Hysthttp://rix

分布式配置——Spring Cloud Config

默认情况下,Eureka 使用 hostname 进行服务注册,以及服务信息的显示, 如果我们相拥 IP 地址的方式,可以在配置文件中配置 eureka.instance.prefer-ip-address=true

idea中ctrl+鼠标左键,点击 eureka.instance.prefer-ip-address=true 进入查看 EurekaInstanceConfigBean 会引入这个属性

EurekaInstanceConfigBean

/**

* Flag to say that, when guessing a hostname, the IP address of the server should be

* used in prference to the hostname reported by the OS.

*/

private boolean preferIpAddress = false;

preferIpAddress: 首选IP地址。 默认false,也就是默认不注册ip.

肯定有地方做了判断,在 EurekaInstanceConfigBean 搜索preferIpAddress,发现了 getHostName 方法, 此方法用于返回得到的hostname或者ip

@Override

public String getHostName(boolean refresh) {

if (refresh && !this.hostInfo.override) {

this.ipAddress = this.hostInfo.getIpAddress();

this.hostname = this.hostInfo.getHostname();

}

return this.preferIpAddress ? this.ipAddress : this.hostname;

}

1.首先会判断: this.hostInfo.override 属性. 此属性在setIpAddress方法里设置。setIpAddress方法对应的是 eureka.instance.ip-address= 这个配置属性。

也就是说: eureka.instance.ip-address 和 eureka.inshttp://tance.prefer-ip-address = true 同时设置是优先取 eureka.instance.ip-address 的配置

public void setIpAddress(String ipAddress) {

this.ipAddress = ipAddress;

this.hostInfo.override = true;

}

2.preferIpAddress为false返回hostname属性,为true返回ipAddress属性 在EurekaInstanceConfigBean搜索hostname 会返现hostname 与ipAddress 可从hostInfo获得;hostInfo从inetUtils.findFirstNonLoopbackHostInfo获得。

public EurekaInstanceConfigBean(InetUtils inetUtils) {

this.inetUtils = inetUtils;

this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo();

this.ipAddress = this.hostInfo.getIpAddress();

this.hostname = this.hostInfo.getHostname();

}

重点就落在了这个InetUtils.findFirstNonLoopbackHostInfo方法上。

public InetAddress findFirstNonLoopbackAddress() {

InetAddress result = null;

try {

// 记录网卡最小索引

int lowest = Integer.MAX_VALUE;

// 获取所有网卡

for (Enumeration nics = NetworkInterface

.getNetworkInterfaces(); nics.hasMoreElements();) {

NetworkInterface ifc = nics.nextElement();

if (ifc.isUp()) {//判断网卡是否工作

log.trace("Testing interface: " + ifc.getDisplayName());

if (ifc.getIndex() < lowest || result == null) {

lowest = ifc.getIndex();

}

else if (result != null) {

continue;

}

// @formatter:off

//网卡不忽略列表中

if (!ignoreInterface(ifc.getDisplayName())) {

for (Enumeration addrs = ifc

.getInetAddresses(); addrs.hasMoreElements();) {

InetAddress address = addrs.nextElement();

if (

address instanceof Inet4Address//是IPV4

&& !address.isLoopbackAddress()//不是回环地址(127.***)

&& isPreferredAddress(address)) {//有推荐网卡,判断是推荐网卡内的ip

log.trace("Found non-loopback interface: "

+ ifc.getDisplayName());

result = address;

}

}

}

// @formatter:on

}

}

}

catch (IOException ex) {

log.error("Cannot get first non-loopback address", ex);

}

if (result != null) {

return result;

}

try {

//都没有找到使用JDK的InetAddress获取

return InetAddress.getLocalHost();

}

catch (UnknownHostException e) {

log.warn("Unable to retrieve localhost");

}

return null;

}

此方法,会获 取所有网卡,取ip地址合理、索引值最小且不在忽略列表的网卡的IP地址 作为结果。如果没有找到合适的IP, 就调用 InetAddress.getLocalHost() 方法。

至此我们来总结下,关于注册的几种灵活配置:

Ip注册: eureka.instance.prefer-ip-address=true

指定IP注册: eureka.instance.ip-address=

忽略网卡: spring.cloud.inetutils.ignored-interfaces[0]

推荐网卡: spring.cloud.inetutils.preferredNetworks[0]

配置本机的host文件:当InetUtils找不到合适ip时,会调用JDK的 InetAddress.getLocalHost() 。该方法会根据本机的hostname解析出对应的ip。所以可以配置本机的hostname和 /etc/hosts 文件http://,直接将本机的主机名映射到有效IP地址

总结

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot如何通过devtools实现热部署
下一篇:Spring 自动装配的二义性实例解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~