Spring Cloud Micro Service Architecture From Getting Started to Getting Used - Service Gateway Spring Cloud Gateway

Keywords: Programming Spring Java WebFlux Nginx

In the first two articles, we have preliminarily completed a small micro-service framework, including a service registry, an order service, and an inventory service. Order services can also make inter-service invocations of inventory services through feign.In that article, we will introduce the service gateway Spring Cloud Gateway.

Spring Cloud Gateway is designed to provide a simple and effective way to route to the API.Spring Cloud Gateway is built on Spring Framework 5, Spring Boot 2.0.Spring Cloud Gateway was developed by Spring to replace Zuul.Spring Cloud Gate is implemented based on WebFlux of Spring Framework 5.

Refer to this article for a comparison between Zuul and Spring Cloud Gateway: https://www.cnblogs.com/yizhishi/archive/2019/09/26/11588860.html

Next we'll start introducing the Spring Cloud Gateway.

1. Create a gateway service module

Follow the second article to create a module named server-gateway.

2. Modify the pom file to introduce gateway

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <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>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Here we also register gateway with eureka.

3. Modify Startup Class

@SpringBootApplication
@EnableEurekaClient
public class ServerGatewayApplication {

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

}

All you need to do here is add a comment from the eureka client

4. Modify the configuration file

server.port=10010
spring.application.name=server-gateway

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.instance-id=${spring.application.name}:${server.port}

# Routing to app-storage service
spring.cloud.gateway.routes[0].id=app-storage
spring.cloud.gateway.routes[0].uri=lb://APP-STORAGE
spring.cloud.gateway.routes[0].predicates[0]=Path=/storage/v1/**
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=2
# Routing to app-order service
spring.cloud.gateway.routes[1].id=app-order
spring.cloud.gateway.routes[1].uri=lb://APP-ORDER
spring.cloud.gateway.routes[1].predicates[0]=Path=/order/v1/**
spring.cloud.gateway.routes[1].filters[0]=StripPrefix=2

Here we will focus on how to configure routing, taking app-storage as an example.

Okay, here we can access app-store and app-order services through the gateway

5. Accessing services through the gateway

Start server-eureka, server-gateway, app-storage, app-order in turn

Visit http://127.0.0.1:10010/order/v1/order/placeOrder/commit , as we wrote in the last article.Because we added the request prefix order/v1, we add it here.

If true is returned, the gateway is successfully configured.

If the gateway is only used for routing, it is not necessary to use Spring Cloud Gateway. nginx can do it. Routing is only the basic instinct of the gateway. Based on the gateway, we can do interface authentication, flow restriction, etc.Next article we'll talk about how to authenticate through a gateway

Posted by daddymac on Mon, 23 Mar 2020 10:59:24 -0700