自己挖了个SpringCloud Gateway的坑

目录

1.背景

2.环境

3.坑

4.爬坑历程

1)第一次爬坑

2)第二次爬坑

3)第三次爬坑

4)第四次爬坑

5)出坑

5.总结

6.参考资料


1.背景

就是想用一把SpringCloud Gateway~~

2.环境

SpringBoot2.1.2.RELEASE
SpringCloudGreenwich.SR1
Gateway2.1.2.RELEASE

3.坑

自己模拟的微服务工程,结构是这样的:

-- 祖工程
   -- 父工程
      -- 孙工程1
      -- 孙工程2
      -- 孙工程3(网关服务)        // 坑

因为理解网关也是同其余业务一样,是一个微服务工程,所以就把网关也做到了孙工程里(这一开始开始挖,就整了个大坑~~~)。然后开始按照通常配置:

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**

怎么得都调不通~~

4.爬坑历程

1)第一次爬坑

无法启动,出现类似报错:

Description:

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type "org.springframework.http.codec.ServerCodecConfigurer" that could not be found.

Action:
Consider defining a bean of type "org.springframework.http.codec.ServerCodecConfigurer" in your configuration.

按照网上说的,去掉两个包依赖。能正常启动了,暂时出坑~~

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2)第二次爬坑

以为是自己的配置问题。因为注册中心中,服务名是大写的,看度娘说,需要加参数,于是:

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true    // 第一次爬坑高度线。表示将请求中服务名改小写
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**

3)第三次爬坑

因为访问接口时一直报404,所以以为是自己的uri哪里没写对(也不知道自己为什么就自动觉得uri有问题,如果uri有问题,不是应该是报类似router错误么~~)。于是:

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true    // 第一次爬坑高度线。表示将请求中服务名改小写
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**
          filters:
            - StripPrefix=1            //  第二次爬坑高度线。路径截取个数。例如:上述访问如果是http://网关IP:网关PORT/user/xxx到了后端服务上就成了http://服务IP:服务PORT/xxx

4)第四次爬坑

从第三次到第四次中间爬了一小段~~以为是依赖包的版本匹配问题,所以各种调整依赖包版本,又回到了第一个坑~~这个时候也出现了一些曙光:发现所有父工程pom.xml中引入的依赖包也自动引入了到了网关工程里。这里就疑惑了,按照以往的逻辑,子工程不引入的话,父工程的包应该不会自动出现在子工程的依赖中的。开始怀疑,工程的位置~~

5)出坑

既然有许多其他无关依赖包,且排除也无效,那么调整工程结构试试~~

-- 祖工程
   -- 网关服务        // 出坑
   -- 父工程
      -- 孙工程1
      -- 孙工程2

调整工程结构以后,也不需要按照第一次爬坑中进行包的排除操作了。这里只引入了eureka和gateway的包即可

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
         <version>2.1.2.RELEASE</version>
    </dependency>
</dependencies>

配置也做了相应的调整,这样简单配置也能够访问服务了

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**

5.总结

虽然还是不完全理解为什么网关为什么不能和业务服务一级,也不太理解为什么使用filters的StripPreix时需要把Path增加一级路径/user-service,但现在起码能通了。

如有高手路过,还望能指点一二,感激涕零~~

6.参考资料

SpringCloud Gateway 修改请求路径的过滤器(StripPrefix Filter和PrefixPath Filter)_刘德华一不小心就打代码的博客-CSDN博客

SpringCloud--GateWay搭建及路由转发规则介绍_没有对象的指针-博客-CSDN博客

Spring Cloud Gateway -- 关于Path的配置 - 简书

SpringCloud之服务网关Gateway_我的博客-CSDN博客

springboot集成springCloud中gateway时启动报错的解决-云海天教程

SpringCloud Gateway配置自定义路由404坑

SpringCloud之Gateway使用篇_猿上生活-CSDN博客

SpringCloud-Greenwich版本新特性探索(1)---SpringCloudGateway - 未分配微服务 - 博客园

springcloud gateway网关多路由配置访问404解决方案_wyler *_*的博客-CSDN博客 

Spring Cloud Gateway 多种思路实现动态路由 - 简书

springcloud使用gateway访问项目接口404_Destiny121325的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值