• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

自己挖了个SpringCloud Gateway的坑

武飞扬头像
aduocd
帮助1

目录

1.背景

2.环境

3.坑

4.爬坑历程

1)第一次爬坑

2)第二次爬坑

3)第三次爬坑

4)第四次爬坑

5)出坑

5.总结

6.参考资料


1.背景

就是想用一把SpringCloud Gateway~~

2.环境

SpringBoot 2.1.2.RELEASE
SpringCloud Greenwich.SR1
Gateway 2.1.2.RELEASE

3.坑

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

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

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

  1.  
    spring:
  2.  
    application:
  3.  
    name: gateway-service
  4.  
    cloud:
  5.  
    gateway:
  6.  
    discovery:
  7.  
    locator:
  8.  
    enabled: true
  9.  
    routes:
  10.  
    - id: user-service
  11.  
    uri: lb://user-service
  12.  
    predicates:
  13.  
    - Path=/user/**

怎么得都调不通~~

4.爬坑历程

1)第一次爬坑

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

  1.  
    Description:
  2.  
     
  3.  
    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.
  4.  
     
  5.  
    Action:
  6.  
    Consider defining a bean of type "org.springframework.http.codec.ServerCodecConfigurer" in your configuration.

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

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.cloud</groupId>
  3.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  4.  
    <exclusions>
  5.  
    <exclusion>
  6.  
    <groupId>org.springframework.boot</groupId>
  7.  
    <artifactId>spring-boot-starter-web</artifactId>
  8.  
    </exclusion>
  9.  
    <exclusion>
  10.  
    <groupId>org.springframework.boot</groupId>
  11.  
    <artifactId>spring-boot-starter-webflux</artifactId>
  12.  
    </exclusion>
  13.  
    </exclusions>
  14.  
    </dependency>

2)第二次爬坑

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

  1.  
    spring:
  2.  
    application:
  3.  
    name: gateway-service
  4.  
    cloud:
  5.  
    gateway:
  6.  
    discovery:
  7.  
    locator:
  8.  
    enabled: true
  9.  
    lower-case-service-id: true // 第一次爬坑高度线。表示将请求中服务名改小写
  10.  
    routes:
  11.  
    - id: user-service
  12.  
    uri: lb://user-service
  13.  
    predicates:
  14.  
    - Path=/user/**

3)第三次爬坑

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

  1.  
    spring:
  2.  
    application:
  3.  
    name: gateway-service
  4.  
    cloud:
  5.  
    gateway:
  6.  
    discovery:
  7.  
    locator:
  8.  
    enabled: true
  9.  
    lower-case-service-id: true // 第一次爬坑高度线。表示将请求中服务名改小写
  10.  
    routes:
  11.  
    - id: user-service
  12.  
    uri: lb://user-service
  13.  
    predicates:
  14.  
    - Path=/user/**
  15.  
    filters:
  16.  
    - StripPrefix=1 // 第二次爬坑高度线。路径截取个数。例如:上述访问如果是http://网关IP:网关PORT/user/xxx到了后端服务上就成了http://服务IP:服务PORT/xxx
学新通

4)第四次爬坑

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

5)出坑

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

  1.  
    -- 祖工程
  2.  
    -- 网关服务 // 出坑
  3.  
    -- 父工程
  4.  
    -- 孙工程1
  5.  
    -- 孙工程2

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

  1.  
    <dependencies>
  2.  
    <dependency>
  3.  
    <groupId>org.springframework.cloud</groupId>
  4.  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5.  
    <version>2.1.2.RELEASE</version>
  6.  
    </dependency>
  7.  
    <dependency>
  8.  
    <groupId>org.springframework.cloud</groupId>
  9.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  10.  
    <version>2.1.2.RELEASE</version>
  11.  
    </dependency>
  12.  
    </dependencies>

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

  1.  
    spring:
  2.  
    application:
  3.  
    name: gateway-service
  4.  
    cloud:
  5.  
    gateway:
  6.  
    routes:
  7.  
    - id: user-service
  8.  
    uri: lb://user-service
  9.  
    predicates:
  10.  
    - Path=/user/**

5.总结

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

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

6.参考资料

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

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

SpringCloud Gateway配置自定义路由404坑

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

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjagfc
系列文章
更多 icon
同类精品
更多 icon
继续加载