由浅入深分布式(5)dubbo提供者用内网地址注册provider以及 spring boot admin client用主机名注册spring boot admin server

之前遇到过dubbo提供者用内网地址注册provider的问题 当时改了host文件成功了 但是没有想为什么会有这个问题

现在使用spring boot admin 来监控spring boot项目出现了如下问题, 如果是client和server端 分离,而且不在一台机器上,client会将主机名作为地址注册导致注册失败

要注意服务器之间是可以根据各自的主机名来访问的哦,如果不能访问也应该可以在hosts里设置。。

但是现在遇到了windows下spring boot admin的client注册到linux的server 失败的问题

下面的图显式的是linux的client的注册,道理一样。

由浅入深分布式(5)dubbo提供者用内网地址注册provider以及 spring boot admin client用主机名注册spring boot admin server_第1张图片

直接查spring boot admin clientd 的源代码

/**
 * Scheduler that checks the registration of the application at the spring-boot-admin.
 */
public class SpringBootAdminRegistratorTask implements Runnable {

	private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootAdminRegistratorTask.class);

	@Autowired
	private Environment env;

	@PostConstruct
	public void check() {
		Assert.notNull(env.getProperty("spring.boot.admin.url"),
				"The URL of the spring-boot-admin application is mandatory");
		Assert.notNull(env.getProperty("server.port"), "The server port of the application is mandatory");
		Assert.notNull(env.getProperty("info.id"), "The id of the application is mandatory");
	}

	/**
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try {
			String id = env.getProperty("info.id");
			int port = env.getProperty("server.port", Integer.class);
			String adminUrl = env.getProperty("spring.boot.admin.url");
			RestTemplate template = new RestTemplate();
			template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
			ApplicationList list = template.getForObject(adminUrl + "/api/applications", ApplicationList.class);
			for (Application app : list) {
				if (id.equals(app.getId())) {
					// the application is already registered at the admin tool
					LOGGER.debug("Application already registered with ID '{}'", id);
					return;
				}
			}
			// register the application with the used URL and port
			String url = new URL("http", InetAddress.getLocalHost().getCanonicalHostName(), port, "").toString();
			Application app = new Application();
			app.setId(id);
			app.setUrl(url);
			template.postForObject(adminUrl + "/api/applications", app, String.class);
			LOGGER.info("Application registered itself at the admin application with ID '{}' and URL '{}'", id, url);
		} catch (Exception e) {
			LOGGER.warn("Failed to register application at spring-boot-admin, message={}", e.getMessage());
		}
	}

	private static class ApplicationList extends ArrayList {
		private static final long serialVersionUID = 1L;
	}

}

InetAddress.getLocalHost().getCanonicalHostName() 这行代码有问题,获得的是主机名或者内网ip

修改hosts文件没有用

由浅入深分布式(5)dubbo提供者用内网地址注册provider以及 spring boot admin client用主机名注册spring boot admin server_第2张图片

最后修改注册表由浅入深分布式(5)dubbo提供者用内网地址注册provider以及 spring boot admin client用主机名注册spring boot admin server_第3张图片

PS C:\Users\BAO> hostname
210.82.98.38

OK

你可能感兴趣的:(spring-boot,zookeeper,服务治理,分布式,dubbo)