Spring Boot Micro-Services with Eureka and Zuul proxy with Feign Client.
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.
- 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/)
1.1 pom.xml in Eureka Server
1.2 Application main class in Eureka server.
@EnableEurekaServer: Enable the eureka server.
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.
1.4 Up and running Eureka Server with the configured port in the application.yml file 8661.
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.
2.1 User-service application main class.
@EnableEurekaClient : Enable registering capabilities as micro service in eureka server.
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.
- Services : Handle the business logic
- Repositories : Data persist ad retrial to/from data base.
- Entites: Represent the database table as objects.
- Dtos : The representation of the entity or set of entities.
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.
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.
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.
- 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.
- 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.
- UserDetailsDTO : Should be same as user-service UserDetailsDTO
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.
4.1 Gateway-service (Zuul proxy) main class.
@EnableZuulProxy : Enable the zuul proxy functionalities such as smart routing.
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.
This is the time to test our hard work….
Run all the projects and see whether all services are running…
Test the API end points using postman
- SaveUpdate end point
2. GetByName API end point
How is the database look like ……?????
Have a Nice Day !!!!!!!