一:创建 eureka-zuul-client 服务
1.1 在主Maven工程中创建一个新的 Module 工程,命名为eureka-gateway-client。采用Spring Initializr 的方式的方式创建。
1.2 eureka-gateway-client 的 pom.xml 的内容如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-hx</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-gateway-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-gateway-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3 主Module 的 的 pom.xml 加上:
1.4 在启动类上加上 @EnableDiscoveryClient 开启服务注册于发现,代码如下:
package com.example.eurekagatewayclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//开启服务注册于发现
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaGatewayClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaGatewayClientApplication.class, args);
}
}
1.5 eureka-gateway-client 的 配置文件 application.yml 的内容如下:
spring:
#使用哪个配置文件
profiles:
active: predicate-path # Path 路由断言
#predicate-path # Path 路由断言
#pridicate-timedate # 请求时间路由断言
#pridicate-host # host路由断言
#pridicate-header # header路由断言
#pridicate-cookie # cookie路由断言
#pridicate-method # method路由断言
#pridicate-queryParam # 请求查询参数路由断言
#pridicate-remoteAddr # 通过请求ip地址路由断言
1.6 其余8个配置文件 的 内容 分别如下:
- application-predicate-path.yml Path 路由断言
接收一个匹配路径的参数来判断是否走路由。
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-path #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client
#uri: http://localhost:8762
#断言
predicates:
#表示将以/HiController开头的请求转发到uri为lb://eureka-client的地址上
#转发地址格式为 uri/HiController/**
- Path=/HiController/**
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-timedate.yml 请求时间路由断言
Predicate 支持设置一个时间,在请求进行转发的时候,可以通过判断在这个时间之前或者之后或者之间进行转发
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-timedate #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#表示在该时间点之后的时间,发出的请求会被路由到uri
- After=2020-02-03T00:00:00.789-07:00[America/Denver]
#表示在该时间点之前的时间,发出的请求会被路由到uri
#- Before=2020-02-03T00:00:00.789-07:00[America/Denver]
#表示在该时间点之前的时间,发出的请求会被路由到uri
#- Before=2020-02-03T00:00:00.789-07:00[America/Denver]
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-host.yml host路由断言
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-host #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#表示当请求带有host为**.host.test时,发出的请求会被路由到uri
- Host=**.host.test
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-header.yml header路由断言
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-header #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#表示当请求的请求头中有 key=Hello,value=World,发出的请求会被路由到uri
- Header=Hello, World
#可以是正则表达式 例如 - Header=Hello, \d+
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-cookie.yml cookie路由断言
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-cookie #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#表示当请求带有名为Hello,值为World的Cookie时,发出的请求会被路由到uri
- Cookie=Hello, World
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-method.yml method路由断言
可以通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-method #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#表示GET请求,都会被路由到uri
- Method=POST
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-queryParam.yml 请求查询参数路由断言
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-queryParam #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#表示请求带有参数key=a, value=b时,该请求会被路由到uri
- Query=a, b
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- application-predicate-remoteAddr.yml 通过请求ip地址路由断言
Predicate 也支持通过设置某个 ip 区间号段的请求才会路由,例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-remoteAddr #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client/
#uri: http://localhost:8762
#断言
predicates:
#Predicate 也支持通过设置某个 ip 区间号段的请求才会路由,RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1),
#例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。
- RemoteAddr=192.168.0.1/16
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
- 组合使用的路由断言 application-predicate-combination.yml
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-path #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client
#uri: http://localhost:8762
#断言
predicates:
- Host=**.host.test
- Path=/HiController/**
- Method=GET
- Header=X-Request-Id, \d+
- Query=foo, ba.
- Query=baz
- Cookie=chocolate, ch.p
- After=2018-01-20T06:06:06+08:00[Asia/Shanghai]
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
1.7 启动 eureka-serve, eureka-client (8762,8763 端口),eureka-gateway-client ,服务,浏览器访问 http://localhost:8761/
1.8 测试
1.predicate-path Path 路由断言
使用 curl 测试,命令行输入:
curl http://localhost:8769/HiController/aaa
3.pridicate-host host路由断言
使用 curl 测试,命令行输入:
curl http://localhost:8769/HiController/aaa -H "Host: www.host.test"
4.pridicate-header header路由断言
使用 curl 测试,命令行输入:
curl http://localhost:8769/HiController/aaa -H "X-Request-Hello:World"
5.pridicate-cookie cookie路由断言
使用 curl 测试,命令行输入:
curl http://localhost:8769/HiController/aaa --cookie "Hello=World"
6.pridicate-method method路由断言
使用 curl 测试,命令行输入:
# curl 默认是以 GET 的方式去请求
curl -X POST http://localhost:8769/HiController/aaa
7.pridicate-queryParam # 请求查询参数路由断言
使用 curl 测试,命令行输入:
curl localhost::8769/HiController/aaa?a=b
8.pridicate-remoteAddr 通过请求ip地址路由断言
可以将此地址192.168.0.1 设置为本机的 ip 地址进行测试。
curl localhost::8769/HiController/aaa
结果如图:
下个文章学习 gateway 的 Filter(过滤器)
参考 资料:
https://blog.csdn.net/qq_42815754/article/details/94622244
http://www.ityouknow.com/springcloud/2018/12/12/spring-cloud-gateway-start.html