In previous post, we already create eureka server which serves as a vital component for facilitating seamless communication between services. In this article, we will continue our journey to explore eureka service discovery to develop Eureka Client, we will learn how service will communicate each other. we can harness the power of Eureka’s service discovery capabilities to enhance the scalability and reliability of our distributed systems.
Optimizing Service Communication with Eureka Clients
To establish effective communication between services in a distributed environment, Eureka clients play a crucial role. These clients act as intelligent connectors, enabling services to seamlessly discover and interact with each other. By utilizing Eureka’s service registry, a Eureka client can dynamically locate and invoke services without the need for hard-coded dependencies or manual configuration updates.
Step 1: Setting Up a New Eureka Client
The first step in creating a Eureka client is to set up the necessary dependencies and configurations, we can add some dependency like
spring-boot-starter-web: This dependency includes all the necessary libraries and configurations to develop web applications using Spring MVC. It provides essential components like an embedded web server, HTTP request handling, and servlet support. With this starter, you can quickly set up and start building web applications in Spring Boot.
spring-boot-starter: This starter is a fundamental dependency for all Spring Boot applications. It includes the core components and configurations needed to bootstrap a Spring Boot project. It sets up various features like auto-configuration, logging, and property management. Additionally, it integrates seamlessly with other Spring Boot starters, allowing you to easily add additional functionality to your application.
spring-cloud-starter-netflix-eureka-client: This dependency enables your application to act as a Eureka client and interact with a Eureka server for service discovery. It provides the necessary libraries and configurations to register your application with a Eureka server, discover other services, and communicate with them. By using this starter, you can leverage the power of Eureka’s service registry and enable seamless communication between microservices in your distributed system.
Include dependency management for spring cloud
Full pom.xml
Step 2: Registering the Eureka Client int Eureka Server
After dependency already set on project, we can configure eureka client to registered in eureka server registry by modify application.yml, before it, we should define service name
then , define Eureka server URL and another property to setup preservation
Last, add annotation @SpringBootApplication in main class
Run your service
mvn spring-boot:run
Now your service appear on eureka server
Step 3: Discovering and Communicating with Services
To integrate each service other, we can use feign client, this library is part of spring-cloud dependency. We can do by just create client like previous point and add dependency for feign client
Enable feign client on second client main class using annotation @EnableFeignClients
Now, add controller in first service, we will hit first service through second service via feign client
Create interface in second service to access first controller
We can see annotation @FeignClient(name="first-service"), name indicates we will invoke service called first-service because service already wrapped up by eureka, we only need to define target service name without define url manually, it’s eureka magic
Last, create controller in second service so we can invoke controller from outside
Now run your both service
mvn spring-boot:run
and try hit second service in port 9000 using curl
$ curl http://localhost:9000/hitme
second service will return response from second service, you can find both service at my repository page on github see you next post about how to secure eureka server