spring cloud gateway 的初步认识和使用

1.简单介绍 

spring cloud gateway 是spring 官方基于 spring 5.0 ,springBoot 2.0 和 Project Reactor 等技术开发的网关,目标是替代netflix zuul ,之所以要替换是因为Zuul基于servlet 2.5 (works with 3.x),使用阻塞API。它不支持任何长期的连接,如websocket。Gateway建立在Spring Framework 5,Project Reactor和Spring Boot 2上,使用非阻塞API。支持Websockets,因为它与Spring紧密集成,所以它会是一个让开发者有更好体验的框架,但是 zuul 2基于netty也实现了非阻塞和长连接,这是后话

2.使用spring cloud gateway

1.引入spring cloud gateway 依赖,springBoot版本是2.0.7.RELEASE,spring cloud 版本是 Finchley.SR2  对应的spring-cloud-starter-gateway 的版本是2.0.2RELEASE

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
 </dependency>

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

2.参考spring官方文档,

 Glossary

  • Route: Route the basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if aggregate predicate is true.
  • Predicate: This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This allows developers to match on anything from the HTTP request, such as headers or parameters.
  • Filter: These are instances Spring Framework GatewayFilter constructed in with a specific factory. Here, requests and responses can be modified before or after sending the downstream reque

简单翻译如下

术语

路由:路由网关的基本构建块。它由ID、目标URI、谓词集合和筛选器集合定义。如果聚合谓词为真,则匹配路由。

谓词:这是一个Java 8函数谓词。输入类型是Spring Framework ServerWebExchange。这允许开发人员匹配来自HTTP请求的任何内容,如标题或参数。

过滤器:这些是用特定工厂构造的Spring Framework GatewayFilter实例。在这里,可以在发送下游reque之前或之后修改请求和响应

即这个比较重要的概念是 Route Predicate Filter  

Predicate  不仅仅可以根据请求的路径-Path决定请求的url,还可以根据anything from the HTTP request ,例如请求头,请求参数。

Route Predicate Factories  

例如

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://example.org
        predicates:
        - Cookie=chocolate, ch.p

根据请求参数中cookie中是否含有chocolate这个属性,并且它的值符合正则表达式 ch.p,即将请求转发到http://example.org

GatewayFilter Factories

Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories

Route filter 可以修改进来和传出的请求

3.也可以整合eureka ,调用服务的方式就变成了  网关的地址/eureka服务名/加具体的url,也做到添加微服务之后,无需该更改原有的配置,即可通过网关访问到服务的提供者,注意这里的微服务名不要用下划线来命名,否则会出现NullPointException

#yml格式文件书写 注意事项
#- 不用空两格  ,表示list  
#= 等于号连接表示map

server:
  port: 6604
#服务名
spring:
  application:
     name: gateway-service
  cloud:
    gateway:
        discovery:
          locator:
            #使用服务名的方式的访问请求的路径,无需一个个添加  http:网关地址/服务名小写/访问路径
            enabled: true
            lower-case-service-id: true  #服务名小写
       #包含三个重要的概念  route  predicate 和  filters   一个route下面包含  id  uri  predicate 和 filter  
   # predicate即路由匹配规则  filter即过滤规则  
#      routes:
#       - id: app_user  # route 下面有好多  具体的路由id为app_user的微服务
#         #lb代表从注册中心中获取服务  使用uri的 http://localhost:8081 的方式成功
#         #uri: http://localhost:8081
#         uri: lb://app_user
#         predicates:
#                      #即转发路径规则
#         - Path=/api/user/**
#                      #过滤规则
#         filters:
#         #StripPrefix即去掉前缀  1代表路径的1个路径字段  2 代表2个路径字段
#         - StripPrefix=1
#       routes:
#        - id: hystrix-test
#          uri: http://localhost:8080
#          predicates:
#          - Path=/hystrix/**
#          filters:
#          - Hystrix=getUserName
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:123@localhost:9003/eureka/
  instance:
    prefer-ip-address: true   
    #默认是主机名:application-name :端口

#logging:
#  level:
#    org.springframework.cloud.gateway: trace
#    org.springframework.http.server.reactive: debug
#    org.springframework.web.reactive: debug
#    reactor.ipc.netty: debug
  

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值