微服务网关Gateway

Gateway简介

1.1 简介

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式,统一访问接口。SpringCloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。它是基于Nttey的响应式开发模式。

2.1

(1) 创建工程导入依赖

在项目中添加新的模块 shop_gateway_server ,并导入依赖

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

注意SpringCloud Gateway使用的web框架为webflux,和SpringMVC不兼容。引入的限流组件是
hystrix。redis底层不再使用jedis,而是lettuce。

(2) 配置启动类

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

(3) 编写配置文件

创建 application.yml 配置文件

server:
  port: 8080 #服务端口
spring:
   application:
       name: api-gateway #指定服务名
   cloud:
     gateway:
       routes:
        - id: product-service
          uri: http://127.0.0.1:8002
          predicates:
          - Path=/product/**

id:我们自定义的路由 ID,保持唯一
uri:目标服务地址
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
filters:过滤规则,暂时没用。

上面这段配置的意思是,配置了一个 id 为 product-service的路由规则,当访问网关请求地址以product 开头时,会自动转发到地址: http://127.0.0.1:8002/ 。

2.2 路由规则

Spring Cloud Gateway 的功能很强大,前面我们只是使用了 predicates 进行了简单的条件匹配,其实Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。在 Spring Cloud Gateway 中 Spring 利用Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。
在这里插入图片描述
示例

#路由断言之后匹配
spring: 
  cloud:
    gateway:
       routes: 
       - id: after_route 
         uri: https://xxxx.com
         #路由断言之前匹配
         predicates:
          - After=xxxxx 
#路由断言之前匹配
spring:
  cloud:
    gateway:
      routes: 
      - id: before_route 
        uri: https://xxxxxx.com
        predicates: 
        - Before=xxxxxxx
#路由断言之间 
spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://xxxx.com
        predicates:
        -  Between=xxxx,xxxx
#路由断言Cookie匹配,此predicate匹配给定名称(chocolate)和正则表达式(ch.p)
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://xxxx.com
        predicates: - Cookie=chocolate, ch.p
#路由断言Header匹配,header名称匹配X-Request-Id,且正则表达式匹配\d+
spring:
  cloud:
    gateway:
      routes:
       - id: header_route
         uri: https://xxxx.com
         predicates: - Header=X-Request-Id, \d+
#路由断言匹配Host匹配,匹配下面Host主机列表,**代表可变参数
spring:
  cloud:
    gateway:
      routes:
       - id: host_route 
         uri: https://xxxx.com
         predicates:
         - Host=**.somehost.org,**.anotherhost.org
#路由断言Method匹配,匹配的是请求的HTTP方法
spring:
  cloud:
    gateway:
      routes:
        id: method_route
        uri: https://xxxx.com
        predicates:
        - Method=GET 
#路由断言匹配,{segment}为可变参数 
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
      - uri: https://xxxx.com
      - predicates: - Path=/foo/{segment},/bar/{segment} 
#路由断言Query匹配,将请求的参数param(baz)进行匹配,也可以进行regexp正则表达式匹配 (参数包含 foo,并且foo的值匹配ba.) 
spring:
  cloud:
   gateway:
     routes:
     - id: query_route
       uri: https://xxxx.com
       predicates:
     -  Query=baz 或 Query=foo,ba. 
#路由断言RemoteAddr匹配,将匹配192.168.1.1~192.168.1.254之间的ip地址,其中24为子网掩码位 数即255.255.255.0
 spring:
   cloud:
     gateway:
       routes:
       - id: remoteaddr_route
         uri: https://example.org
         predicates:
         - RemoteAddr=192.168.1.1/24

2.3 动态路由

和zuul网关类似,在SpringCloud GateWay中也支持动态路由:即自动的从注册中心中获取服务列表并访问。

(1)添加注册中心依赖

在工程的pom文件中添加注册中心的客户端依赖(这里以Eureka为例)

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 
</dependency>
(2)配置动态路由

修改 application.yml 配置文件,添加eureka注册中心的相关配置,并修改访问映射的URL为服务名称

server: port: 8080 #服务端口 
spring:
  application:
    name: api-gateway #指定服务名
  cloud:
    gateway:
      routes:
      - id: product-service
        uri: lb://shop-service-product
        predicates:
        - Path=/product/** 
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
      registry-fetch-interval-seconds: 5 # 获取服务列表的周期:5s
    instance:
      preferIpAddress: true
      ip-address: 127.0.0.1

uri : uri以 lb: //开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称

2.4 重写转发路径

在SpringCloud Gateway中,路由转发是直接将匹配的路由path直接拼接到映射路(URI)之后,那么在微服务开发中往往没有那么便利。这里就可以通过RewritePath机制来进行路径重写。

(1) 添加RewritePath重写转发路径

修改 application.yml ,添加重写规则。

spring:
  application:
    name: api-gateway #指定服务名 
  cloud:
    gateway:
      routes:
      - id: product-service
        uri: lb://shop-service-product
        predicates:
        - Path=/product-service/**
        filters:
        - RewritePath=/product-service/(?<segment>.*), /$\{segment}

通过RewritePath配置重写转发的url,将/product-service/(?.*),重写为{segment},然后转发到订单微服务。比如在网页上请求http://localhost:8080/product-service/product,此时 将请求转发到http://127.0.0.1:8002/product/1( 值得注意的是在yml文档中 $ 要写成 $\ )

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值