[Spring cloud 一步步实现广告系统] 3. 网关路由
2019-07-26 21:59
1701 查看
Zuul(Router and Filter)
WIKI: 传送门
作用
- 认证,鉴权(Authentication/Security)
- 预判(Insights)
- 压力测试(Stress Testing)
- 灰度/金丝雀测试(Canary Testing)
- 动态路由(Dynamic Routing)
- 服务迁移(Service Migration)
- 降低负载(Load Shedding)
- 静态响应处理(Static Response handling)
- 主动/主动交换管理(Active/Active traffic management)
关键配置:
The configuration property
zuul.host.maxTotalConnectionsandzuul.host.maxPerRouteConnections, which default to 200 and 20 respectively.
创建mscx-ad-zuul
三步曲创建法:
添加依赖<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>加注解
@SpringCloudApplication @EnableZuulProxy //启用网关 public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }改配置
spring: application: name: ad-gateway-zuul server: port: 1111 eureka: client: service-url: defaultZone: http://server1:7777/eureka/,http://server2:8888/eureka/,http://server3:9999/eureka/ instance: hostname: ad-gateway-zuul zuul: ignored-services: '*' # 过滤所有请求,除了下面routes中声明过的服务 routes: sponsor: #在路由中自定义服务路由名称 path: /ad-sponsor/** serviceId: mscx-ad-sponsor #微服务name strip-prefix: false search: #在路由中自定义服务路由名称 path: /ad-search/** serviceId: mscx-ad-search #微服务name strip-prefix: false prefix: /gateway/api strip-prefix: false #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀过滤器编写
我们来编写一个记录请求时间周期的过滤器,根据Filter的三种类型:
Pre filters,
routing filters和
Post filters,我们需要定义2个filter,用来记录开始和结束时间,很明显,我们需要实现
Pre&
Post2个过滤器。
@Slf4j @Component public class PreRequestFilter extends ZuulFilter { @Override public String filterType() { // pre filter return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { //获取当前请求的请求上下文 RequestContext requestContext = RequestContext.getCurrentContext(); //记录请求进入时间 requestContext.set("api_request_time", System.currentTimeMillis()); return null; } } --- @Slf4j @Component public class AccessLogFilter extends ZuulFilter { @Override public String filterType() { return FilterConstants.POST_TYPE; } @Override public int filterOrder() { //需要最后一个执行的filter return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext requestContext = RequestContext.getCurrentContext(); HttpServletRequest request = requestContext.getRequest(); log.info("Request \"{}\" spent : {} seconds.", request.getRequestURI(), (System.currentTimeMillis() - Long.valueOf(requestContext.get("api_request_time").toString())) / 1000); return null; } }
Gateway
后续更新
相关文章推荐
- [Spring cloud 一步步实现广告系统] 配置项目结构 & 实现Eureka服务
- 【夯实Spring Cloud】Spring Cloud中使用Zuul实现路由网关详解
- 利用Linux系统实现VLAN间的单臂路由及安全网关的功能
- angular基于路由控制ui-router实现系统权限控制
- 基于SpringCloud微服务架构 广告系统设计与实现
- x86主机搭建家庭智能路由系统 ---- Proxmox虚拟化实现一机多用
- Spring Cloud入门教程 - Zuul实现API网关和请求过滤
- spring cloud 网关路由信息动态配置
- ASP.NET路由系统实现原理:HttpHandler的动态映射
- Android 和 iOS 实现路由(route)系统。
- Spring Cloud入门教程 - Zuul实现API网关和请求过滤
- 在linux系统下利用PC机实现路由转发的具体步骤
- java 实现一个广告系统
- 大数据Spark “蘑菇云”行动第60课: 广告点击系统广告点击趋势分析与实现
- 开启Linux系统路由转发功能 实现多网段电脑共享上网
- 虚拟机中linux系统实现路由转发功能
- x86主机搭建家庭智能路由系统 ---- Proxmox虚拟化实现一机多用
- 大数据IMF传奇行动绝密课程第94课:SparkStreaming实现广告计费系统中在线黑名单过滤实战
- x86主机搭建家庭智能路由系统 ---- Proxmox虚拟化实现一机多用
- 基于 SpringCloud 微服务架构下 广告系统设计与实现 完整版