springcloud注册hostname或者ip的那些事
作者:wuqinduo 发布时间:2022-05-06 00:57:37
SpringCloud简介
Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的
微服务:就是把一个单体项目,拆分为多个微服务,每个微服务可以独立技术选型,独立开发,独立部署,独立运维.并且多个服务相互协调,相互配合,最终完成用户的价值.
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署
五大重要组件
服务发现——Netflix Eureka
客服端负载均衡——Netflix Ribbon/Feign
服务网关——Netflix Zuul
断路器——Netflix Hystrix
分布式配置——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.instance.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<NetworkInterface> 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<InetAddress> 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 文件,直接将本机的主机名映射到有效IP地址
总结
以上所述是小编给大家介绍的springcloud注册hostname或者ip的那些事网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://juejin.im/post/5dcb5789f265da4cf50c6ff5?utm_source=tuicool&utm_medium=referral
猜你喜欢
- PowerPoint幻灯片中可插入公式,用于在幻灯片放映时演示相关内容的论证、推算的依据,能有效地为演讲者提供论述的数据支撑。通过后端程序代
- 本文实例讲述了Java链表(Linked List)基本原理与实现方法。分享给大家供大家参考,具体如下:在分析链表之前,我们先来对之前的动态
- 1. String.trim()trim()是去掉首尾空格2.str.replace(" ", ""
- 前言假设项目打包后,项目结构为:此时如果需要再windows环境中进行项目的启动或关闭,需要频繁的手敲命令,很不方便。此时可以编写.bat脚
- 今天在群里看见有人问了这个问题,那就把我自己总结的知识拿出来与大家分享一下吧..当然可能还有什么不对的地方,希望指出:***msbase.j
- 前言当初年少懵懂,那年夏天填志愿选专业,父母听其他长辈说选择计算机专业好。从那以后,我的身上就有了计院深深的烙印。从寝室到机房,从机房到图书
- Spring Boot产生环形注入***************************APPLICATION FAILED TO STAR
- 如何实现封装可以分为两步:第一步:将类的变量声明为private。第二步:提供公共set和get方法来修改和获取变量的值。代码展示publi
- 实现多线程的方式:实现多线程的方式有多种,这里只列举两种常用的,而第一种继承Thread的方式无法实现多窗口卖票。一,继承Thread方式:
- Spring 提供了自动代理机制,可以让容器自动生成代理,从而把开发人员从繁琐的配置中解脱出来 。 具体是使用 BeanPostProces
- 本文实例为大家分享了java中文传值乱码问题,以及解决方法,供大家参考,具体内容如下一般编码格式设置:1.可以经过两次编码处理,即设置字符集
- 1、首先导入solrj需要的的架包2、需要注意的是低版本是solr是使用SolrServer进行URL实例的,5.0之后已经使用SolrCl
- @PropertySource读取配置文件通过@Value参数注入有参数文件如下test.propertiesproject.author=
- A:首先先看下一个简单的面试题斐波那契数列计算数组{1,1,2,3,5,8.......} 第30位值规律:1 1 从第三项开始,每一项都是
- 问题描述:在用fabric集成后编译出现如下错误,Error:Cause: hostname in certificate didn'
- Class.forName(xxx.xx.xx) 返回的是一个类一.首先你要明白在java里面任何class都要装载在虚拟机上才能运行。1.
- public void add(intindex, Eelement)从index索引的位置添加element元素,后面的元素都往
- java通过IP解析地理位置在项目开发中,需要在登录日志或者操作日志中记录客户端ip所在的地理位置。目前根据ip定位地理位置的第三方api有
- 目录1、this代表了()的对象引用,super表示的是当前对象的()对象?2、输出内容是:3、下面程序的输出是:()4、执行下列代码的输出
- 前言对于数组遍历,基本上每个开发者都写过,遍历本身没什么好说的,但是当我们在遍历的过程中,有一些复杂的业务逻辑时,将会发现代码的层级会逐渐加