【SpringCloud教程】5.SpringCloud Gateway 整合配置中心实现动态配置

每次更改网关的yml都需要项目,这样非常不方便,以下两种方式可以实现动态配置:

1.基于配置中心

2.基于数据库

本文通过nacos的配置中心来动态更改配置,配置如下:

Nacos配置

在Nacos 创建配置文件:

【SpringCloud教程】5.SpringCloud Gateway 整合配置中心实现动态配置_第1张图片

Data ID: gateway.yaml

配置内容如下:

spring:
  cloud:
    gateway:
      # 配置路由
      routes:
        # id 保证唯一
        - id: csdn
          # 配置跳转路径(这里以我的博客首页为例)
          uri: https://blog.csdn.net/qq_37493888/
          # 路由条件
          predicates:
            - Path=/csdn/**
        - id: user
          # 配置跳转路径(这里以我的博客首页为例)
          uri: lb://user-api
          # 路由条件
          predicates:
            - Path=/user/**
          filters:
            - StripPrefix=1

Gateway配置

创建bootstrap.yml,而不是application.yml,配置如下:

spring:
  application:
    name: gateway
  cloud:
    nacos:
      # 注册中心
      discovery:
        server-addr: 127.0.0.1:8848
      # 配置中心
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
    gateway:
      discovery:
        locator:
          #### 开启以服务id去注册中心上获取转发地址
          enabled: true

pom.xml 添加如下依赖

	<dependencies>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webfluxartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-loadbalancerartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
            <version>3.0.0version>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-parentartifactId>
                <version>2.4.2version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>2020.0.5version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2021.1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 网关配置
 * @author terry
 * @version 1.0
 * @date 2022/5/20 23:06
 */
@SpringBootApplication
@EnableDiscoveryClient
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

启动访问:http://localhost:8080/csdn,成功

修改配置csdn修改成csdn1如下:

【SpringCloud教程】5.SpringCloud Gateway 整合配置中心实现动态配置_第2张图片

发布后,访问:http://localhost:8080/csdn1,成功;

你可能感兴趣的:(SpringCloud,spring,cloud,gateway,spring,boot)