Java小强个人技术博客站点    手机版
当前位置: 首页 >> 框架 >> SpringCloud启动LN:170 Cannot determine local hostname

SpringCloud启动LN:170 Cannot determine local hostname

106980 框架 | 2022-4-18

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

微信截图_20220418102839.jpg

每次都要写点这东西我也烦透了,但是基本格式还是要遵守的,就加上吧。


一个项目是基于SpringCloud Alibaba的,但是我发现本地启动时总是很长时间才会启动,测试服务器没有发现该问题。查看日志我发现,一直卡在一个输出:

o.s.cloud.commons.util.InetUtils LN:170 Cannot determine local hostname


字面意思就是解析不了hostname,那为什么解析不了,尝试使用SwitchHosts(Hosts切换工具SwitchHosts)来手动补上localhost和主机名对应本地IP,发现问题依然如此。

后来看到有人去设置一个什么inetutils配置,在IDEA上咱们直接点进去看源码


微信截图_20220418102630.jpg


源码如下:


package org.springframework.cloud.commons.util;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Properties for {@link InetUtils}.
 *
 * @author Spencer Gibb
 */
@ConfigurationProperties(InetUtilsProperties.PREFIX)
public class InetUtilsProperties {
	/**
	 * Prefix for the Inet Utils properties.
	 */
	public static final String PREFIX = "spring.cloud.inetutils";

	/**
	 * The default hostname. Used in case of errors.
	 */
	private String defaultHostname = "localhost";

	/**
	 * The default IP address. Used in case of errors.
	 */
	private String defaultIpAddress = "127.0.0.1";

	/**
	 * Timeout, in seconds, for calculating hostname.
	 */
	@Value("${spring.util.timeout.sec:${SPRING_UTIL_TIMEOUT_SEC:1}}")
	private int timeoutSeconds = 1;

	/**
	 * List of Java regular expressions for network interfaces that will be ignored.
	 */
	private List<String> ignoredInterfaces = new ArrayList<>();

	/**
	 * Whether to use only interfaces with site local addresses. See
	 * {@link InetAddress#isSiteLocalAddress()} for more details.
	 */
	private boolean useOnlySiteLocalInterfaces = false;

	/**
	 * List of Java regular expressions for network addresses that will be preferred.
	 */
	private List<String> preferredNetworks = new ArrayList<>();
}


其中一个属性引起我的注意


/**
 * List of Java regular expressions for network interfaces that will be ignored.
 */
private List<String> ignoredInterfaces = new ArrayList<>();


为什么要配置忽略的网卡信息?由于之前做过通信类开发,我们的服务器一般情况下不止一块网卡,一般我们程序为了监听所有网卡会使用0.0.0.0来配置监听IP。而我们个人电脑一般只有一块网卡,怎么出现这种情况?

我这个人不喜欢猜,就喜欢哪怕是笨一点,也要动手检查一下,那就打开网络配置看一下:



微信截图_20220418103258.jpg


谁给你说硬件有一个,网络配置就只有一个的,脑瓜子嗡嗡的,难道SpringCloud连虚拟机配置都不放过吗?

那我就配置上忽略,增加如下配置:


spring:
  cloud:
    inetutils:
      ignored-interfaces: 'VMware Virtual Ethernet Adapter for VMnet1,VMware Virtual Ethernet Adapter for VMnet8'


效果:完美解决,直接启动了。


EDN

推荐您阅读更多有关于“ spring hostname springboot Nacos SpringCloud ”的文章

上一篇:Nacos采坑:非集群Nacos不要使用同一个MySQL数据库 下一篇:Http框架OkHttps

猜你喜欢

发表评论: