Spring Cloud Gateway是spring cloud中起着很是重要的做用,是终端调用服务的入口,同时也是项目中每一个服务对外暴露的统一口径,咱们能够在网关中实现路径映射、权限验证、负载均衡、服务聚合等业务功能。spring
(一) 版本说明数组
a) Spring boot 2.0.6.RELEASEapp
b) Spring cloud Finchley.SR2负载均衡
d) spring-cloud-starter-gateway 2.0.2.RELEASEspa
(二) 项目设置code
1. Pom文件blog
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>2.0.2.RELEASE</version> </dependency>
spring:
application:
name: gateway
cloud:
gateway:
enabled: true
routes:
#终端模块
- id: clientservice
uri: lb://CLIENTSERVICE
predicates:
- Path=/client/**
filters:
- StripPrefix=1
#回调模块
- id: callbackservice
uri: lb://CALLBACKSERVICE
predicates:
- Path=/callback/**
filters:
- StripPrefix=1
a) spring.application.name 项目名称
b) spring.cloud.gateway 全部gateway配置信息的根节点
c) spring.cloud.gateway.enabled 是否启用
d) spring.cloud.gateway.routes 路由映射,注意这里是数组
e) spring.cloud.gateway.routes[0].id 标志号
f) spring.cloud.gateway.routes[0].uri 路由映射目标路径
g) spring.cloud.gateway.routes[0].predicates 匹配规则,也是暴露的映射路径
h) spring.cloud.gateway.routes[0].StripPrefix 是否包含匹配的前缀,好比 /callback/**,若是设置为1,则只有**传递到目标路径,若是设置为0,,则callback也一并传递到目标路径,通常设置为1不传递自定义的暴露服务名称
(三) 项目运行
1. 运行项目,在注册中心便可看到gateway注册进来了,以下图所示
2. 也要把咱们配置文件中配置的2个微服务已经在运行以下图所示
名称你能够改为你本身的服务名称,但记得要跟配置的映射一致。
3. 在任何一个终端输入gateway的IP:PORT/映射的服务名称/API名称,就能够看到通过网关映射后的效果
a) Client服务
b) Callback服务
这样spring cloud gateway网关就介绍完了,若是在开发中遇到问题,也能够留言共同探讨共同进步。