spring-cloud项目中gateway配置解析
文章目录
- spring-gateway配置
- 📋 文件结构说明
- 🏗️ 配置分层解析
- 1. **bootstrap.yml - 基础配置**
- 2. **bootstrap-dev.yml - 开发环境配置**
- 1. **Nacos 服务发现 (Service Discovery)**
- 2. **Nacos 配置中心 (Configuration Center)**
- 3. **命名空间 (Namespace)**
- 🎯 配置文件加载顺序
- 🌐 微服务架构中的角色
- Nacos中的配置
spring-gateway配置
Spring Cloud 项目的核心配置,采用了 Nacos 作为配置中心和服务注册中心。
📋 文件结构说明
bootstrap.yml # 主引导配置文件(高优先级)
bootstrap-dev.yml # 开发环境特定配置(通过spring.profiles.active=dev激活)
🏗️ 配置分层解析
1. bootstrap.yml - 基础配置
# Tomcat 服务器配置
server:port: 39701 # 网关服务端口号# Spring 应用配置
spring:application:name: teach-gateway # 应用名称(重要!用于服务发现)profiles:active: dev # 激活dev环境配置
关键作用:
- ✅ 定义服务启动端口:39701
- ✅ 设置应用名称:
teach-gateway
(服务注册的标识) - ✅ 指定使用dev环境配置
2. bootstrap-dev.yml - 开发环境配置
spring:cloud:nacos:discovery:server-addr: xxx.xxx.xx.xxx:8848 # Nacos服务发现地址namespace: research # 命名空间(环境隔离)config:server-addr: xxx.xxx.xx.xxx:8848 # Nacos配置中心地址file-extension: yml # 配置文件格式namespace: research # 配置命名空间shared-configs:- data-id: teach-common.yml # 共享配置文件
1. Nacos 服务发现 (Service Discovery)
discovery:server-addr: xxx.xxx.xx.xxx:8848 # Nacos服务器地址namespace: research # 命名空间(环境隔离)
- 作用: 服务注册与发现,微服务自动注册到Nacos
- 地址:
xxx.xxx.xx.xxx:8848
- Nacos服务器 - 命名空间:
research
- 用于环境隔离
2. Nacos 配置中心 (Configuration Center)
config:server-addr: xxx.xxx.xx.xxx:8848 # 与discovery相同地址file-extension: yml # 使用YAML格式配置文件namespace: research # 配置命名空间shared-configs:- data-id: teach-common.yml # 共享通用配置
- 动态配置: 应用配置集中管理,实时更新
- 共享配置:
teach-common.yml
包含通用配置
3. 命名空间 (Namespace)
namespace: research
- 环境隔离: 实现多环境(dev/test/prod)配置隔离
- 数据隔离: 不同环境的服务注册和配置完全隔离
🎯 配置文件加载顺序
- bootstrap.yml → 基础配置(最先加载)
- bootstrap-{profile}.yml → 环境特定配置
- Nacos配置中心 → 动态远程配置
- application.yml → 本地应用配置(如果有)
🌐 微服务架构中的角色
客户端请求 → teach-gateway:39701 → Nacos服务发现 → 路由到具体微服务
Nacos中的配置
1.teach-gateway.yml
配置微服务入口及白名单
spring:cloud:gateway:discovery:locator:enabled: trueroutes:- id: jyh-basicuri: lb://jyh-basicpredicates:- Path= /basic/**- id: jyh-questionuri: lb://jyh-questionpredicates:- Path= /question/**
security:# 白名单ignore:whites:- /u-auth/index/**- /u-auth/wc/**- /u-auth/member/**- /**/swagger-ui/**- /**/swagger-resources/**- /**/swagger-resources- /**/v2/api-docs- /**/swagger-resources/configuration/ui- /**/doc.html# 放行所有静态资源- /**/*.css- /**/*.js- /**/*.html- /**/*.png- /**/*.jpg- /**/*.jpeg- /**/*.gif- /**/*.svg- /**/*.woff- /**/*.woff2- /**/*.ttf- /**/*.eot- /**/static/**- /**/assets/**
2.teach-common.yml
配置数据库、redis、短信服务等公共配置
redisson:password: XXXsingleServerConfig:address: xxx.xxx.xx.xxx:6379database: 4codec: 'cn.qbs.wa.teach.common.redisson.codec.FastJsonCodec'spring:redis:database: 15host: xxx.xxx.xx.xxxport: 6379password: XXXXtimeout: 10000lettuce:pool:max-active: 100max-idle: 32max-wait: -1min-idle: 10datasource:type: com.zaxxer.hikari.HikariDataSourcehikari:# 是客户端等待连接池连接的最大毫秒数connection-timeout: 30000# 配置最小池大小minimum-idle: 8# 配置最大池大小maximum-pool-size: 16# 允许连接在连接池中空闲的最长时间(以毫秒为单位)idle-timeout: 120000# 池中连接关闭后的最长生命周期(以毫秒为单位)max-lifetime: 180000# 配置从池返回的连接的默认自动提交行为。默认值为true。auto-commit: true# 连接池的名称pool-name: WaHikariCP# 开启连接监测泄露leak-detection-threshold: 5000# 测试连接数据库connection-test-query: SELECT 1logging:level:org.springframework: warncom.alibaba.nacos: warncn.qbs.wa.teach.other.api: debug# 系统全局统一配置
global:# 账号禁用时长forbiddenDuration: 60# 数据库相关配置datasource:address: xxx.xxx.xx.xxx:3306username: rootpassword: xxxxali:sms:accessKeyId: 123accessSecret: 123signName: 123templateCode: 123minio:cos:accessKey: xxxxsecretKey: xxxxendpoint: https://xxx.xxx.xx.xxx:9000/user: xxxuserPassWord: xxxxbucket: xxxcustom:sms:requestUrl: http://xxx.xxx.xx.xxx:7862/smsaccount: xxxxpassword: xxxxextno: xxxx