When you're working in a microservices architecture, you can have dozens or hundreds of services, each owning its own slice of logic. Statically configuring those services in an API Gateway is like trying to line up a queue of cats by hand: nobody guarantees a service won't change address tomorrow or that a new one won't pop up.
Integrating an API Gateway with Eureka lets you:
- Automatically discover microservices: Eureka handles service registration and heartbeat monitoring. The API Gateway will query that info in real time.
- Dynamically route requests: new services or changes in existing ones are picked up automatically in the routes.
- Make microservice management easier: less manual drudgery — more automation.
Preparation for integration
Before you start, make sure you have:
- A running Eureka Server (we configured this in the previous lecture "Practice: setting up Eureka for microservice discovery").
- One or more client microservices registered in Eureka (for example,
user-serviceandorder-service).
Now let's create a project for the API Gateway and wire it to the Eureka Server.
1. Creating the API Gateway project
1. Project initialization Use Spring Initializr to create a new Spring Boot app:
- Dependencies:
- Spring Cloud Gateway
- Eureka Discovery Client
- Spring Boot Actuator (for monitoring — optional but useful)
Download the project and open it in your favorite IDE (for example, IntelliJ IDEA).
2. Configuring application.yml
Add the basic config to your application.yml to connect to the Eureka Server:
server:
port: 8080 # API Gateway port
spring:
application:
name: api-gateway # Service name in Eureka
cloud:
gateway:
discovery:
locator:
enabled: true # Enable integration with Eureka
routes:
- id: default_route
uri: lb://placeholder # Temporary placeholder until automatic routing kicks in.
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ # Link to your Eureka Server
fetch-registry: true
register-with-eureka: true
Note the key parts of this configuration:
spring.cloud.gateway.discovery.locator.enabled: true— turns on the Gateway's integration with Eureka.lb://— tells Spring to use Ribbon (the load balancer) for routing.
3. Enabling automatic routing
Now that discovery.locator.enabled is turned on, the API Gateway will automatically read registered services from Eureka. Each service in Eureka becomes addressable by its name. For example:
lb://user-servicewill route requests touser-service.lb://order-servicewill route toorder-service.
To verify this in practice, let's add a couple of test routes manually:
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/users/**
- id: order-service
uri: lb://order-service
predicates:
- Path=/orders/**
These routes tell the Gateway to send:
- All requests starting with
/users/touser-service. - All requests starting with
/orders/toorder-service.
4. Running the API Gateway and testing
Start the API Gateway and test the routes. Make sure your user-service and order-service have registered with the Eureka Server and are up.
Testing with *Postman or curl*
- Request:
Expected result: the request will be forwarded to
curl -X GET http://localhost:8080/users/alluser-serviceand you'll get a list of all users (or an error if the service is misconfigured). - Request:
Expected result: the request will be forwarded to
curl -X GET http://localhost:8080/ordersorder-service.
If everything works, the integration was successful!
5. Debugging and monitoring
For debugging the integration you can use Spring Boot Actuator. Add the following to application.yml:
management:
endpoints:
web:
exposure:
include: "*"
Now you can check /actuator/gateway/routes to get a list of all routes in the API Gateway that were discovered via Eureka.
Example command to check:
curl http://localhost:8080/actuator/gateway/routes
6. Advanced configuration
You can also add filters to manipulate requests. For example, to add a header to all requests:
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/users/**
filters:
- AddRequestHeader=X-Service, UserService
Now any request going to user-service will automatically include the header X-Service: UserService.
To protect services from overload, you can set up rate limiting:
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/users/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10 # requests per second
redis-rate-limiter.burstCapacity: 20
Common issues and fixes
- Eureka Server is unreachable: if the API Gateway can't connect to Eureka, make sure the Eureka Server is running and its address is correct in
application.yml. - Microservice not registering: check that the
application.ymlof your microservices is configured correctly for registering with Eureka. - Routing errors: inspect the routes via
/actuator/gateway/routesand make sure theuriis correct.
Congrats! Your API Gateway just got smarter thanks to Eureka. You've not only simplified request routing but also made the system more resilient to changes in microservices. We'll keep building your skills in the upcoming lectures — see you then!
GO TO FULL VERSION