You are currently viewing Building Microservices with Spring Cloud Netflix (Zuul, Eureka)

Building Microservices with Spring Cloud Netflix (Zuul, Eureka)

We’ll create two simple microservices and use Zuul as an API gateway to route requests, and Eureka for service discovery. I’ll also demonstrate how to implement pre-filters, post-filters, and error-filters in Zuul.

Prerequisites:

  • JDK 8 or later installed
  • Maven installed
  • Basic knowledge of Spring Boot

Step 1: Create Eureka Server

  1. Create a new Spring Boot project for the Eureka server:
spring init --dependencies=eureka-server eureka-server
cd eureka-server
  1. Open src/main/resources/application.properties and add:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
  1. Run the Eureka server:
mvn spring-boot:run

The Eureka server will be running at http://localhost:8761.

Step 2: Create Microservice 1

  1. Create a new Spring Boot project for Microservice 1:
spring init --dependencies=web,eureka-client microservice1
cd microservice1
  1. Open src/main/resources/application.properties and add:
spring.application.name=microservice1
server.port=8081
eureka.client.service-url.default-zone=http://localhost:8761/eureka
  1. Create a simple controller in src/main/java/com/example/microservice1/Controller.java:
  1. Run Microservice 1:
mvn spring-boot:run

Step 3: Create Microservice 2

  1. Create a new Spring Boot project for Microservice 2:
spring init --dependencies=web,eureka-client microservice2
cd microservice2
  1. Open src/main/resources/application.properties and add:
spring.application.name=microservice2
server.port=8082
eureka.client.service-url.default-zone=http://localhost:8761/eureka
  1. Create a simple controller in src/main/java/com/example/microservice2/Controller.java:
  1. Run Microservice 2:
mvn spring-boot:run

Step 4: Create Zuul API Gateway

  1. Create a new Spring Boot project for Zuul:
spring init --dependencies=web,zuul,eureka-client zuul-gateway
cd zuul-gateway
  1. Open src/main/resources/application.properties and add:
spring.application.name=zuul-gateway
server.port=8080
eureka.client.service-url.default-zone=http://localhost:8761/eureka
  1. Create a Zuul filter for logging in src/main/java/com/example/zuulgateway/LoggingFilter.java:
  1. Register the filter by creating a bean in src/main/java/com/example/zuulgateway/ZuulGatewayApplication.java:
  1. Run Zuul API Gateway:
mvn spring-boot:run

Step 5: Test the Setup

  1. Open your browser or use a tool like curl to make requests:
  • http://localhost:8080/microservice1/hello
  • http://localhost:8080/microservice2/hello

You should see the responses from Microservice 1 and Microservice 2. Zuul is now acting as a gateway, routing requests to the appropriate microservice.

Step 6: Implementing Pre-filters, Post-filters, and Error-filters

  1. Create a pre-filter in LoggingPreFilter.java:
  1. Create a post-filter in LoggingPostFilter.java:
  1. Create an error-filter in LoggingErrorFilter.java:
  1. Register these filters in ZuulGatewayApplication.java:

Now, when you make requests to the microservices, you should see log entries for pre-filters, post-filters, and error-filters in the Zuul API Gateway logs.

Congratulations! You have successfully set up a simple microservices architecture using Spring Cloud Netflix components.

Leave a Reply