(40) spring cloud gateway integration of java Spring Cloud+Spring boot+mybatis enterprise rapid development architecture Eureka routing and forwarding

Keywords: Java Spring Spring Boot Spring Cloud

In this section, we first create a Gateway project, then implement the simplest forwarding function and integrate Eureka routing. Recommended source code of Honghu distributed Cloud Architecture

Create Gateway project
Create a Maven project of Spring Boot and increase the dependency of Spring Cloud Gateway. The code is as follows.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath />
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
</dependencies>


Start the class in the way of Spring Boot without adding additional annotations. The code is shown below.

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}


Routing forwarding example
Let's implement the simplest forwarding function - Path based matching forwarding function.

The Gateway's routing configuration supports the YML file better. We create an application.yml file under resources. The contents are as follows:

server:
  port: 2001
spring:
  cloud:
    gateway:
      routes:
        - id: path_route
uri: http://c.biancheng.net
predicates:
  - Path=/spring_cloud


When you visit http://localhost:2001/spring_cloud will be forwarded to http://c.biancheng.net/spring_cloud.

If we want to support multi-level paths, the configuration method is the same as that in Zuul. Just add two * signs after it, such as:

- id: path_route2
uri: http://c.biancheng.net
predicates:
  - Path=/spring_cloud/**


In this way, the above configuration can support multi-level paths, such as access http://localhost:2001/spring_cloud/view/1 will be forwarded to http://c.biancheng.net/spring_cloud/view/1.

Integrate Eureka routing
Add the dependency of Eureka Client. The code is as follows.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


Configure Eureka Based Routing:

- id: user-service
uri: lb://user-service
predicates:
  - Path=/user-service/**


The uri starts with lb: / / (lb stands for getting the service from the registry), followed by the service name you need to forward to. The service name must correspond to that in Eureka, otherwise the service will not be found. The error code is as follows:

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for user-service1


Integrate Eureka's default routing
Zuul will forward all services by default. We only need to specify the services to be accessed on the access path. In this way, there is no need to configure forwarding rules for each service. When a service is added, there is no need to configure routing rules and restart the gateway.

Of course, Spring Cloud Gateway also has such functions, which can be enabled through configuration. The configuration is as follows:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true

After opening, we can access the service through the address. The format is as follows:

http://Gateway address / service name (in uppercase)/**
http://localhost:2001/USER-SERVICE/user/get?id=1


This capitalized name still has a great impact. If we upgrade from Zuul to Spring Cloud Gateway, it means that the request address has changed, or the routing address of each service has been reconfigured. Through the source code, the author found that compatibility can be achieved, and then add another configuration:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true


After configuration, we can access the service name in lowercase, as shown below:

http://Gateway address / service name (lowercase)/**
http://localhost:2001/user-service/user/get?id=1


Note: after the lowercase service name is enabled, the uppercase service name cannot be used. You can only choose one of the two.

The configuration source code is in the org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties class, as shown in the code.

@ConfigurationProperties("spring.cloud.gateway.discovery.locator")
public class DiscoveryLocatorProperties {
    /**
     * The service name is configured in lowercase, and the default is false
     *
     */
    private boolean lowerCaseServiceId = false;
}


 

Posted by nick314 on Sat, 25 Sep 2021 18:34:28 -0700