1. 程式人生 > >由淺入深分散式(5)dubbo提供者用內網地址註冊provider以及 spring boot admin client用主機名註冊spring boot admin server

由淺入深分散式(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的註冊,道理一樣。


直接查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<Application> {
		private static final long serialVersionUID = 1L;
	}

}

InetAddress.getLocalHost().getCanonicalHostName() 這行程式碼有問題,獲得的是主機名或者內網ip

修改hosts檔案沒有用


最後修改登錄檔

PS C:\Users\BAO> hostname
210.82.98.38

OK