1、官网地址
spring-cloud-gateway官网:https://docs.spring.io/spring-cloud-gateway
2、创建GateWay工程
3、pom.xml文件中引入依赖
<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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
版本参见:
4、application.yml中添加配置
注意:以上的id和nacos中的项目名称不允许使用下划线,否则地址无法访问
5、创建启动类
6、测试
(1)将项目service-order项目启动注入到nacos中,将cloud-gateway项目启动注入到nacos中
如下
(2)访问service-order中的接口,可正常访问
访问gateway中的接口也可以正常访问
(3)通过gateway访问service-order中的接口
注:
如果加过滤,例如:将http://localhost:8080/service-order/test 地址转为:http://localhost:8080/test
代表把Path中的第一个api-passenger去掉