小强哥博客

小强哥,小强哥博客,技术大咖

spring cloud gateway踩坑记录(二)

由于项目需要,最近开始使用springgatway,这篇文章主要记录下遇到的坑。

springboot使用,如下,

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
  </parent>

springcloud gateway,如下,

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
      <version>2.0.0.RELEASE</version>
    </dependency>

springcloud使用,如下,

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

第二个坑,通过路由访问后端服务,如果后端服接口返回值为void时,出现500错误,如下,

后端服务接口如下定义,这里作为演示,仅用Put方法作为演示,

@PutMapping(value = "/v1/course/normals/test/test")
  public void putTets(){
    // TOOD 业务逻辑
  }

路由定义如下,

.route(
            r -> r.path("/v1/course/normals/test").and().host(plateformHost).and()
                .method(
                    HttpMethod.PUT)
                .uri("lb://pacaya")
        )

通过路由访问出现错误,如下,

java.lang.NullPointerException: null
	at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011) ~[na:1.8.0_171]
	at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006) ~[na:1.8.0_171]
	at org.springframework.cloud.gateway.filter.NettyRoutingFilter.lambda$filter$3(NettyRoutingFilter.java:117) ~[spring-cloud-gateway-core-2.0.0.RELEASE.jar:2.0.0.RELEASE]
	at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:177) ~[reactor-core-3.1.9.RELEASE.jar:3.1.9.RELEASE]
	at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:108) ~[reactor-core-3.1.9.RELEASE.jar:3.1.9.RELEASE]

解决方法为升级 spring-cloud-gateway-core 依赖,如下,

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
      <version>2.0.1.RELEASE</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-gateway-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-gateway-core</artifactId>
      <version>2.0.1.RELEASE</version>
    </dependency>

改问题在github上被指定为bug,已经有人反馈过了,https://github.com/spring-cloud/spring-cloud-gateway/issues/392

https://github.com/morganyvm/jsprbt-fa8-k8s/commit/4ec09bc5ed48404a44123d25c285e325801c8699

完。