Reputation: 2694
I have a monolith application A, which needs to invoke a microservice B via service discovery . Consul is the service discovery server used. Microservice B is registered with the Consul server.
From A i am able to invoke B by giving http://hostname:portname/endpoint
How to do this via service discovery.
I tried adding the dependency spring-cloud-dependencies
in Monolith Application A so that i can use org.springframework.cloud.client.discovery.DiscoveryClient
to do the service discovery, but this spring dependency is bringing in embedded tomcat jar which is clashing with my jboss as both run on the default port of 8080
. converting monolith A into a springboot app just for the sake of service discovery isnt an option.
Is there a non spring option to do service discovery from a monolith application to a Consul server?
Upvotes: 1
Views: 353
Reputation: 17803
You can use a Consul aware loadbalancer such as https://traefik.io/ or https://github.com/fabiolb/fabio or a client-side load balancing solution such as https://linkerd.io/
Upvotes: 0
Reputation: 2694
I managed to lookup the consul server from monolith application. Add a dependency in your pom.xml
<!-- https://mvnrepository.com/artifact/com.orbitz.consul/consul-client -->
<dependency>
<groupId>com.orbitz.consul</groupId>
<artifactId>consul-client</artifactId>
<version>0.17.0</version>
</dependency>
The below method will return the hostname,port like http://hostname:port
on which the services are running and as registered on consul
public String serviceUrl(String serviceName) {
String consulRegistryHost = System.getProperty("consul.registry.url");
Consul consul = Consul.builder().withUrl(consulRegistryHost).build(); // connect to Consul on localhost by default , otherwise
HealthClient healthClient = consul.healthClient();
List<ServiceHealth> nodes = healthClient.getAllServiceInstances(serviceName).getResponse(); // discover only "passing" nodes
if (nodes != null && nodes.size() > 0 ) {
ServiceHealth node = nodes.get(0);
return "http://"+node.getService().getAddress()+":"+node.getService().getPort();
}
return null;
}
Upvotes: 0