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

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日

悬赏问题

  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口