pom.xml核心配置
spring boot、spring cloud 、spring cloud alibaba三个组件版本需要配置正确,版本匹配不对,可能导致工程错误。
版本参照:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<properties>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
application.xml核心配置
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8080
namespace: xxxxxxxxxxxxxxxxxx
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
filters:
需要注意:
- 当未配置"spring.cloud.gateway.discovery.locator.filters="时,访问http://localhost:10001/actuator/gateway/routes
显示下列内容,filter中包含了一串内容,此时访问dateway时,url地址中需要带上两次test
http://localhost:10001/test/test/info?departmentId=22
{
"predicate": "Paths: [/test/**], match trailing slash: true",
"metadata": {
"nacos.instanceId": "127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@test",
"nacos.weight": "1.0",
"nacos.cluster": "DEFAULT",
"preserved.register.source": "SPRING_CLOUD",
"nacos.healthy": "true"
},
"route_id": "ReactiveCompositeDiscoveryClient_test",
"filters": [
"[[RewritePath /test/(?<remaining>.*) = '/${remaining}'], order = 1]"
],
"uri": "lb://test",
"order": 0
}
-
当未配置"spring.cloud.gateway.discovery.locator.filters="时,访问http://localhost:10001/actuator/gateway/routes
显示下列内容,filter中包含了一串内容,此时访问dateway时,url地址中只需要带一次工程名,访问正确
http://localhost:10001/test/test/info?departmentId=22
{
"predicate": "Paths: [/test/**], match trailing slash: true",
"metadata": {
"nacos.instanceId": "127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@test",
"nacos.weight": "1.0",
"nacos.cluster": "DEFAULT",
"preserved.register.source": "SPRING_CLOUD",
"nacos.healthy": "true"
},
"route_id": "ReactiveCompositeDiscoveryClient_test",
"filters": [],
"uri": "lb://test",
"order": 0
}