Apache Camel: Service Discovery with Spring Cloud Netflix

Daniel S. Blanco
4 min readDec 12, 2020

--

We have already seen enough of Apache Camel, and today we will see how to make Service Discovery through the Spring Cloud Netflix library. But first, as always, we will look at various concepts to better understand the example.

We must place ourselves in an environment of creation and use of Microservices. And understand that they are deployed through containers in the cloud mainly. So, their network locations (IPs and addresses) can be dynamic. And therefore it can happen that the configuration we have over their location not always is valid.

Hence the need for Service Discovery, or what is the same, automatic detection of devices and services offered on the same network. In this way, we will always have the correct configuration of the available Microservices.

To do this, we need a Service Registry, which will be a server responsible for collecting information about the Microservices: their location, status, etc. And the Microservices will act as the clients of this Service Registry. There are different Service Registry implementations and we will use Netflix Eureka.

Several patterns can be used to carry out Service Discovery, and we will choose the Client-Side Discovery Pattern. This is where the client will be responsible for registering itself in the Service Registry.

Once we have established the base, we can start with our example. This consists of 3 applications:

  • A Service Registry made only with Spring Boot.
  • A Service Client made with Spring Boot, which will have a resource that will return values to us.
  • A Service Client made with Apache Camel, which will connect to the Service Registry and the other Service Client dynamically.

Let’s start with the Service Registry. This will be the simplest part, basically a Spring Boot project with the dependency spring-cloud-starter-netflix-eureka-server and the annotation @EnableEurekaServer.

However, as it is a local proof of concept and with only one Service Registry, we will have to modify the application’s configuration file to avoid errors at the start and excessive traces. And this is due to the fact that the Service Registry is prepared to work in a cluster and therefore when it starts up it tries to search for other instances and register in them. Therefore, the configuration file will be as follows:

server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

The next step will be to create a client with Spring Boot that connects to the Service Registry and has a resource called /books that returns information. This application will be called discovery-client.

For last, we will now proceed to create our Microservice with Apache Camel, which we will connect to the Service Registry and with which we will also invoke the discovery-client Microservice. In order to carry out all this, we will use the following Spring Boot and Spring Cloud libraries. The rest will be the usual Apache Camel libraries.

<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-cloud-netflix-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>

At the configuration level, we will have to indicate what the name of our application will be (necessary for registering on the Eureka server) and where the Service Registry is located. Remember that we are going to use the Client-Side Discovery pattern, so it must be the client that lets the server know that it is available and where.

spring.application.name=app-camel-eureka-client
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka/

If we already start our application and access the Eureka server at http://localhost:8761. We will be able to see that both Microservices are available and apart from that some information about the server itself.

With this, we can already check that our infrastructure works, and thanks to the Service Registry we can locate our Microservices at any time. Now we are going to see how we can invoke it dynamically from Apache Camel, and we are going to see it in two different ways.

To perform the first dynamic invocation we are going to use the Apache Camel Service Call component. Through this component, we will be able to invoke remote services that are registered in a Service Registry. We will have to indicate the name of the service and its resource in this case discovery-client and books.

In this way we do not have to configure the URL of the Microservice to invoked, we only need to know its identifier in the Service Registry.

We can also make this call through an instance of DiscoveryClient, a Spring Boot utility. Given an identifier of a Microservice, it will indicate its URI and other values associated with it.

As you can always see, with Apache Camel, Spring Boot, and all the starters we can do great things easily. If you want you can see all the code here.

--

--