Reprint please indicate source: www.fangzhipeng.com This article comes from Fang Zhipeng’s blog

In the previous article, WE introduced Spring Cloud Gateway Predict and Filter. We have a preliminary understanding of Spring Cloud Gateway. In the previous article, hard-coded routing and forwarding was adopted. This article presents a case study of how the Spring Cloud Gateway works with a service registry for routing and forwarding.

Project introduction

In this case, the spring Boot version is 2.0.3.RELEASE, and the Spring Cloud version is finchley.release. Three projects are involved in, namely registry Eureka-server, service provider service-Hi and service gateway service-gateway, as follows:

Project name port role
eureka-server 8761 Registry Eureka Server
service-hi 8762 Service provider Eurka Client
service-gateway 8081 Route gateway Eureka Client

Among the three projects, service-Hi and service-gateway register with the registry Eureka-server. The user’s request first passes through the service-gateway, and the gateway predict determines which router to enter according to the path. The router is processed by various filters, and finally routes to specific services, such as service-Hi. As shown in figure:

Eureka – server, these two service – hi engineering directly copied from my another article, https://blog.csdn.net/forezp/article/details/81040925, in not to repeat, you can view the source code, Source address see the end of the link. The service-hi service exposes a RESTFUL interface/HI. Now we’ll focus on service-gateway.

Gateway Project details

To introduce the dependencies required by the project into the Gateway project, including eureka-client startup dependencies and Gateway startup dependencies, the code is as follows:

<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>
Copy the code

In the configuration file application.yml of the project, the startup port of the program is specified as 8081, the registered address, gateway configuration and other information, and the configuration information is as follows:

server:
  port: 8081

spring:
  application:
    name: sc-gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
          
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

Copy the code

Among them, the spring. Cloud. Gateway. Discovery. A locator. Enabled to true, indicates that the function of the open gateway service registration and discovery, And the Spring Cloud Gateway automatically creates a router for each service based on service discovery, which forwards the request path starting with the service name to the corresponding service. Spring. Cloud. Gateway. Discovery. A locator. LowerCaseServiceId is configured to service the request path name lowercase (because the service registry, to the registry when the service name to uppercase). For example, a request path with /service-hi/* is routed to a service named service-hi.

Ask for localhost:8081/service-hi/hi? Name =1323, the page gets the following response:

hi 1323 ,i am from port:8762

Copy the code

In the example above, the request url sent to gateway-service must be prefixed with the service name service-hi before it can be forwarded to service-hi. Service-hi is removed before forwarding. Can I customize the request path? After all, the request path is sometimes too long according to the service name, or historical reasons cannot be routed according to the service name, so I need to customize the path and forward to the specific service. The answer is yes, you only need to modify the project configuration file application.yml, the specific configuration is as follows:

spring:
  application:
    name: sc-gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
          lowerCaseServiceId: true
      routes:
      - id: service-hi
        uri: lb://SERVICE-HI
        predicates:
          - Path=/demo/**
        filters:
          - StripPrefix=1
         

Copy the code

As described in the previous configuration, the Path as /demo/** predict will forward the requests starting with /demo/** to the lb:// service-hi URI. Lb :// service-hi is the load-balancing address of the service-hi SERVICE. Use StripPrefix filter to remove /demo before forwarding. At the same time will spring. Cloud. Gateway. Discovery. A locator. Enabled to false, if you don’t change, before the localhost: 8081 / / service – hi hi? A request address such as name=1323 is also accessible because two routers are created for each service.

Request localhost:8081/demo/hi? Name =1323, the browser returns the following response:

hi 1323 ,i am from port:8762

Copy the code

The results returned were what we expected.

Download the source code

Github.com/forezp/Spri…

The resources

Juejin. Cn/post / 684490…

www.jianshu.com/p/1c942a8ab…

Juejin. Cn/post / 684490…



Scan and support the author

(Please indicate the author and source of the article reproduced from the blog of Fang Zhipeng)