Spring Boot Micro-Services with Eureka and Zuul proxy with Feign Client.

Iroshan Aberathne
6 min readMay 30, 2019

--

I would like to illustrate the spring-boot micro-service implementation with eureka and zuul gateway. Apart from that explain how to configure feign client to make communication among the micro-services. The sample project contains four applications. Note that i’m not going to explain much of theoretical aspect of the title, rather like to discuss the things with code base.

  1. Eureka Server Application

Eureka server or service registry is the application where keep all the micro services detail like a directory.

2.User-Service Application

Micro-service which is responsible to handle the CRUD operations of user details engaging the database layer.

3.Resource-Service Application

Another micro-service which is internally communicate with user-service so that end user can get the user details data from resource-service.

4. Gateway-Service (Zuul proxy) Application.

Gateway-service or zuul proxy is the one that stay at front door in any http request. The end user only see the zuul proxy url and once the user trigger a http request the zuul proxy rout that request the specific micro service in smarter way.

1.Create Eureka server with Spring Initializr( https://start.spring.io/)

Eureka Server Generation

1.1 pom.xml in Eureka Server

Eureka Server pom.xml

1.2 Application main class in Eureka server.

@EnableEurekaServer: Enable the eureka server.

Eureka Main Class

1.3 Eureka server application.yml file.

Line 1->3 :

The assign port of the service or application

Line 4->6 :

The name of the service or application

Line 8->17 :

These configurations are used to register a given micro service with the eureka server. But the eureka server itself no need to register with it. That is why the “registerWithEureka” and ”fetchRegistry” attributes are set to false.

Eureka server application.yml

1.4 Up and running Eureka Server with the configured port in the application.yml file 8661.

Up and Running Eureka Server

2. Create resource-service with spring initialzr adding following dependencies.

  • Eureka Discovery(Cloud Discovery)
  • RestRepositories(Web)
  • JPA(SQL)
  • MySQL(SQL)

Then the pom.xml file should be like this.

User-Service pom.xml

2.1 User-service application main class.

@EnableEurekaClient : Enable registering capabilities as micro service in eureka server.

User-Service application main.class

2.2 User-service application.yml file.

Line 7->18:

These configuration is used to handle the data base connectivity with user name and password.(refer Hikari properties too.)

Line 22-> 23:

The flags set to true in order to register the user-service in eureka server as a micro service.

2.3 User-service project structure

This dummy micro-service has its own database to manage the user data. The layered structure of the project has following components.

  • Controllers : Expose the api end points for CRUD operations.
UserController
  • Services : Handle the business logic
UserService
  • Repositories : Data persist ad retrial to/from data base.
UserRepository
  • Entites: Represent the database table as objects.
UserDetails Entity
  • Dtos : The representation of the entity or set of entities.
UserDetails DTO

3. Create resource-service with spring initialzr adding following dependencies.

  • Eureka Discovery(Cloud Discovery)
  • RestRepositories(Web)
  • Feign(Cloud Routing)

Then the pom.xml should be like this.

Resource-Service pom.xml

3.1 Resource-service application main class.

@EnableFeignClient : Enable communication capabilities among the registered micro-services in eureka server. This dummy micro-service has its own database to manage the user data. The layered structure of the project has following components.

Resource-Service Application Main.class

3.2 Resource-service application.yml file.

Line 10 -> 11 :

The flags set to true in order to register the resource-service in eureka server as a micro service.

3.3 Resource-service project structure

This is the place where feign client comes in to the picture. The resource-service internally communicate with the user-service to get and post data between the micro service. Let’s see how the process go on.

Project structure of resource-service
  • UserResourceController: This has the api end points which are available to the rest of the world. Once a HTTP api call triggers by a user the resource-server communicate with user-service through feign client.
UserResourceController
  • UserServiceFeign: This the place where magic happen.

@FeginClient(name = “user-service”) :

Enable the communication with user-service though fegin client. The name attribute value should be the application name of the user-service mentioned in application.yml.

GET & POST API Mappings

The rest of all the GET and POST mapping should be as same as API end points available in user-service controller. (refer user-service controller). Then the fegin client resolve the end point in smart way.

FeginClient Interface
  • UserDetailsDTO : Should be same as user-service UserDetailsDTO
UserDetailsDTO in resource-service

4. Create gateway-service (Zuul proxy ) with spring initializr adding following dependencies.

  • Eureka Discovery(Cloud Discovery)
  • Zuul(Cloud Routing)

Then the pom.xml file as follows.

Gateway service (Zuul) pom.xml

4.1 Gateway-service (Zuul proxy) main class.

@EnableZuulProxy : Enable the zuul proxy functionalities such as smart routing.

Gateway-Service main class

4.2 Gateway-service application.yml file.

Line 17->30 :

This configuration depicts the configuration of smart routing in multiple micro-services. We refer it as smart routing because end user is not aware about exact ip of each micro service but they only aware the service names. resource-service and user-service are such micro-services.

path :

Refer the api end point url prefix to identify the specific micro-service

eg: http://localhost:8662/resource/api/saveUpdate

Zuul (gateway-service- localhost:8762) smarter enough to rout the HTTP request to resource-service application (micro service)

serviceId :

Should be the application name of each micro service which is already defined at specific application.yml file as “spring.application.name” property.

Gateway-service application.yml

This is the time to test our hard work….

Run all the projects and see whether all services are running…

Eureka dashboard

Test the API end points using postman

  1. SaveUpdate end point
SaveUpdate API end point

2. GetByName API end point

GetByName API Call

How is the database look like ……?????

UserDetails Table

Have a Nice Day !!!!!!!

--

--

Responses (10)