question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve Discovery support for Consul

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mirwais27commented, Jun 9, 2016

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! 😃

0reactions
joshistecommented, Jun 10, 2016

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service Discovery Explained | Consul - HashiCorp Developer
Service discovery provides benefits for all organizations, ranging from simplified scalability to improved application resiliency.
Read more >
Discover Services with Consul
Discover Services with Consul · Maintaining service information · Use Consul to discover, track, and monitor services · Mercedes-Benz · Getting Started with...
Read more >
Implementing Service Discovery of Microservices with ...
Service Discovery : Distributed applications can use Consul to dynamically discover service endpoints. Once a service is registered with Consul, ...
Read more >
CAS - Consul Service Discovery - Apereo Blog
Service Discovery : Clients of Consul can provide a service, such as api or mysql, and other clients can use Consul to discover...
Read more >
Discovery - HashiCorp Consul | F5 Distributed Cloud Tech Docs
Create Discovery for Consul Cluster · Click Manage > Service Discoveries . · Click Add Discovery .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found