环境 SpringCloud:Finchley.RELEASE SpringBoot:2.0.0.RELEASE JDK:1.8
1. 核心概念
- 路由(route) 路由是网关中最基础的部分,路由信息包括一个ID、一个目的URI、一组断言工厂、一组Filter组成。如果断言为真,则说明请求的URL和配置的路由匹配。
- 断言(predicates) Java8中的断言函数,SpringCloud Gateway中的断言函数类型是Spring5.0框架中的ServerWebExchange。断言函数允许开发者去定义匹配Http request中的任何信息,比如请求头和参数等。
- 过滤器(Filter) SpringCloud Gateway中的filter分为Gateway FilIer和Global Filter。Filter可以对请求和响应进行处理。
2. 入门案例
2.1 搭建环境
- 创建工程导入坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- 配置启动类
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
- 编写配置文件
spring:
application:
name: microservice-gateway
cloud:
gateway:
routes: #设置路由:路由id、路由打微服务的uri、断言
- id: microservice-provider #路由ID,全局唯一
uri: http://127.0.0.1:8001 #目标微服务的请求地址和端口
predicates:
- Path=/provider/** #路由条件,采用path路由规则
2.2 路由规则
内置断言:
2.2.1 The after route predicate
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
2.2.2 the before route predicate
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
2.2.3 the between route predicate
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
2.2.4 the cookie route predicate
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
2.2.5 the header route predicate
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
2.2.6 the host route predicate
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
2.2.7 the method route predicate
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
2.2.8 the path route predicate
Http请求路径匹配
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
2.2.9 the query route predicate
http请求参数匹配
- 请求参数包含green
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
- 请求参数reg正则匹配gree.
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
2.2.10 the remoteaddress route predicate
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
2.2.11 the weight route predicate
根据权重负载
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
2.3 动态路由
- 导入坐标
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 修改启动类(可以不修改,eureka client配置application.yml的注册中心地址默认会开启服务发现)
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
- 修改配置文件
spring:
application:
name: microservice-gateway
cloud:
gateway:
routes: #设置路由:路由id、路由打微服务的uri、断言
- id: microservice-provider #路由ID,全局唯一
# uri: http://127.0.0.1:8001 #目标微服务的请求地址和端口
uri: lb://microservice-provider # lb://微服务名 根据微服务名称从注册中心获取地址
predicates:
- Path=/provider/** #路由条件,采用path路由规则
##eureka配置信息
eureka:
client:
service-url: #注册中心地址列表
defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/
instance:
instance-id: microservice-gateway6101 # 自定义控制台中微服务名称
prefer-ip-address: true #eureka控制访问路径显示ip
#解决eureka控制台中的Info页面错误问题
info:
app.name: com.xyz.microservice
build.artifactId: $project.artifactId$ #使用maven内置变量project.artifactId和project.version完成对变量的赋值
build.version: $project.version$
2.4 重写转发路径
通过RewritePath的过滤器,实现把请求路径重写成实际后端微服务的地址
spring:
application:
name: microservice-gateway
cloud:
gateway:
routes: #设置路由:路由id、路由打微服务的uri、断言
- id: microservice-provider #路由ID,全局唯一
# uri: http://127.0.0.1:8001 #目标微服务的请求地址和端口
uri: lb://microservice-provider # lb://微服务名 根据微服务名称从注册中心获取地址
predicates:
- Path=/product/** #路由条件,采用path路由规则
filters: # 实现重写转发路径: http://localhost:6101/product/provider/list --> http://localhost:6101/provider/list
- RewritePath=/product/(?<segment>.*), /$\{segment}
2.5 设置通过微服务名称转发
spring.cloud.gateway.discovery.locator.enabled=true开启后默认通过微服务名称进行匹配
spring:
application:
name: microservice-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启通过微服务名称默认进行转发规则:如http://localhost:6101/microservice-provider/provider/list
lower-case-service-id: true #微服务名称通过小写的方式请求
todo 开启spring.cloud.gateway.discovery.locator.enabled=true,启动服务报错