[Spring cloud 一步步实现广告系统] 3. 网关路由
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.maxTotalConnections
andzuul.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
& Post
2个过滤器。
@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
后续更新

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
[Spring cloud 一步步实现广告系统] 1. 业务架构分析
什么是广告系统? 主要包含: 广告主投放广告的《广告投放系统》 媒体方(广告展示媒介-<地铁广告屏幕>)检索广告用的《广告检索系统》 广告计费系统(按次,曝光量等等) 报表系统 Etc. 使用技能栈 JDK1.8 MySQL 8+ Maven 3+ Spring cloud Greenwich.SR2 Eureka Zuul / gateway Feign ... Spring boot 2.1.5 Kafka 2.2.0 MySQL Binlog 项目结构 项目架构
-
下一篇
[Spring cloud 一步步实现广告系统] 5. 投放系统配置+启动
广告投放系统启动主类说明 /** * SponsorApplication for 广告赞助商/投递服务启动类 * 添加注解{@link EnableFeignClients}之后,当前微服务就可以调用别的微服务, * 但是当前服务是广告提供,不需要调用别的微服务,在此只是为了在dashboard中监控 * {@link EnableCircuitBreaker} 也是为了dashboard监控 * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a> * @since 2019/6/15 */ @EnableDiscoveryClient //开启服务发现Eureka Client @EnableCircuitBreaker //开启断路器 @EnableFeignClients //开启feign client,使其可以通过HTTP调用其他微服务 @SpringBootApplication public class SponsorApplication { public sta...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- MySQL数据库在高并发下的优化方案
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果