zuul网关_服务网关Zuul和SpringCloudGateway

a170b4042f29c3227410d3fb1aa25353.png

本文参考了<Spring Cloud微服务和分布式系统实战>一书, 表示感谢!

具体可关注公众号: 戏说码农职场


首先二者都是服务网管, Zuul是旧的API网关, 后者是新的, 先比较下二者区别:

  1. Zuul 来自Netflix, 提供身份验证, 安全校验, 限流, 动态路由等, 但是性能一般
  2. Gateway是在Zuul基础之上创建.

基本定义:

  • 网关: 是一种外部网络和内部网络的关卡, 通过网关进行转发, 可以分为硬件网关和软件网关, 硬件网关包括LVS 和F5, 软件网关包括Nginx,Zuul, Gateway等, 主要作用体现在请求过滤和路由转发功能.

简单介绍下使用

Zuul

  1. 如何使用

增加依赖

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

入口方法

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulProxyApplication {

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

}

可以看出Zuul定义为一个服务代理的角色. 继续稍微看下源码:

@EnableCircuitBreaker
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ZuulProxyMarkerConfiguration.class)
public @interface EnableZuulProxy {

}

其中@EnableCircuitBreaker 表面内部是是要断路器Hystrix.

# 定义Spring应用名称,它是一个微服务的名称,一个微服务可拥有多个实例
spring:
 application:
  name: zuul

# 向端口为5001和5002的Eureka服务治理中心注册
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:5001/eureka, http://localhost:5002/eureka

# Zuul的配置
zuul:
 # 路由配置
 routes:
  # 用户微服务
  user-service:
    # 请求拦截路径配置(使用ANT风格)
    path: /u/**
    # 通过一个URL配置
    url: http://localhost:6001/
  # 产品微服务配置
  fund-service:
    # 请求拦截路径配置(使用ANT风格)
    path: /p/**
    service-id: product

意味着/u/user/info/1会被映射为localhost:6001/user/info/1

以上是拦截器的功能

2. 限流

依赖springCloud Resilience4j限速器, rateLimiter

那又如何限制呢?

# resilience4j配置
resilience4j:
 # 限速器注册机
 ratelimiter:
  limiters:
   # 名称为“user”的限速器
   user:
     # 时间戳内限制通过的请求数,默认为50
    limitForPeriod: 3
    # 配置时间戳(单位毫秒),默认值为500 ns
    limitRefreshPeriodInMillis: 5000
    # 超时时间
    timeoutInMillis: 10

3. 动态路由

可以使用MySQL保存服务信息

4. 网关过滤

SpringCloudGateway

特点:

  1. Spring Cloud Gateway 底层使用了高性能的通信框架Netty

2. 三个术语: 过滤, 路由和断言

详细说明转自:

SpringCloud gateway (史上最全)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值