1.1 在主Maven工程中建立一个新的 Module 工程,命名为eureka-gateway-client。采用Spring Initializr 的方式的方式建立。
1.2 eureka-gateway-client 的 pom.xml 的内容以下:html
<?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 开启服务注册于发现,代码以下:java
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 的内容以下:web
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个配置文件 的 内容 分别以下:正则表达式
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/
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/
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/
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/
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/
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/
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/
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/
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/
spring
1.8 测试apache
1.predicate-path Path 路由断言
使用 curl 测试,命令行输入:浏览器
curl http://localhost:8769/HiController/aaa
3.pridicate-host host路由断言
使用 curl 测试,命令行输入:cookie
curl http://localhost:8769/HiController/aaa -H "Host: www.host.test"
4.pridicate-header header路由断言
使用 curl 测试,命令行输入:app
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
结果如图:
https://blog.csdn.net/qq_42815754/article/details/94622244 http://www.ityouknow.com/springcloud/2018/12/12/spring-cloud-gateway-start.html