Implementing Dynamic Routing with Spring Cloud Gateway and Nacos
Maven Module Configuration
Initiate a new Maven JAR module within your existing microservices parent project. Configure the pom.xml to include the necessary dependencies for Spring Cloud Gateway, Nacos service discovery, and Nacos configuration management.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example.platform</groupId>
<artifactId>microservices-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>edge-gateway</artifactId>
<dependencies>
<!-- Nacos Service Discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Nacos Configuration Center -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- Spring Cloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Bootstrap Context -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- Client Side Load Balancing -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
Bootstrap Configuration
Create a bootstrap.yml file in the src/main/resources directory. This file connects the application to the Nacos server and defines the basic application properties.
server:
port: 9090
spring:
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
Upon startup, the application attempts to fetch configuration files from Nacos following a specific naming pattern: ${spring.application.name}, ${spring.application.name}.${file-extension}, and ${spring.application.name}-${profile}.${file-extension}.
Dynamic Route Definition
Access the Nacos console and create a new configuration file named api-gateway.yaml. This configuration defines the routing rules, directing traffic based on path predicates to specific microservices using the load balancer protocol.
spring:
cloud:
gateway:
discovery:
locator:
lowerCaseServiceId: true
routes:
- id: user-service-route
uri: lb://user-service
predicates:
- Path=/api/users/**
- id: inventory-service-route
uri: lb://inventory-service
predicates:
- Path=/api/inventory/**
In this configuration, requests matching the pattern /api/users/** are forwarded to the service registered as user-service in Nacos. The lb:// prefix ensures that Spring Cloud LoadBalancer distributes the traffic across available instances.
Verification
Restart the gateway application to apply the dynamic configuration changes. Send an HTTP request to the gateway endpoint to verify the routing logic.
curl http://localhost:9090/api/users/profile?id=42
If the configuration is correct, the gateway successfully proxies the request to an available instance of the user-service, returning the appropriate response.