Spring Cloud Gateway - Quick Start

Spring Cloud Gateway works

The client makes a request to Spring Cloud Gateway, if the request is routed to the gateway program defined matching, which will be sent to the gateway Web handler, the request handler runs a specific filter chain.

Cause between the filter by the broken line is a filter may perform logic before sending the request or after the agent. All "pre" logic to perform the filter, and then request execution agent, the agent after the completion of the request, perform "post" filter logic.

How to Start Spring Cloud Gateway

1, the new Maven project, add its dependencies pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.anoyi</groupId>
    <artifactId>core-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>core-gateway</name>
    <description>gateway for miroservice</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gateway</artifactId>
                <version>2.0.0.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>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>复制代码

2, add the startup class Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication
public class Application {

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

}复制代码

3. Start Application (Spring Boot project and the same)

Access http://localhost:8080/error 404, while log output:

2018-06-27 09:18:48.981  WARN 44156 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : 
Failed to handle request [GET http://localhost:8080/]: Response status 404复制代码

Configuring Routing Service: Profile mode

Suppose a local Spring Boot initiated two additional services are service A (http: // localhost: 8081), the service B (http: // localhost: 8082), the following services to both routed through Spring Cloud Gateway.

1, in the add path to the configuration fileresourcesapplication.yml

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://localhost:8081
        predicates:
        - Path=/a/**
        filters:
        - StripPrefix=1
      - id: host_route
        uri: http://localhost:8082
        predicates:
        - Path=/b/**
        filters:
        - StripPrefix=1复制代码

  • id: fixed, different id corresponding function, refer to the official documentation
  • uri: target service address
  • predicates: routing conditions
  • filters: filtering rules

2, restart the Gateway service

3, the test

Access http://localhost:8080/a/routes to the service Ahttp://localhost:8081/

Access http://localhost:8080/b/routes to the service Bhttp://localhost:8082/

Other addresses, such as http://localhost:8080/a/user/allthe route to the service Ahttp://localhost:8081/user/all

Configuring Routing Service: encoding

As implement service routing, it may also be implemented by coding.

1, delete the profile application.yml

2, modify , add custom routing configurationApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();
        config.setParts(1);
        return builder.routes()
                .route("host_route", r -> r.path("/a/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8081"))
                .route("host_route", r -> r.path("/b/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8082"))
                .build();
    }

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

}复制代码

Other functions

http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html

The official provided a large number of routing rules, such as Time, Host, Header etc., but also provide a large number of filters, such as AddRequestHeader, AddRequestParameter, AddResponseHeader and so on. It can achieve a powerful gateway service only by a simple configuration.

© copyright reserved by the authors, reprint please contact the author or content partners

img

APM tool looking around and found SkyWalking is my true love

the Spring external configuration injected into the interior of the Boot application of static variables

The HTML into PDF new posture

the Java Docker API calls using UnixSocket

FASTJSON fatal flaw

Service Mesh - GRPC local FBI remote service

Use Thymeleaf dynamic rendering HTML

FASTJSON fatal flaw

the Spring integration log4j2日志框架the Boot 2

core Java chapter sets off point summary of the interview on Answers

framework for Java interview clearance papers set of reference points are summarized answers

the Spring Security combat dry goods: how to protect user passwords

the Spring the Boot RabbitMQ - priority queue

This article from the blog article multiple platforms OpenWrite release!

Guess you like

Origin juejin.im/post/5db7138751882564011cc774