This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

Springcloud GateWay Service GateWay

Gateway profile

Spring Cloud Gateway is the second generation Gateway framework officially launched by Spring Cloud, replacing Zuul Gateway. Common functions of gateways include routing and forwarding, permission verification, and traffic limiting control.

SpringCloud Gateway is implemented based on the WebFlux framework, and the Underlying layer of the WebFlux framework uses the high performance Reactor mode communication framework Netty.

Compare Spring Cloud, Netflix, Zuul and Spring Cloud Gateway

Compared with Zuul, Gateway relies more on Spring-WebFlux. With the support of Spring, gateway has more powerful functions, internal flow limiting, load balancing, etc., and stronger scalability. But at the same time, it is limited to spring Cloud suite, while Zuul can be extended to other microservice frameworks. Gateway supports asynchrony very well, while Zuul only supports synchronization

features

  • Based on Spring 5, Reactor(mode) and SpringBoot 2.0
  • Can match routes on any request attribute
  • Assertions and filters are routing specific
  • Hystrix circuit breaker integrated
  • SpringCloud DiscoveryClient integration
  • Easy to write assertions and filters
  • Request rate limit
  • The path to rewrite

Three core concepts

  • Route (Route)Routing is the basic building block of a gateway. It consists of an ID, a target URI, a series of assertions and filters. If the request matches the assertion, it is routed
  • Predicate (assertions): reference is java8 Java. Util. The function. The Predicate, the developer can match all the content in the HTTP request (for example, request or request parameters), if the request is match the assertion is routed
  • Filter (Filter): refers to an instance of a GatewayFilter in the Spring framework that allows you to modify a request before or after it is routed.

use

Create the module

It mainly relies on the Spring-cloud-starter -gateway and the Spring-cloud-starter – Netflix-Eureka-client

pom.xml

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.5.2 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>gateway</artifactId> <version>0.0.1 -snapshot </version> <name>gateway</name> <description>Demo project for gateway</ description> <properties> < Java version > 1.8 < / Java version > < spring - cloud. Version > 2020.0.3 < / spring - cloud. Version > < / properties > < dependencies > <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> < artifactId > maven - surefire plugin < / artifactId > < version > 2.22.2 < / version > < configuration > < the skip > true < / skip > </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> </artifactId> </artifactId> <version>3.1.0</version> </plugin> </build> </project>Copy the code

Startup class annotation

Add @ EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
​
}
Copy the code

Configuration yml

application.yml

server: port: 9000 spring: cloud: gateway: discovery: locator: enabled: true lower-case-service-id: true routes: - id: Producer-gateway # Id of the route, unique URI: lb:// Producer # Service name # URI: localhost:8201 Address Predicates: =/producer/** eureka: instance: appname: gateway prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port} lease-renewal-interval-in-seconds: 30 lease-expiration-duration-in-seconds: 90 client: serviceUrl: defaultZone: http://localhost:8761/eurekaCopy the code

The test gateway calls the Producer service