gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩。老套路,三板斧:
1、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"> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-alibaba-gateway</artifactId> <groupId>com.wlf</groupId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.9.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application.yml(因为涉及到路由映射,routes下面会有很多配置,yml成了标配):
#端口 server: port: 8484 #应用名 spring: application: name: lxytrans-gateway #注册中心 cloud: nacos: discovery: server-addr: localhost:8848 register-enabled: true # 网关 gateway: discovery: locator: lowerCaseServiceId: true enabled: true routes: - id: lxytrans-consumer uri: lb://lxytrans-consumer predicates: - Path=/wlf/consumer/** filters: - StripPrefix=2 - id: lxytrans-provider uri: lb://lxytrans-provider predicates: - Path=/wlf/provider/** filters: - StripPrefix=2
这里的配置说明下:
spring.cloud.gateway.discovery.locator.lowerCaseServiceId:支持服务实例名小写(如果nacos上的服务名是大写的话)。
spring.cloud.gateway.discovery.locator.enabled:支持gateway到注册中心进行服务的注册和发现,打开后可以直接通过服务实例名访问。
spring.cloud.gateway.routes:
id
:路由的ID,唯一。uri
:匹配路由的转发地址,lb是load balance缩写,后面带转发的服务实例名。predicates
:配置该路由的断言,有很多种断言方式,可以组合使用。最常用的Path是请求路径匹配。- filters:过滤规则,也是有多种。这里StripPrefix用来截去Path前面的路径,从左边开始截,配1就截去1,上面配2,那么/wlf/provider/hello最终转发过去就是/hello。
3、启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class TransGatewayApplication { public static void main(String[] args) { SpringApplication.run(TransGatewayApplication.class, args); } }
打完收功。跑起来后nacos会出现gateway实例:
我们的测试还是基于之前弄的服务提供方和消费方(最新的参见0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例)。从上面看到服务消费方、提供方的实例都是活的,通过网关来试试调用它们:
直接用实例名(消费方调服务方超时降级):
用路由: