问题描述,配置了gateway后发起请求503
1. gateway 项目:
- 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/>
</parent>
<groupId>com.eid</groupId>
<artifactId>eid-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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-gateway</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
这里要说明一下,spring cloud alibaba 2021.* 版本
需要自己引入gateway 与springboot版本对应起来就好。
- 启动文件
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
- 配置文件
server:
port: 20600
spring:
application:
name: gateway
cloud:
nacos:
discovery:
enabled: true
server-addr: 127.0.0.1:8848 # nacos服务地址
username: xxx # nacos 用户名
password: xxx # nacos 密码
gateway:
discovery:
locator:
enabled: true
routes:
- id: user-server-route
uri: lb://user-server
predicates:
- Path=/user-api/**
filters:
- StripPrefix=1
- name: Retry # Retry 为重试过滤器,当后端服务不可用时,网关会根据配置参数来发起重试请求
args:
retries: 3
status: 503
- 启动后 使用gateway 网关访问时就会出现503
解决方法
添加依赖 spring-cloud-starter-loadbalancer
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>3.1.6</version>
</dependency>
再次访问,通了
测试异常原因
这里的配置请求不通过会返回505,而上图是返回的503,说明是没有执行转发操作
这里应该是nacos 从2021版本 不在集成ribbon
而是要使用 spring-cloud-starter-loadbalancer 来实现负载均衡’
而我的版本是2021.0.4.0所以 手动引入 即可解决
自己踩过的坑 mark一下