Spring Cloud Gateway

基于Spring5.0和SpringBoot2.0,非阻塞API,支持WebSockets

Route(路由):Predicate(断言) + Filter(过滤器)

集成动态路由、熔断器、限流、DiscoveryClient、路径重写

client --> Spring Cloud Gateway(Gateway Handler Mapping --> Gateway Web Handler --> FilterChains)

路由

提供2种方式的配置

  • 在启动主类Application中,通过@Bean自定义RouteLocator
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
	return builder.routes()
		.route("path_route_about", r->r.path("/about")
				.uri("http://dwiki.cmbchina.cn/#all-updates"))
		.build();
}
  • application.yml
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
      - id: path_route_about
        uri: http://dwiki.cmbchina.cn/#all-updates
        predicates:
#        - Host=**.foo.org
#        - Path=/about
#        - Method=GET
#        - Header=X-Request-Id, \d+
#        - Query=foo, ba.
#        - Query=baz
#        - Cookie=chocolate, ch.p
        - After=2020-05-27T18:32:32+08:00[Asia/Shanghai]

Predicate

Filter

示例

项目依赖: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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cmb</groupId>
	<artifactId>sc-gw-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>sc-gw-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
	</properties>

	<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>

	<dependencies>
		<!--netty+webflux机制,无需web-->
		<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.3.1</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

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

spring-cloud-gateway-start | ityouknowSpring Cloud Gateway初探

posted @ 2020-05-27 13:11  万箭穿心,习惯就好。  阅读(186)  评论(0编辑  收藏  举报