在上一篇文章Spring Cloud GateWay 路由转发规则介绍中咱们讲解了SpringCloud Gateway
内部提供的断言、谓语
,让咱们能够组合更精确的业务场景进行请求,既然SpringCloud GateWay
担任了网关
的角色,在以前Zuul
能够经过服务名进行自动转发,SpringCloud Gateway
是否能够实现自动转发呢?html
Spring Cloud Gateway
能够根据配置的断言、谓语
进行知足条件转发,也能够自动同步服务注册中心
的服务列表进行指定serviceId
前缀进行转发,这里的serviceId
是业务服务的spring.application.name
配置参数。java
把SpringCloud
的版本依赖添加到pom.xml
内,以下所示:git
//... <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <!--Spring Cloud 版本控制--> <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> </dependencies> </dependencyManagement> //...
咱们本章使用Eureka
做为服务注册中心来完成服务请求转发讲解,须要把Spring Cloud Gateway
网关项目做为一个Client
注册到Eureka Server
,先来看下添加的依赖,pom.xml
以下所示:web
//... <dependencies> <!--Spring Cloud Gateway--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--Eureka Client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> //....
接下来咱们须要开启Gateway
服务注册中心的发现配置,开启后才能自动同步服务注册中心的服务列表
,application.yml
配置文件以下所示:spring
# 服务名称 spring: application: name: spring-cloud-gateway # 开启 Gateway 服务注册中心服务发现 cloud: gateway: discovery: locator: enabled: true # Eureka Server 配置 eureka: client: service-url: defaultZone: http://localhost:10000/eureka/ # 配置Gateway日志等级,输出转发细节信息 logging: level: org.springframework.cloud.gateway: debug
配置参数解释以下所示:api
spring.application.name
:服务名spring.cloud.gateway.discovery.locator.enabled
:开启SpringCloud Gateway
的注册中心发现配置,开启后可自动从服务注册中心拉取服务列表,经过各个服务的spring.application.name
做为前缀进行转发,该配置默认为false
。eureka.client.service-url.defaultZone
:配置Eureka Server
默认的空间地址logging.level.org.springframework.cloud.gateway
:设置SpringCloud Gateway
日志等级为debug
,用于输出转发的细节日志,方便查看细节流程。在入口类添加对应的注解,开启服务自动注册,以下所示:app
@SpringBootApplication @EnableDiscoveryClient public class SpringCloudGatewayApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudGatewayApplication.class, args); } }
对应上面网关
配置的Eureka Server
的地址,咱们须要添加对应的配置,pom.xml
以下所示:负载均衡
//... <dependencies> <!--Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> //...
添加依赖后对Eureka Server
进行配置,配置文件application.yml
以下所示:curl
# 服务名 spring: application: name: sample-eureka-server # 端口号 server: port: 10000 # Eureka 配置信息 eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka/ fetch-registry: false register-with-eureka: false
这里咱们修改默认的端口号为10000
,为了匹配在网关项目
的配置信息,至于fetch-registry
、register-with-eureka
能够去我以前的文章查看,SpringCloud组件:将服务提供者注册到Eureka集群spring-boot
咱们经过@EnableEurekaServer
注解来开启服务,以下所示:
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
网关
、服务注册中心
咱们都已经准备好了,下面咱们能够编写业务逻辑服务,来验证SpringCloud Gateway
具体是否能够根据serviceId
进行转发请求。
咱们简单编写一个GET
请求地址,输出字符串信息,pom.xml
添加依赖以下所示:
<dependencies> <!--Web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Eureka Client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
配置文件application.yml
以下所示:
# 服务名 spring: application: name: user-service # 注册到Eureka eureka: client: service-url: defaultZone: http://localhost:10000/eureka/ # 服务端口号 server: port: 9090
配置该服务的服务名称为user-service
,这里对应SpringCloud Gateway
的serviceId
。
@SpringBootApplication @EnableDiscoveryClient @RestController public class UserServiceApplication { /** * logger instance */ static Logger logger = LoggerFactory.getLogger(UserServiceApplication.class); public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); logger.info("「「「「「用户服务启动完成.」」」」」"); } @GetMapping(value = "/index") public String index() { return "this is user index"; } }
user-service
提供了/index
的请求地址,当访问时,会对应输出this is user index
。
接下来咱们进行验证,测试顺序以下所示:
第一步:启动Eureka Server
第二步:启动SpringCloud Gateway
启动成功后控制台会打印响应的注册到Eureka
的日志信息,以下所示:
DiscoveryClient_SPRING-CLOUD-GATEWAY/192.168.1.56:spring-cloud-gateway: registering service... Netty started on port(s): 8080
SpringCloud Gateway
内部经过Netty
完成WebServer
的请求转发。
第三步:启动user-service服务
启动成功后控制台打印相应注册日志,以下所示:
DiscoveryClient_USER-SERVICE/192.168.1.56:user-service:9090: registering service... Tomcat started on port(s): 9090 (http) with context path ''
第四步:测试访问
SpringCloud Gateway
会每间隔30秒
进行从新拉取服务列表后路由重定义操做,日志信息以下所示:
# Spring Cloud Gateway RouteDefinition CompositeDiscoveryClient_SPRING-CLOUD-GATEWAY applying {pattern=/SPRING-CLOUD-GATEWAY/**} to Path RouteDefinition CompositeDiscoveryClient_SPRING-CLOUD-GATEWAY applying filter {regexp=/SPRING-CLOUD-GATEWAY/(?<remaining>.*), replacement=/${remaining}} to RewritePath RouteDefinition matched: CompositeDiscoveryClient_SPRING-CLOUD-GATEWAY # User Service RouteDefinition CompositeDiscoveryClient_USER-SERVICE applying {pattern=/USER-SERVICE/**} to Path RouteDefinition CompositeDiscoveryClient_USER-SERVICE applying filter {regexp=/USER-SERVICE/(?<remaining>.*), replacement=/${remaining}} to RewritePath RouteDefinition matched: CompositeDiscoveryClient_USER-SERVICE
经过上面的日志信息咱们已经能够推断出SpringCloud Gateway
映射spring.application.name
的值做为服务路径前缀,不过是大写的,预计咱们能够经过http://localhost:8080/USER-SERVICE/index
访问到对应的信息。
访问测试以下:
~ curl http://localhost:8080/USER-SERVICE/index this is user index
经过网关访问具体服务的格式:http://网关IP:网关端口号/serviceId/**
若是Eureka Server
上有两个相同serviceId
的服务时,SpringCloud Gateway
会自动完成负载均衡。
复制一个user-service
服务实例,修改服务端口号
,以下所示:
# 服务名称 spring: application: name: user-service # Eureka Server eureka: client: service-url: defaultZone: http://localhost:10000/eureka/ # 服务端口号 server: port: 9091
在复制的项目内使用相同的spring.application.name
保持serviceId
一致,只作端口号的修改,为了区分GateWay
完成了负载均衡,咱们修改/index
请求的返回内容以下所示:
@GetMapping(value = "/index") public String index() { return "this is user lb index"; }
访问http://localhost:8080/USER-SERVICE/index
,输出内容以下所示:
this is user lb index this is user index this is user lb index this is user index ...
经过本章的讲解,咱们已经对SpringCloud Gateway
的转发有一个简单的理解,经过从服务注册中心拉取服务列表后,自动根据serviceId
映射路径前缀,同名服务多实例时会自动实现负载均衡。
Gitee
:https://gitee.com/hengboy/spr...
ApiBoot
:https://gitee.com/hengboy/api...