The background,

Recently, I have been learning about Spring Cloud Gateway, and our routing configuration is written in the configuration file by default, so when we have a new service access, we need to modify the configuration file, and then restart the Gateway application. So can we dynamically refresh the routing information without stopping the Gateway?

Second, solutions

As we know, NaCos enables dynamic refreshes of configuration and service discovery. So if we put the Spring Cloud Gateway configuration on NACOS, can we achieve dynamic refresh routing?

It was found through testing that it could be done. Here we use Spring Cloud Alibaba technology to achieve.

1. The registration and discovery of the service is realized by Spring Cloud Alibaba Nacos. 2. The Gateway is implemented using the Spring Cloud Gateway.

Conclusion: In fact, only the Spring Cloud Alibaba Nacos Config needs to be integrated in the Spring Cloud Gateway, and the routing configuration of the Gateway can be refreshed automatically without additional coding.

Third, realize the function

1. Provide a product service, and provide an interface of FindAllProduct to return product information. 2, implement a gateway service, access to external services, are first through the gateway program, and then call to the specific service. 3. Routing configuration information of the gateway service needs to be dynamically configured.

IV. Implementation steps

1. Implementation of gateway services

1. POM file


<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

2. The bootstrap.yml configuration file

Spring: application: name: gateway: 9201 cloud: discovery: # config: server: addr: localhost:8847 /dataID: /dataID: /dataID: /dataID: /dataID: /dataID: /dataID: /dataID: /dataID: /dataID: /dataID Group: DEFAULT_GROUP group: DEFAULT_GROUP # configure the namespace, which is the value of the id of the namespace, by default public namespace # namespace: Prefix: ${spring.application.name} server: port: 9201 prefix: ${spring.application.name} server: port: 9201

Note: Since the configuration is placed on Nacos, the configuration related to Spring Cloud Alibaba Nacos Config needs to be placed in the bootstrap.yml configuration file.

This is where the configuration information for NACOS is mainly configured, not the routing information for the gateway.

3. Startup classes

@SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class); }}

No additional annotations are required on the boot class.

4. Gateway configuration on NaCos

spring: cloud: gateway: routes: - id: user-9202 uri: lb://product-9202 predicates: - Path=/user/** filters: - RewritePath=/user(? <segment>/? .*), $\{segment} - id: product-9203 uri: lb://product-9203 predicates: - Path=/product123/** filters: - RewritePath=/product123(? <segment>/? .*), $\{segment}

5. The gateway configuration is complete

The gateway configuration is complete, so the route can be dynamically refreshed after configuration? Yes. After the integration of Spring Cloud Gateway and Spring Cloud Alibaba Nacos Config, the automatic refresh function of configuration is automatically realized.

2. Realization of goods and services

Goods and services simply provide a REST API and then register with Nacos.

Five, achieve the effect

Six, complete the code

https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/gateway-dynamic-refresh-route

7. Reference documents

1, https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-config-e xample/readme-zh.md