SpringCloud Gateway simple application

One: Create a eureka-zuul-client services

1.1 Module to create a new project in the main Maven project, named eureka-gateway-client. Create a way of using Spring Initializr way.
Here Insert Picture Description
1.2 eureka-gateway-client pom.xml content is as follows:

<?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>

Pom.xml 1.3 Main Module of plus:
Here Insert Picture Description
1.4 on startup class service registration plus @EnableDiscoveryClient open to discovery, as follows:

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 application.yml content profile of eureka-gateway-client as follows:

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 SUMMARY remaining eight profiles are as follows:

  1. application-predicate-path.yml Path Routing assertion
    receiving a path match parameter to determine whether the walk.
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/
  1. application-predicate-timedate.yml time routing request asserted
    Predicate support a set time, when forwarding the request may be forwarded before or between or after this time is determined by
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/
  1. application-predicate-host.yml host routing assertion
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/
  1. application-predicate-header.yml header routing assertion
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/
  1. application-predicate-cookie.yml cookie routing assertion
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/
  1. application-predicate-method.yml method assertions routing
    can be routed through POST, GET, PUT, DELETE and the like different request
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/
  1. application-predicate-queryParam.yml assertion routing request query parameters
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/
  1. application-predicate-remoteAddr.yml ip address routing requests asserted by
    Predicate will also support the request by providing a routing number segment interval ip, e.g. 192.168.0.1/16 (where the IP address is 192.168.0.1, is the subnet mask 16 code).
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/
  1. Combined Routing using assertions 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 Start eureka-serve, eureka-client (8762,8763 ports), eureka-gateway-client, service, browser to access http: // localhost: 8761 /
Here Insert Picture Description

1.8 Test

1.predicate-path Path Routing assertions
using curl test, command line, type:

curl http://localhost:8769/HiController/aaa

3.pridicate-host host routing assertions
using curl test, command line, type:

curl http://localhost:8769/HiController/aaa  -H "Host: www.host.test" 

4.pridicate-header header routing assertions
using curl test, command line, type:

curl http://localhost:8769/HiController/aaa  -H "X-Request-Hello:World"

5.pridicate-cookie cookie routing assertions
using curl test, command line, type:

curl http://localhost:8769/HiController/aaa --cookie "Hello=World"

6.pridicate-method method routing assertions
using curl test, command line, type:

# curl 默认是以 GET 的方式去请求
curl -X POST http://localhost:8769/HiController/aaa

7.pridicate-queryParam # assertion routing request query parameters
using curl test, command line, type:

curl localhost::8769/HiController/aaa?a=b

8.pridicate-remoteAddr assertions by the request route ip address
test can set this native address 192.168.0.1 ip address.

curl localhost::8769/HiController/aaa

The results are shown:
Here Insert Picture Description

The next article to learn gateway of Filter (filter)

References:

https://blog.csdn.net/qq_42815754/article/details/94622244
http://www.ityouknow.com/springcloud/2018/12/12/spring-cloud-gateway-start.html

Published 50 original articles · won praise 75 · views 8653

Guess you like

Origin blog.csdn.net/weixin_40991408/article/details/104161424