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

来源:互联网 发布:淘宝收藏宝贝没反应 编辑:程序博客网 时间:2024/06/10 16:32

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

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

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

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

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


直接查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);@Autowiredprivate Environment env;@PostConstructpublic 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() */@Overridepublic 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 toolLOGGER.debug("Application already registered with ID '{}'", id);return;}}// register the application with the used URL and portString 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<Application> {private static final long serialVersionUID = 1L;}}

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

修改hosts文件没有用


最后修改注册表

PS C:\Users\BAO> hostname
210.82.98.38

OK

0 0
原创粉丝点击