SpringCloud alibaba之gatway搭建(踩了很多坑)

SpringCloud alibaba之gatway搭建(踩了很多坑)

搭建流程

引入依赖
说明: 我的加了log4j2,所以排除掉logging;路由的配置是放在SpringCloud alibaba config服务上,所以加了config的依赖,如果只是路由配置不需要放在配置中心,则需要去掉下面spring-cloud-starter-alibaba-nacos-config的依赖,不然启动会报错。

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- alibaba的cloud组件 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.0.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gateway-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

配置文件

本地配置

bootstrap.yml

spring:
  profiles:
    active: dev

bootstrap-dev.yml

server:
  port: 8080   #gatway是无法设置contex-path的
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        namespace: 04783ad2-a10a-4a29-8a98-165ec0d507e1  #nacos上组的id(命名空间)
        server-addr: 192.168.31.11:8848  #nacos的地址
      config:
        namespace: ${spring.cloud.nacos.discovery.namespace}
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        #以下为加载一个配置文件,多个配置请复制下面,更改数组的序号和data-id的对应你要引入的文件名称
        ext-config[0]:  
          group: fitness-dev #这个填你用的nacos上那一组的配置文件
          data-id: gatway-${spring.profiles.active}.yml

如下图
在这里插入图片描述

Nacos配置中心的路由文件配置

spring:
  cloud:
    gateway:
      discovery:
        locator:
          ####开启以服务id去注册中心上获取转发地址
          enabled: true
        ###路由策略
      routes:
        ###fitness-api登录服务
        - id: fitness-api
          uri: lb://fitness-api   #fitness-api需要转发的服务名称,就是nacos的注册服务名称
          predicates:
          - Path=/fitness/**  #fitness是 fitness-api服务的context-path的路径,(注意:fitness-api  和 fitness千万别设置成一样的,不然会出现问题)

踩坑记录

  1. gatway不能引入spring-boot-starter-web依赖
  2. gatway不能设置contex-path
  3. 被gatway转发的服务中,服务名字和context-path不能一样,不然会出问题,具体问题可自己测试,debug org.springframework.cloud.gateway.filter.LoadBalancerClientFilter中的filter方法,看下uri的变化。
  4. 参考:https://www.cnblogs.com/zhixie/p/13150402.html
  5. 参考:https://blog.csdn.net/destiny121325/article/details/103296351
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的老菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值