查看: 100|回复: 0

Spring Cloud Gateway网关路由配置

[复制链接]

2

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2010-8-15
发表于 2025-10-26 15:49:00 | 显示全部楼层 |阅读模式
Spring Cloud Gateway 配置使用 lb:// 协议时,需依赖以下组件:

核心依赖

‌Spring Cloud Gateway 依赖‌
需添加 spring-cloud-starter-gateway 依赖,用于启用网关功能。 ‌
 
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

服务注册中心依赖‌

若使用 lb:// 格式(即服务发现模式),需引入服务注册中心组件,例如Nacos 或 Eureka 的 Spring Cloud 启动器:
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

负载均衡器依赖‌

需引入 spring-cloud-starter-loadbalancer 依赖,用于实现服务发现和负载均衡:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

配置要求

‌服务发现配置‌:确保 Nacos/Eureka 等服务注册中心已正确配置,且服务已注册。 ‌
‌路由配置示例‌:
# Tomcat
server:
  port: 8080

# Spring
spring:
  application:
    # 应用名称
    name: test-gateway
  profiles:
    # 环境配置
    active: dev
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        # 系统模块
        - id: test-system
          uri: lb://test-system
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1
 
技术博主:AlanLee
博客地址:http://www.cnblogs.com/AlanLee
GitHub地址:https://github.com/AlanLee-Java


来源:https://www.cnblogs.com/AlanLee/p/19166931
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部