前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每天20分钟之zuul

每天20分钟之zuul

原创
作者头像
李子健
发布2022-08-13 12:54:55
4900
发布2022-08-13 12:54:55
举报
文章被收录于专栏:每日一善每日一善

概述

网关为我们管理api接口提供的主要功能

  • 管理api接口
  • 适配协议
  • 安全认证
  • 转发路由
  • 限制流量
  • 监控日志
  • 防止爬虫
  • 灰度发布
  • 服务聚合

不建议使用zuul1作为线上网关使用,大家可以使用zuul2或者是spring-cloud-gateway作为微服务的网关

假如你使用zuul2作为网关的话,zuul1可以学习使用,其实基本功能类似,只是在底层改为netty去转发http请求

zuul1提供的功能

zuul的核心功能是过滤器,通过过滤器实现

  • 动态路由
  • 请求监控
  • 认证鉴权
  • 压力测试
  • 灰度发布

坑一

  • 注意zuul1和springboot的版本适配问题(zuul后面已经被spring-cloud干掉了,不在支持集成使用)
<dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

基础的重定向,从path重定向到url

spring.application.name=zuul-gateway-static
server.port=9011
server.address=127.0.0.1

debug=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS

zuul.routes.test1.path=/abc/**
zuul.routes.test1.url=http://www.example.com/

zuul.routes.localpath.path=/a/**
zuul.routes.localpath.url=forward:/a

zuul.routes.userinfo.path=/user/**
zuul.routes.userinfo.service-id=zuul-user
zuul.routes.userinfo.stripPrefix=false
zuul.routes.userinfo.sensitive-headers=true
zuul.routes.userinfo.customSensitiveHeaders=true

zuul.retryable=true
ribbon.okhttp.enabled=true


eureka.instance.ip-address=127.0.0.1
eureka.client.serviceUrl.defaultZone=http://tom:123456@localhost:9010/eureka/
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.application.name}:${server.address}:${server.port}
eureka.client.healthcheck.enabled=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=15

logging.config=classpath:logback.xml

查看所有routers的配置

  • http://localhost:9011/actuator/routes
  • http://localhost:9011/actuator/filters
  1. 访问服务:
  2. http://localhost:9011/abc/123
  3. 基础的匹配
zuul.routes.test1.path=/abc/**
zuul.routes.test1.url=http://www.example.com/
  1. 本地跳转zuul.routes.localpath.path=/a/** zuul.routes.localpath.url=forward:/a
  2. 使用eureka的匹配
  3. http://localhost:9011/user/1234
zuul.routes.userinfo.path=/user/**
zuul.routes.userinfo.service-id=zuul-user
zuul.routes.userinfo.stripPrefix=false
zuul.routes.userinfo.sensitive-headers=true
zuul.routes.userinfo.customSensitiveHeaders=true
  1. 自定义过滤器
  2. pre 请求被路由之前调用
  3. route 请求路由时调用
  4. post 在route和error过滤器之后调用
  5. error 在请求发生错误时调用

代码路径:

https://github.com/beckbikang/spring-cloud/tree/main/kzuul

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • zuul1提供的功能
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档