Spring Cloud Gateway Introduction to use

Spring Cloud Gateway is similar Nginx gateway routing agent, you have to replace the original Spring cloud zuul meaning:

Spring 5 launched its own Spring Cloud Gateway, supports Java 8, Reactor API, can use at Spring Boot 2, saw the responsive components Reactor, one can understand the objectives of the program are the gateway can be achieved using Reactive efficient gateway.

Want to create a Spring Cloud Gateway, then, on the Spring Tool Suite you can select the "Gateway" The Starter, in order to register to the service discovery server, and to be able to open gateway / routes endpoints, in order to observe the routing information, on the way to join Eureka and Actuator the Starter, such as may be contained in the build.gradle:

  

implementation('org.springframework.boot:spring-boot-starter-actuator')  
implementation('org.springframework.cloud:spring-cloud-starter-gateway')
implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

 

Spring Cloud Gateway can be registered in the registration server service ID, automatically establishes the routing information, for which you can set as follows bootstrap.properties:

  

server.port=5555

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true

eureka.instance.preferIpAddress=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

management.endpoints.web.exposure.include: gateway

 

spring.cloud.gateway.discovery.locator.enabled enabled automatically create a path routing, routing based on service ID corresponding to the ID can use uppercase, lowercase ID if you want to use, can be set up spring.cloud.gateway.discovery.locator.lowerCaseServiceId It is true; in the settings also open the gateway endpoint. If necessary, you can use RouteLocator implement custom routing way.

 

Then start the relevant service, start Spring Cloud Gateway, the default will run in Netty, if the test request http: // localhost: 5555 / actuator / gateway / routes, then you can see the following:

 

 

[
    {
        "route_id": "CompositeDiscoveryClient_ACCTSVI",
        "route_definition": {
            "id": "CompositeDiscoveryClient_ACCTSVI",
            "predicates": [
                {
                    "name": "Path",
                    "args": {
                        "pattern": "/acctsvi/**"
                    }
                }
            ],
            "filters": [
                {
                    "name": "RewritePath",
                    "args": {
                        "regexp": "/acctsvi/(?<remaining>.*)",
                        "replacement": "/${remaining}"
                    }
                }
            ],
            "uri": "lb://ACCTSVI",
            "order": 0
        },
        "order": 0
    },
    ...
]

 

Each route will have a route_id set as identification, routing defined predicates, you can see the set Path, which is Spring Cloud Gateway built-in assertions factory Bean name, pattern this setting means that for http: // localhost : 5555 / acctsvi / xxxx request will be forwarded to the set value uri: lb: // ACCTSVI, that is transferred to the routing service ID for the ACCTSVI service.

 

filters set in RewritePath, this is a filter factory Bean name, in accordance with the rules of regexp, it will capture part of the request / acctsvi / after, apply to the URI of the service, which is http: // localhost: 5555 / acctsvi / xxxx request will be routed forwarded to http: // acctsvi-uri / xxxx.

 

predicates and filters are important characteristics of Spring Cloud Gateway, which paths predicates assert in line with the definition of routing, filters set specific what specific path filter which applies, in addition to the set, if necessary, you can define the code yourself.

 

Spring Cloud Gateway also built a number of assertions factory and filter factory , these factories category, it can be defined by file attributes, if necessary, can also be custom factory class .

 

For the above settings, the request http: // localhost:? 5555 / acctsvi / accountByName username = caterpillar can get the following response:

 

 

{
    "name": "caterpillar",
    "email": "[email protected]",
    "password": "$2a$10$CEkPOmd.Uid2FpIOHA6Cme1G.mvhWfelv2hPu7cxZ/vq2drnXaVo.",
    "_links": {
        "self": {
            "href": "http://Justin-2017:8084/accountByNameEmail?username=caterpillar"
        }
    }
}

If you want to customize the route, you can write application.yml (if we are not automatically establish a route, you can spring.cloud.gateway.discovery.locator.enabled with spring.cloud.gateway.discovery.locator.lowerCaseServiceId commented out):

 

 

spring:
    application:
            name: gateway
    cloud:
        gateway:
            routes: 
                - predicates:
                    - Path=/acct/**
                  filters:
                      - StripPrefix=1
                  uri: lb://acctsvi
                - predicates:
                    - Path=/msg/**
                  filters:
                      - StripPrefix=1
                  uri: lb://msgsvi     
                - predicates:
                    - Path=/email/**
                  filters:
                      - StripPrefix=1
                  uri: lb://email         

 

上述配置filters中的StripPrefix也是内建的过滤器工厂Bean名称,设定值为1表示将路由中的第一个层去除,其余保留用来转发请求,请求http://localhost:5555/actuator/gateway/routes的话,就可以看到以下:

 

 

[
    {
        "route_id": "545d278b-192b-4370-8156-161815957f91",
        "route_definition": {
            "id": "545d278b-192b-4370-8156-161815957f91",
            "predicates": [
                {
                    "name": "Path",
                    "args": {
                        "_genkey_0": "/acct/**"
                    }
                }
            ],
            "filters": [
                {
                    "name": "StripPrefix",
                    "args": {
                        "_genkey_0": "1"
                    }
                }
            ],
            "uri": "lb://acctsvi",
            "order": 0
        },
        "order": 0
    },
    ...
]

 

也就是对http://localhost:5555/acct/accountByName?username=caterpillar的请求,会转给http://acctsvi-url/accountByName?username=caterpillar。

 

如果想要设定api前置路径,就是修改一下StripPrefix=1为StripPrefix=2:

 

 

spring:
    application:
            name: gateway
    cloud:
        gateway:
            default-filters:
                - StripPrefix=2
            routes: 
                - predicates:
                    - Path=/api/acct/**
                  uri: lb://acctsvi
                - predicates:
                    - Path=/api/msg/**
                  uri: lb://msgsvi     
                - predicates:
                    - Path=/api/email/**
                  uri: lb://email               

 

对于每个路由都要套用的过滤器,可以使用default-filters来设置,就以上设定来说,可以请求http://localhost:5555/api/acct/accountByName?username=caterpillar来取得使用者信息。

一开始自动根据服务ID建立路由时,可以看到RewritePath,它也是内建的过滤器工厂,可以运用规则表示式来进行路径重写,因此,也可以这么设置api前置:

 

 

spring:
    application:
            name: gateway
    cloud:
        gateway:
            default-filters:
                - RewritePath=/api/.*?/(?<remaining>.*), /$\{remaining}
            routes: 
                - predicates:
                    - Path=/api/acct/**
                  uri: lb://acctsvi
                - predicates:
                    - Path=/api/msg/**
                  uri: lb://msgsvi     
                - predicates:
                    - Path=/api/email/**
                  uri: lb://email            

 

就目前的设定来说,在客户端的部份,〈使用Zuul〉中的gossip就可以了,毕竟交互的接口没有改变,但是因为使用spring.application.gateway作为应用代理了,还是要记得改一下@FeignClient中的服务ID为gateway。

可以在首页qq群加群获取代码

Guess you like

Origin www.cnblogs.com/zhaokejin/p/11496546.html