GateWay 网关服务,java 程序员进阶路线
GateWay 核?逻辑:路由转发+执?过滤器链。
[](
)GateWay 应?
GateWay 不需要使? web 模块,它引?的是 WebFlux(类似于 SpringMVC)。
[](
)依赖
pom.xml 文件如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
[](
)配置
server:
port: 9002
eureka:
client:
serviceUrl: # eureka server 的路径
defaultZone: http://democloudeurekaservera:8761/eureka/,http://democloudeurekaserverb:8762/eureka/ #把 eureka 集群中的所有 url 都填写了进来,也可以只写一台,因为各个 eureka server 可以同步注册表
instance:
#使用 ip 注册,否则会使用主机名注册了(此处考虑到对老版本的兼容,新版本经过实验都是 ip)
prefer-ip-address: true
#自定义实例显示格式,加上版本号,便于多版本管理,注意是 ip-address,早期版本是 ipAddress
instance-id: {spring.application.name}:${server.port}:@project.version@
spring:
application:
name: lagou-cloud-gateway
cloud:
gateway:
routes: # 路由可以有多个
id: service-autodeliver-router # 我们自定义的路由 ID,保持唯一
#uri: http://127.0.0.1:8096 # 目标服务地址 自动投递微服务(部署多实例) 动态路由:uri 配置的应该是一个服务名称,而不应该是一个具体的服务实例的地址
uri: lb://demo-service-autodeliver # gateway 网关从服务注册中心获取实例信息然后负载后路由
predicates: # 断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
Path=/autodeliver/**
id: service-resume-router # 我们自定义的路由 ID,保持唯一
#uri: http://127.0.0.1:8081 # 目标服务地址
#http://localhost:9002/resume/openstate/1545132
#http://127.0.0.1:8081/openstate/1545132
uri: lb://demo-service-resume
断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
predicates:
Path=/resume/**
filters:
StripPrefix=1
上?这段配置的意思是,配置了?个 id 为 service-autodeliver-router 的路由规则,当向?关发起
请求
http://localhost:9002/autodeliver/checkAndBegin/1545132
请求会被分发路由到对应的微服务上。
[](
)GateWay 路由规则详解
Spring Cloud GateWay 帮我们内置了很多 Predicates 功能,实现了各种路由匹配规则(通过 Header、请求参数等作为条件)匹配到对应的路由。
时间点后匹配
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
After=2017-01-20T17:42:47.789-07:00[America/Denver]
时间点前匹配
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
Before=2017-01-20T17:42:47.789-07:00[America/Denver]
时间区间匹配
spring:
cloud:
gateway:
routes:
id: after_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]
指定 Cookie 正则匹配指定值
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
Cookie=chocolate, ch.p
指定 Header 正则匹配指定值
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
Header=X-Request-Id, \d+
请求 Host 匹配指定值
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
Host=.somehost.org,.anotherhost.org
请求 Method 匹配指定请求?式
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
Method=GET,POST
请求路径正则匹配
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
predicates:
Path=/red/{segment},/blue/{segment}
请求包含某参数
spring:
cloud:
gateway:
routes:
id: after_route
uri: https://example.org
评论