xuziwen127 2021-03-18 15:02 采纳率: 55.6%
浏览 939
已结题

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
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月24日

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?