How Spring Cloud Gateway works
The client makes a request to Spring Cloud Gateway and, if the request matches the route defined by the gateway program, sends it to the Gateway Web handler, which runs a specific request-filter chain.
The reason the filters are separated by dashes is that the filter may execute logic before or after sending the proxy request. All "Pre" filter logic is executed first, then the proxy request is executed, and after the proxy request is completed, the "POST" filter logic is executed.
How to start Spring Cloud Gateway
1, New Maven project, add related dependenciespom.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 forMiroservice</description> <properties> <project.build.sourceencoding>utf-8</project.build.s Ourceencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> < ;java.version>1.8</java.version> </properties> <dependencyManagement> <DEPENDENCIES&G T <dependency> <groupId>org.springframework.cloud</groupId> <ARTIFACTID&G T;spring-cloud-gateway</artifactid> <version>2.0.0.RELEASE</version> <t Ype>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> <depende ncies> <dependency> <groupId>org.springframework.cloud</groupId> <art ifactid>spring-cloud-starter-gateway</artifactid> </dependency> </dependencies> <buil d> <plugins> <plugin> <groupid>org.springframework.boot</groupid& Gt <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </b Uild></project>
2. Add 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 { publicstaticvoid main (string[] args) { Springapplication.run (Application. class , args);} }
3. Start application (same as Spring boot project)
Access http://localhost:8080/error 404, while the 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
Configure Routing for services: How to configure Files
Assume that another two spring boot services are started locally, namely service A (http://localhost:8081), service B (http://localhost:8082), which is routed through Spring Cloud Gateway to these two Service.
1. Add the configuration file under the resources path APPLICATION.YML
Spring: Cloud: Gateway: routes: - id:host_route uri:http:// localhost:8081 predicates: -path=/a/** Filters: -Stripprefix =1 -Id:host_route http://localhost: 8082 predicates: -path=/b/** filters: -Stripprefix=1
ID: fixed, different ID corresponding to different functions, can refer to the official document
URI: Destination Service Address
Predicates: Routing Conditions
Filters: Filtering rules
2. Restart the Gateway service
3. Testing
Access http://localhost:8080/a/routing to service a http://localhost:8081/
Access http://localhost:8080/b/routing to service B http://localhost:8082/
Other addresses, such as Http://localhost:8080/a/user/all routing to service a Http://localhost:8081/user/all
Configure Routing For Services: Encoding method
The implementation of the above service routing can also be achieved by encoding.
1. Delete the configuration file application.yml
2. Modify Application.java to add custom routing configuration
Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;ImportOrg.springframework.cloud.gateway.route.RouteLocator;ImportOrg.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;ImportOrg.springframework.context.annotation.Bean; @SpringBootApplication Public classApplication {@Bean Publicroutelocator customroutelocator (Routelocatorbuilder builder) {Stripprefixgatewayfilterfactory.config Config /c7>=NewStripprefixgatewayfilterfactory.config (); Config.setparts (1); returnbuilder.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 voidMain (string[] args) {springapplication.run (application.class, args); }}
Other features
Http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html
The official provides a number of routing rules, such as time, Host, Header and so on, but also provides a large number of filters, such as Addrequestheader, Addrequestparameter, Addresponseheader and so on. Powerful gateway services can be implemented with a simple configuration.
Recommended reading: https://www.roncoo.com/course/view/ad054a612db54315927a232ea722a03b
Article Source: Https://www.jianshu.com/p/1c942a8abe18?utm_source=desktop&utm_medium=timeline
Using the gateway under MicroServices Spring Cloud Gateway