Spring Cloud Gateway

1、Spring Cloud Gateway
1.1、简介

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,功能上比Zuul更加强大,性能也更好,随着Spring Cloud的版本迭代,官方有用Gateway取代Zuul网关的趋势。网关作为一个系统的流量的入口,有着举足轻重的作用,大致功能如下:
路由转发、协议转换
流量聚合,对流量进行监控,日志输出
限流:作为整微服务前端入口,外部流量只能通过网关请求,对流量进行控制
权限控制
Spring Cloud Gateway替换Zuul比较简单,成本较低,Spring Cloud Gateway几乎包含了zuul的所有功能。

Spring Cloud Gateway_第1张图片
1.2、创建一个简单Gateway工程
基于Spring Boot版本2.1.1.RELEASE,Spring Cloud版本为Greenwich.RC2创建,Spring Cloud Gateway官方文档地址:http://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RC3/single/spring-cloud-gateway.html,创建its-spb-gatewey工程pom:


	4.0.0
	com.its.geteway
	its-spb-geteway
	1.0.0
	its-spb-geteway
	its-spb-geteway
	
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
	
	
	
		1.8
		Greenwich.RC2
		UTF-8
	
	
	
		
		
			org.springframework.cloud
			spring-cloud-starter-gateway
		
	

	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
		
	

its-spb-gatewey配置appication.yml:

server:
  ###服务端口号
  port: 80

创建Application 类,运行Application 启动its-spb-gatewey

package com.its.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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 {

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

	//创建一个简单路由RouteLocator的Bean进行路由转发,将请求进行处理,最后转发到目标的下游服务。
	@Bean
	public RouteLocator routes(RouteLocatorBuilder builder) {
		return builder.routes().route(
				p -> p.path("/get").filters(f -> f.addRequestHeader("Gateway", "HelloWorld")).uri("http://httpbin.org:80"))
				.build();
	}
}

代码中的routes方法为创建路由,配置predicates(断言:配置请求的规则,由具体的route去处理)和filters(各种过滤器,来对请求做各种判断和修改)。
代码中创建的route可以将请求http://127.0.0.1/get都转发到http://httpbin.org:80/get,filters为请求添加一个RequestHeader,header的key为Gateway,value为HelloWord。
在浏览器上访问http://127.0.0.1/get,浏览器显示如下:

{
  "args": {}, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "zh-CN,zh;q=0.9", 
    "Cache-Control": "max-age=0", 
    "Connection": "close", 
    "Cookie": "Hm_lvt_57011ad09625c9c2cefad265f1d664b1=1545295992", 
    "Forwarded": "proto=http;host=127.0.0.1;for=\"127.0.0.1:61687\"", 
    "Gateway": "HelloWorld", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36", 
    "X-Forwarded-Host": "127.0.0.1"
  }, 
  "origin": "127.0.0.1, 119.139.197.111", 
  "url": "http://127.0.0.1/get"
}

返回结果可见,向gateway网关工程请求http://127.0.0.1/get,gateway网关会将请求转发到http://httpbin.org/get,并且在转发之前,加上一个filter,该filter会在请求添加一个header的key为Gateway,value为HelloWord。
 

2、Spring Cloud Gateway Predicate
Spring Cloud Gateway内置了许多Predict,源码在org.springframework.cloud.gateway.handler.predicate包中,Predicate简介Predicate如下:

RoutePredicateFactory datetime 请求时间校验条件谓语 AfterRoutePredicateFactory 请求时间在配置时间之后
BeforeRoutePredicateFactory 请求时间在配置时间之前
BetweenRoutePredicateFactory 请求时间在配置时间之间
Cookie 请求Cookie校验条件谓语 CookieRoutePredicateFactory 请求Cookie正则表达式匹配指定值
Header 请求Header校验条件谓语 HeaderRoutePredicateFactory 请求Header正则表达式匹配指定值

CloudFoundryRouteService

RoutePredicateFactory

请求Header是否包含指定名称
Host 请求Host校验条件谓语 HostRoutePredicateFactory 请求Host匹配指定值
Method 请求Method校验条件谓语 MethodRoutePredicateFactory 请求Method匹配所配置的method
Path 请求Path校验条件谓语 PathRoutePredicateFactory 请求路径正则匹配指定值
QueryParam 请求查询参数校验条件谓语 QueryRoutePredicateFactory 请求查询参数正则匹配指定值
RemoteAddr 请求远程地址校验条件谓语 RemoteAddrRoutePredicateFactory 请求远程地址正则匹配指定值

Gateway Predicate 例子application.yml:

###Spring Cloud Gateway官方Route Predicate Factories
###yml再建一个配置文件,语法是三个横线“---”        
###After Route Predicate Factory 请求时间在2017-01-20T17:42:47.789-07:00[America/Denver]之后跳转http://example.org
---
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
  profiles: after_route
###Before Route Predicate Factory 请求时间在2017-01-20T17:42:47.789-07:00[America/Denver]之前跳转http://example.org  
---
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
  profiles: befter_route
###Between Route Predicate Factory 请求时间在2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]之间跳转http://example.org  
---
spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
  profiles: between_route
###Cookie Route Predicate Factory 请求Cookie名称JSESSIONID,值3214891EC42262C77D75514BF8E13EFE
---
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://example.org
        predicates:
        - Cookie=JSESSIONID, 3214891EC42262C77D75514BF8E13EFE
  profiles: cookie_route
###Header Route Predicate Factory
---
spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: http://example.org
        predicates:
        - Header=X-Request-Id, \d+
  profiles: header_route
###Host Route Predicate Factory
---
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://httpbin.org:80/get
        predicates:
        - Host=**.127.0.0.1
  profiles: host_route
###Method Route Predicate Factory 请求类型为get
---
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://httpbin.org:80/get
        predicates:
        - Method=GET
  profiles: method_route
###Path Route Predicate Factory
###测试http://127.0.0.1/foo/123
---
spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://example.org
        predicates:
        - Path=/foo/{segment},/bar/{segment}
  profiles: path_route
###Query Route Predicate Factory
###测试http://127.0.0.1?foo=ba.
---
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://example.org
        predicates:
        - Query=foo, ba.
  profiles: query_route
###Query Route Predicate Factory
###测试http://127.0.0.1?foo=ba.
---
spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://example.org
        predicates:
        - RemoteAddr=127.0.0.1
  profiles: remoteaddr_route

 

 

你可能感兴趣的:(SpringCloud)