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>
相关推荐
陈文锦丫12 小时前
微服务-----
java·数据库·微服务
櫻花12 小时前
微服务各大组件总结
微服务·云原生·架构
-大头.12 小时前
微服务架构深度演进与实践指南
微服务·云原生·架构
拾忆,想起15 小时前
Dubbo序列化异常终结指南:从精准诊断到根治与防御
开发语言·前端·微服务·架构·php·dubbo·safari
掘金-我是哪吒15 小时前
第378集设备服务接入系统Java微服务后端架构实战
java·开发语言·spring·微服务·架构
boy快快长大16 小时前
【Spring Cloud Alibaba】Gateway(一)
数据库·gateway
better_liang16 小时前
每日Java面试场景题知识点之-Spring Boot微服务配置管理
java·spring boot·微服务·面试题·配置管理
递归尽头是星辰17 小时前
服务治理三维实战:从架构理论到规模适配,解决六大核心复杂性
微服务·服务治理·分布式系统·高可用架构·java 架构·大规模服务
JavaEdge.17 小时前
零距离拆解银行司库系统(TMS)的微服务设计与实践
微服务·云原生·架构
听风吟丶17 小时前
日志中心实战:ELK Stack 构建微服务智能日志管理体系
elk·微服务·架构