spring-boot-admin icon indicating copy to clipboard operation
spring-boot-admin copied to clipboard

Improve Discovery support for Consul

Open mirwais27 opened this issue 10 years ago • 13 comments

I use consul for service-discovery and my services has a context-path:

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.vattenfall.aot.mcf.core")
@EnableDiscoveryClient
@EnableHystrix
@ImportResource({"classpath:foo-service-spring.xml"})
public class McfFooServiceSpringBoot {

    protected static void start(String[] args) {
        SpringApplication.run(McfFooServiceSpringBoot .class, args);
    }

}

My service property:

spring.application.name=foo-service
server.port=10274
server.context-path=/bar/foo-service

spring.cloud.consul.discovery.healthCheckPath=${server.context-path}/health
spring.cloud.consul.discovery.healthCheckInterval=10s

If I start Spring-Boot-Admin like this:

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableDiscoveryClient
public class AdminDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminDemoApplication.class, args);
    }
}

and a property file like this:

spring.application.name=admin
server.port=9999

Then a wrong health-endpoint for the foo service is shown in spring boot admin UI page. And the service is shown as offline:

http://localhost:10274/health

instead of

http://localhost:10274/bar/foo-service//health

The properties spring.boot.admin.discovery.converter.* seems to has no effect. I guess the DefaultServiceInstanceConverter.convert method ignores context-path details.

mirwais27 avatar May 30 '16 07:05 mirwais27

You should implement your own ServiceInstanceConverter for Consul and add it to your admin server configuration. If it's working well, a PR is appreciated. I didn't take a look for discovery with Consul, yet.

joshiste avatar May 30 '16 08:05 joshiste

Thanks. I can create a new ConsulServiceInstanceConverter and create a PR. But how to configure it then for spring-boot-admin?

mirwais27 avatar May 30 '16 08:05 mirwais27

Just include the bean in your Configuration class, e.g.:

@SpringBootApplication
@Configuration
@EnableAdminServer
@EnableDiscoveryClient
public class AdminDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminDemoApplication.class, args);
    }
   @Bean
   public ConsulServiceInstanceConverter() {
      return new ConsulServiceInstanceConverter();
   }
}

FYI: Using @SpringBootApplication and @EnableAutoConfiguration is redundant since the former includes the latter.

joshiste avatar May 30 '16 13:05 joshiste

Adding ConsulServiceInstanceConverter the way you describe is not working. I always land in DefaultServiceInstanceConverter.convert method if I'm debugging. Could this behaviour caused by the 'hardcoded' DefaultServiceInstanceConverter creation in the ApplicationDiscoveryListener:

public class ApplicationDiscoveryListener {

...
    private ServiceInstanceConverter converter = new DefaultServiceInstanceConverter();
...

mirwais27 avatar Jun 01 '16 12:06 mirwais27

@joshiste @mirwais27 If I recall correctly, the DefaultServiceInstanceConverter is created and marked with @ConditionalOnMissingBean but this would be conditional on a missing bean of type DefaultServiceInstanceConverter. The method signature should probably return ServiceInstanceConverter so that any bean of type ServiceInstanceConverter would override the default one. A work-around to that would be to have ConsulServiceInstanceConverter extend DefaultServiceInstanceConverter which should then cause the bean in the auto-configuration to not be created.

steve-oakey avatar Jun 01 '16 13:06 steve-oakey

@steve-oakey your right there a subtle bug in the autoconfig. I've filed #209 for this issue

joshiste avatar Jun 01 '16 16:06 joshiste

@mirwais27 could you try a 1.3.4-SNAPSHOT?

joshiste avatar Jun 01 '16 17:06 joshiste

Unfortunately the Consul API doesn't expose the health-url. The best way would be to improve the DefaultServiceInstanceConverter and use information from the ServiceInstances metadata. This way we can have different settings per application.

BTW: I've checked that the spring.boot.admin.discovery.converter.management-context-path also works when set in the admin server - but doesn't allow a per app setting.

joshiste avatar Jun 07 '16 21:06 joshiste

Additionally we have to introduce an option to ignore certain serviceIds. Otherwise the consul agent itself is shown in the application list.

joshiste avatar Jun 07 '16 21:06 joshiste

As a workaround I got it also running with 1.3.3:

This is my new Consul-Converter considering the context-path of the services:

/**
 * New Converter to let Consul talk with Spring Boot Admin - even with a context-path.
 */
public class ConsulServiceInstanceConverter extends DefaultServiceInstanceConverter {

    public static final String CONTEXT_PATH = "contextPath";

    private String managementContextPath = "";
    private String healthEndpointPath = "health";

    @Override
    public Application convert(ServiceInstance instance) {
        String serviceUrl = instance.getUri().toString();
        String contextPath = instance.getMetadata().get(CONTEXT_PATH);
        serviceUrl = append(serviceUrl, contextPath);

        String managementUrl = append(serviceUrl, managementContextPath);
        String healthUrl = append(managementUrl, healthEndpointPath);

        return Application.create(instance.getServiceId()).withHealthUrl(healthUrl)
                .withManagementUrl(managementUrl).withServiceUrl(serviceUrl).build();
    }

    @Override
    public void setManagementContextPath(String managementContextPath) {
        this.managementContextPath = managementContextPath;
    }

    @Override
    public void setHealthEndpointPath(String healthEndpointPath) {
        this.healthEndpointPath = healthEndpointPath;
    }
}

And with that I enable the converter:

@SpringBootApplication
@Configuration
@EnableAdminServer
@EnableDiscoveryClient
public class MySpringBootAdminApp {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootAdminApp.class, args);
    }

    @Bean
    public DefaultServiceInstanceConverter serviceInstanceConverter() {
        ConsulServiceInstanceConverter consulServiceInstanceConverter = new ConsulServiceInstanceConverter();
        return consulServiceInstanceConverter;
    }
}

BTW: I like the fix #205 to ignoring the consul agent itself. Good job! :-)

mirwais27 avatar Jun 09 '16 06:06 mirwais27

@mirwais27 I suggest you to use the same metadata key as I do with 1.3.4. This way you don't need to reconfigure all the clients when upgrading your s-b-a server to 1.3.4....

joshiste avatar Jun 09 '16 08:06 joshiste

@joshiste Ok, I will do so. When is release date for 1.3.4?

mirwais27 avatar Jun 10 '16 09:06 mirwais27

@mirwais27 not scheduled yet... but I think roughly in 2 weeks...

joshiste avatar Jun 10 '16 12:06 joshiste