What is Service Discovery?
Sevice Discovery is the process of automatically detecting devices and services on a network, we will be using Eureka Server for this which detects and keep track of its registered clients.
If a microservice needs to talk to another microservice it sends a service discovery request to the eureka server and the server returns where the request will be routed.We will be completing the project from the previous blog post.
- Create a new maven module in your previous project and name it "eureka server"
- Open your main pom.xml file and add the following dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
- Open your eureka-server module pom.xml and the following dependancy
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- Create an application.properties file under resources and the following configuration.
spring.application.name=eurekaServer
server.port=8461
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
- Create the EurekaServerApplication class
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
The @EnableEurekaServer annotation is used to make your Spring Boot application acts as a Eureka Server
- Run the application class and open the server on the port specified just to make sure everthing is working fine as you can see there are no registered clients currenctly in eureka.
- Now lets register the clients on the eureka server
- Open your needed microservice in our case its the util microservices.
- Open the util project pom.xml and add the following dependancy.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- Open up the util application class and add the @EnableEurekaClient annotation
@SpringBootApplication
@EnableEurekaClient
public class UtilApplication {
public static void main(String[] args){
SpringApplication.run(UtilApplication.class,args);
}
}
@EnableEurekaClient annotation allows the project to be registered at the eureka server as a client.
- Add the following property in your application.properties file
#Properties
euraka.client.service-url.defaultZone=http://localhost:8461/eureka
- Start the util application and view the changes on the eureka server portal.
- Notice the util application is now registered as a client on the eureka server.
- Repeat the steps on your customer microservice
- open your refrenced util url in the application.properties file and change the following.
notification.service.url=http://IP:Port/api/v1/email/send
to
notification.service.url=http://UTIL/api/v1/email/send
- Open your customer config class where the rest template used for sending the service is defined.
- Add the annotation @LoadBalanced
The @LoadBalanced annotation will make an instance of created RestTemplate load-balanced. There is no code you need to write to make RestTemplate load-balance HTTP request it sends to an internal microservice. The RestTemplate bean will be intercepted and auto-configured by Spring Cloud
- Run the application and test the customer microservice using a rest client.
- The service should run normally.
You can find full project here.
No comments:
Post a Comment