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
- Create a new Spring Boot project for the Eureka server:
spring init --dependencies=eureka-server eureka-server
cd eureka-server
- 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
- Run the Eureka server:
mvn spring-boot:run
The Eureka server will be running at http://localhost:8761.
Step 2: Create Microservice 1
- Create a new Spring Boot project for Microservice 1:
spring init --dependencies=web,eureka-client microservice1
cd microservice1
- 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
- Create a simple controller in
src/main/java/com/example/microservice1/Controller.java
:
- Run Microservice 1:
mvn spring-boot:run
Step 3: Create Microservice 2
- Create a new Spring Boot project for Microservice 2:
spring init --dependencies=web,eureka-client microservice2
cd microservice2
- 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
- Create a simple controller in
src/main/java/com/example/microservice2/Controller.java
:
- Run Microservice 2:
mvn spring-boot:run
Step 4: Create Zuul API Gateway
- Create a new Spring Boot project for Zuul:
spring init --dependencies=web,zuul,eureka-client zuul-gateway
cd zuul-gateway
- 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
- Create a Zuul filter for logging in
src/main/java/com/example/zuulgateway/LoggingFilter.java
:
- Register the filter by creating a bean in
src/main/java/com/example/zuulgateway/ZuulGatewayApplication.java
:
- Run Zuul API Gateway:
mvn spring-boot:run
Step 5: Test the Setup
- 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
- Create a pre-filter in
LoggingPreFilter.java
:
- Create a post-filter in
LoggingPostFilter.java
:
- Create an error-filter in
LoggingErrorFilter.java
:
- 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.