在nacos和gateway整合的时候,报错
java.lang.IllegalStateException: Can not get service instances from nacos, serviceId=null
意思是服务找不到,在nacos里面找不到,可是明明已经看见了啊,已经注册进去了啊,为啥找不到, gateway里面的这个是通过服务名找项目
所以就出在这个问题上;
就是你的版本问题了,这个整合版本问题是坑,意思是你得找可以使用这个lb模式的版本才可以;
使用其他版本,可以使用http的形式,但是如果你想要使用lb这个模式,就得找对版本
目前我找到一个
首先是父项目的依赖,直接使用我的这个,你的那些版本,可能不支持,或者有解决的方法,但是你得花时间找,直接拿上我的这个版本就可以
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modules>
<!-- <module>order</module>-->
<!-- <module>stock</module>-->
<!-- <module>order-ribbon</module>-->
<!-- <module>order-nacos</module>-->
<module>stock-nacos</module>
<module>order-openfeign</module>
<module>gateway</module>
<!-- <module>nacos-config</module>-->
<!-- <module>order-sentinel</module>-->
<!-- <module>gateway</module>-->
<!-- <module>Sentinel-demo</module>-->
</modules>
<!-- <parent>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.3.2.RELEASE</version>-->
<!-- <relativePath/> <!– lookup parent from repository –>-->
<!-- </parent>-->
<!--属性集合-->
<properties>
<java.version>1.8</java.version>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.boot.version>2.5.5</spring.boot.version>
<spring.cloud.version>2020.0.4</spring.cloud.version>
<spring.cloud.bootstrap.version>3.0.4</spring.cloud.bootstrap.version>
<spring.cloud.alibaba.version>2021.1</spring.cloud.alibaba.version>
<stream.cloud.version>3.1.4</stream.cloud.version>
<gateway.version>3.0.4</gateway.version>
<!-- <java.version>1.8</java.version>-->
<!-- <spring.cloud.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version>-->
<!-- <spring.cloud.version>Hoxton.SR8</spring.cloud.version>-->
<!-- <spring.boot.version>2.3.11.RELEASE</spring.boot.version>-->
</properties>
<groupId>com.jing</groupId>
<artifactId>jing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <packaging>pom</packaging>-->
<name>jing</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>com.alibaba.csp</groupId>-->
<!-- <artifactId>sentinel-core</artifactId>-->
<!-- <version>1.8.0</version>-->
<!-- </dependency>-->
<!-- springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-dependencies</artifactId>-->
<!-- <version>Hoxton.SR3</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<!--引入注册中心阿里巴巴-->
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- <version>2.0.2.RELEASE</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-dependencies</artifactId>-->
<!-- <version>${spring.cloud.version}</version>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
<!-- <version>2.2.6.RELEASE</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-alibaba-dependencies</artifactId>-->
<!-- <version>${spring.cloud.alibaba.version}</version>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
</dependencies>
<!--全局引入下载依赖地址,并不会引入依赖-->
<dependencyManagement>
<!--放在dependencyManagement里的依赖,子maven一定要显式的声明,
没有放在dependencyManagement里的依赖,子maven直接继承父maven的没放的依赖
-->
<!--spring cloud alibaba的版本管理器,通过dependency方式来完成继承-->
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot的版本管理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud的版本管理-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
</plugin>
</plugins>
</build>
</project>
gateway项目里面的pom文件
<?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">
<parent>
<artifactId>jing</artifactId>
<groupId>com.jing</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<dependencies>
<!--gateway依赖,需要有springcloud的依赖管理器,如果没有设置的话,需要指定版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos服务注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
openfeign的项目里面
<?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">
<!-- 订单服务-->
<parent>
<artifactId>jing</artifactId>
<groupId>com.jing</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-openfeign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 1 <!‐‐ openfeign 远程调用 ‐‐>-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<!-- <version>2.2.6.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<!-- <version>2.2.6.RELEASE</version>-->
</dependency>
<!-- nacos 的服务注册与发现的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!-- <version>2.0.2.RELEASE</version>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<!-- <version>3.0.2</version>-->
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-bootstrap</artifactId>-->
<!-- </dependency>-->
</dependencies>
</project>
接下来是gateway的配置文件
server:
port: 8098
spring:
# main:
# allow-bean-definition-overriding: true
application:
name: gateway
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
password: nacos
namespace: public
username: nacos
service: gateway
# gateway 的配置
gateway:
#gateway的配置
discovery:
locator:
enabled: true #是否启动自动识别nacos服务
# 路由规则
routes:
- id: orderroute # 路由唯一的标识
uri: lb://orderopenfeign
# 断言规则,用于路由规则的匹配
predicates:
- Path=/orderserver/**
# 过滤器
filters:
- StripPrefix=1 # 作用是 转发之前去掉 第一层的路径
使用以上的pom里面的版本就可以实现了,不会出现 Nacos+Gateway使用lb: xxxservice不起作用;
其他版本的,你们找到解决方法了嘛,欢迎评论,一起进步