Table of Contents
Eureka Naming Server in Microservices
What have we learned so far,
PART 1 – MICROSERVICES INTRODUCTION – In this tutorial, we discussed what microservices architecture is and how its different from monolithic architecture.
PART 2 – SCENARIO TO DEVELOP – In this tutorial, we discussed the scenario to develop.
PART 3 – FEIGN CLIENT – In this tutorial, we discussed how feign client simplifies the rest client consumption.
PART 4 – RIBBON CLIENT – In this tutorial, we discussed how ribbon client handles the client side load balancing.
What is Eureka Naming Server?
Advantages of eureka naming server
1. Create new project with below details
2. Add @EnableEurekaServer into initializer class
package com.onlyfullstack.eurekanamingservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaNamingServiceApplication { public static void main(String[] args) { SpringApplication.run(EurekaNamingServiceApplication.class, args); } }
3. Add below properties into application.properties file of eureka-naming-server project
spring.application.name=eureka-naming-service server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
4. Start the EurekaNamingServiceApplication and open http://localhost:8761 , it will show the registered services with it. As of now we don’t have any service registered with naming service so its showing as No instances available.
5. Add below maven dependency in accounts-service and customer-service.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
6. Add @EnableDiscoveryClient into CustomerServiceApplication and AccountServiceApplication as below
AccountServiceApplication
package com.onlyfullstack.accountservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients @EnableDiscoveryClient public class AccountServiceApplication { public static void main(String[] args) { SpringApplication.run(AccountServiceApplication.class, args); } /* @Bean //Part 2 public RestTemplate restTemplate() { return new RestTemplate(); }*/ }
CustomerServiceApplication
package com.onlyfullstack.customerservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class CustomerServiceApplication { public static void main(String[] args) { SpringApplication.run(CustomerServiceApplication.class, args); } }
7. Add below link to accounts-service and customer-services application.properties files
account–service -> application.properties
Remove or comment the static entries of service.ribbon.listOfServers
server.port=8100 spring.application.name=account-service #Part 4 #customer-service.ribbon.listOfServers=http://localhost:8200,http://localhost:8201 #Part 5 eureka.client.service-url.default-zone=http://localhost:8761/eureka
customer-service -> application.properties
server.port=8200 spring.application.name=customer-service eureka.client.service-url.default-zone=http://localhost:8761/eureka
8. Now start the accounts-service with default port specified in properties file and the start customer-services on 8200 and 8201 ports. We have already discussed on starting same application on two different ports in last tutorial. Please make sure that the eureka-naming-service is up so that rest of the services will register themselves on it.
9. Lets see eureka console after starting all the projects
10. Now with the help of eureka naming server we can dynamically register the services and retrieve the response.
Source Code
Download source code of microservice with spring cloud from below git repository :
Lets go to our next tutorial where we will discuss
Part 6 – Zuul Gateway
– In this tutorial we will understand below topics
– What is Zuul?
– What is the need of Zuul Gateway?
– How to implement the Zuul Gateway in microservices?
PART 6 – MICROSERVICES WITH SPRING CLOUD : ZUUL GATEWAY