xuziwen127
2021-03-18 15:02
采纳率: 76.5%
浏览 389
已采纳

nacos+gateway通过网关请求接口,不管接口路径是否正确,都返回空值

自己搭了一个sping cloud+nacos+gateway的微服务框架,不通过gateway网关转发直接访问接口是可以获取到数据的,通过网关取访问接口就会返回空,而且我只要写了配置文件中存在的路径匹配规则,哪怕后面写了一个不存在的路径,都会返回空;更更更更更离谱的是这个情况只有在我开发用的笔记本上出现,同样的代码在我同事的电脑或者我自己的台式机上都是没有问题的。我把项目的配置文件和出现的情况截图发出来:

maven父工程的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hidetail</groupId>
  <artifactId>tail_parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
  </parent>
  
  <properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
	<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
  </properties>
  
  <dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Hoxton.SR3</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
		    <groupId>com.alibaba.cloud</groupId>
		    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
		    <version>2.2.1.RELEASE</version>
		    <type>pom</type>
		    <scope>import</scope>
		</dependency>
	</dependencies>
  </dependencyManagement>
  
  <dependencies>
  	<dependency>
    	<groupId>com.alibaba.cloud</groupId>
    	<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <dependency>
	    <groupId>com.alibaba.cloud</groupId>
	    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	</dependency>
	
	<!-- <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency> -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
  </dependencies>
  
  <build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
  </build>
  <modules>
    <module>tail_client1</module>
    <module>tail_gateway</module>
  </modules>
</project>

gateway的pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hidetail</groupId>
    <artifactId>tail_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.hidetail.tail_gateway</groupId>
  <artifactId>tail_gateway</artifactId>
  <name>tail_gateway</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  	<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>
  </dependencies>
</project>

client1的pom.xml:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hidetail</groupId>
    <artifactId>tail_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.hidetail.client1</groupId>
  <artifactId>tail_client1</artifactId>
  <name>tail_client1</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
  </dependencies>
</project>

gateway的配置文件:

spring:
  application:
    name: tail-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
      - id: tail-client1
        uri: lb://tail-client1
        predicates:
        - Path=/tailClient1/**
        filters:
        - StripPrefix=1
server:
  port: 9437

client1中的测试接口:

package com.hidetail.client1.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/firsttest")
public class FirstTestController {
	@RequestMapping(value="/findinterface")
	public String findInterface() {
		return "maven聚合接口搭建成功";
	}
}

通过网关请求的结果:

可以看到http响应码是200但数据为空,下面是不通过网关请求接口的结果:

试了很多方法,换spring cloud版本,改配置规则,上网查资料都无济于事,现在我已学废了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 三岁丫 2021-03-18 15:22
    已采纳

    如果相同的代码你不可以,别人可以,只能说是环境问题了。

    点赞 评论
  • xuziwen127 2021-03-21 22:43

    解决了,是maven仓库里的包有问题,最终和同事对比启动日志发现少加载了一些类得出的结果,最后我把整个maven库删了又重新导了一边maven就正常了。这个问题确实坑人,从编辑器和日志上看不出任何问题。。。。

    点赞 评论

相关推荐 更多相似问题