springcloud集成gateway

本篇文章只介绍gateway模块的搭建步骤,并无gateway详细介绍

gateway详解请查看:SpringCloudGateway官方文档详解

前置处理

父模块中已指定版本

不知道如何选择版本看这篇:
手把手教你梳理springcloud与springboot与springcloudalibaba的版本对应关系

一. Gateway模块引入依赖

复制代码
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<!--客户端负载均衡loadbalancer-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

注意:gateway无需引入spring-boot-starter-web,因为gateway内部集成了Netty服务器,启动会报错

二. 配置 bootstrap.yml 文件(nacos作为注册中心)

这里是引入nacos,gateway具体配置可以放在nacos配置中心

复制代码
spring:
  application:
    name: gateway
  profiles:
    active: dev
  cloud:
    nacos:
      username: nacos
      password: nacos

      discovery:
        # 命名空间
        namespace: 26dc328e-87e1-4ebb-9882-e46aa0cfbf0c
        # 同一个项目下的服务,要在同一个分组下,相互间才能通信
        group: dev
        # nacos服务端
        server-addr: 127.0.0.1:8848

      config:
        # nacos服务端
        server-addr: 127.0.0.1:8848
        # 分组
        group: dev
        # 命名空间
        namespace: 26dc328e-87e1-4ebb-9882-e46aa0cfbf0c
        # 配置文件类型
        file-extension: yaml
        # 读取公用配置
        shared-configs:
          - data-id: common-dev.yaml
            group: dev
            refresh: true

三. nacos配置中心中对应gateway配置文件

复制代码
server:
  port: 7000

spring:
  cloud:
    # 动态路由
    gateway:
      discovery:
        locator:
          # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
          enabled: true
          # 表示将请求路径的服务名配置改成小写 ,因为服务注册的时候,向注册中心注册时将服务名转成大写的了
          lowerCaseServiceId: true
          # 若配置路由URI为lb,则注册中心服务名称不能和server.servlet.context-path名称一致,否则contextPath会被改写为空字符串;加上这一行配置可以解决问题
          filters:
           - StripPrefix=0

      # 路线
      routes:
        - id: admin # 路线ID,保持唯一
          # 表示使用负载均衡策略,admin是服务ID
          uri: lb://admin
          # uri: http://localhost:7001
          # predicates定义了路由的匹配条件,这里使用的是路径匹配,所有以/admin/开头的请求都会被路由到admin服务。
          predicates:
            - Path=/admin/**

        - id: member
          uri: lb://member
          predicates:
            - Path=/member/**

        - id: netdisk
          uri: lb://netdisk
          predicates:
            - Path=/netdisk/**

        - id: blog
          uri: lb://blog
          predicates:
            - Path=/blog/**

        - id: thirdparty
          uri: lb://thirdparty
          predicates:
            - Path=/thirdparty/**

      # 处理跨域请求
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedHeaders: "*" # 允许所有请求头
            allowedOriginPatterns: "*" # 允许所有请求源
            #- "http://www.baidu.com" # 允许特定请求源
            allowCredentials: true # 允许发送Cookie
            allowedMethods: # 允许所有请求方法
              - GET
              - POST
              - DELETE
              - PUT
              - OPTION

四. 检查gateway是否配置成功

还用这篇文章的测试接口 springcloud项目引入naocs2.x

admin模块有一个测试接口,直接调用的连接为

复制代码
http://localhost:7001/admin/nacos/getCommonNacosConfig

用gateway转发的连接为

http://localhost:7000/admin/nacos/getCommonNacosConfig

五. 问题汇总

1. context-path和application.name一致问题:

若配置路由URI为lb,则注册中心服务名称不能和server.servlet.context-path名称一致,否则contextPath会被改写为空字符串;

解决方案:

  1. context-path和application.name设置为不同
  2. filters:
    • StripPrefix=0
  3. filters[0]:
    • StripPrefix=PreserveHostHeader

参考文章:https://blog.csdn.net/weixin_43303455/article/details/122279447

2. Whitelabel Error Page This application has no configured error view, so you are seeing this as a fallback.

原因是缺少负载均衡依赖

解决方法:gateway模块引入spring-cloud-starter-loadbalancer依赖

复制代码
<!--客户端负载均衡loadbalancer-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
相关推荐
大G哥2 小时前
实战演练:用 AWS Lambda 和 API Gateway 构建你的第一个 Serverless API
云原生·serverless·云计算·gateway·aws
RingWu4 小时前
微服务架构-限流、熔断:Alibaba Sentinel入门
微服务·架构·sentinel
李匠20244 小时前
C++GO语言微服务基础技术②
开发语言·c++·微服务·golang
说淑人5 小时前
Spring Cloud & 以Gateway实现限流(自定义返回内容)
java·spring cloud·gateway·限流
掘金-我是哪吒5 小时前
分布式微服务系统架构第130集:Python工程化FastAPI,运维Nginx-keepalived+Nginx实现高可用集群
运维·分布式·微服务·系统架构·fastapi
喵叔哟6 小时前
21.【.NET 8 实战--孢子记账--从单体到微服务--转向微服务】--单体转微服务--身份认证服务拆分规划
微服务·云原生·架构
李匠20247 小时前
C++GO语言微服务之Dockerfile && docker-compose
c++·docker·微服务·架构
掘金-我是哪吒17 小时前
分布式微服务系统架构第127集:cassandra安装部署
分布式·微服务·云原生·架构·系统架构
Kookoos19 小时前
ABP vNext + Dapr 实现云原生微服务治理
微服务·云原生·架构·c#·.net
码农飞哥1 天前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答与解析
java·数据库·spring boot·安全·微服务·面试·电商