ApplicationContext context = event.getApplicationContext();
ConsulProperties consulProperties = context.getBean(ConsulProperties.class);
if (!consulProperties.isEnabled())
return;
ApplicationProperties appProps = context.getBean(ApplicationProperties.class);
ServerProperties serverProperties = context.getBean(ServerProperties.class);
AgentClient agentClient = context.getBean(AgentClient.class);
Service service = new Service();
service.setName(appProps.getId());
Integer port = serverProperties.getPort();
if (port == null) {
port = 8080;
}
service.setPort(port);
service.setTags(consulProperties.getTags());
//TODO: add support for Check
register(agentClient, service);
String managementPort = context.getEnvironment().getProperty("management.port", (String) null);
if (managementPort != null) {
Service management = new Service();
management.setName(appProps.getId() + "/management"); //TODO: configurable management suffix
management.setPort(Integer.parseInt(managementPort));
List<String> tags = new ArrayList<>(consulProperties.getTags());
tags.add("management"); //TODO: configurable management tag
management.setTags(tags);
register(agentClient, management);
}
if (!appProps.getRoutes().isEmpty()) {
try {
KVClient kvClient = context.getBean(KVClient.class);
String key = String.format("routing/%s", appProps.getId());
//TODO: get routes from jax-rs and spring mvc
kvClient.put(key, appProps.getRoutes());
} catch (Exception e) {
LOGGER.error("Error writing routes for app: " + appProps.getId(), e);
}
}
}