This is just a follow-up post, the answer above is still valid. Today I had the same issue, my Spring Boot application didn't register itself to Consul while running on an external Tomcat Server. Although one could come up with a working solution based on all information mentioned in the posts above, I have provided all information here, in one post.
I did have to change one thing in the solution code, the annotation @AutoConfigurationAfter(...)
was used. I had to change it to @AutoConfigureAfter(...)
Solution
Add following MyConsulLifecycle
to your application, based on solution code:
@Configuration
@ConditionalOnConsulEnabled
@ConditionalOnMissingBean(type= "org.springframework.cloud.consul.discovery.ConsulLifecycle")
@AutoConfigureAfter(ConsulAutoServiceRegistrationAutoConfiguration.class)
public class MyConsulLifecycle implements ApplicationContextAware {
private ConsulAutoServiceRegistration registration;
public MyConsulLifecycle(ConsulAutoServiceRegistration registration) {
this.registration = registration;
}
public void setApplicationContext(ApplicationContext context) throws BeansException {
if (registration != null ) {
registration.start();
}
}
}
As opposed to Brian Peterson's solution, the method setPort()
is no longer available. This was already mentioned by Sagar Veeram in the comments on his post.
As spencergibb said, this is solved by setting the spring.cloud.consul.discovery.port=${server.port}
in the application.properties
file:
server.port=8080
spring.cloud.consul.discovery.port=${server.port}
Note that is is a bit strange to have a server.port
property required when using a standalone Tomcat Server.
setPort
method in theConsulAutoServiceRegistration
that we used to set the port as mentioned in the solution here github.com/spring-cloud/spring-cloud-consul/issues/…. It has been replaced with setPortIfNeeded but that is pacakge protected. Let me know if you need more info. – s7vrWebServerInitializedEvent
instead of springContextRefreshedEvent
event. – s7vrWebServerInitializedEvent
if using a random port. – spencergibbspring.cloud.consul.discovery.port=${server.port}
and don't need thesetPort()
method. – spencergibb