前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Spring cloud 一步步实现广告系统] 3. 网关路由

[Spring cloud 一步步实现广告系统] 3. 网关路由

作者头像
Isaac Zhang
发布2019-09-10 18:04:37
4550
发布2019-09-10 18:04:37
举报
Zuul(Router and Filter)

WIKI: 传送门

UTOOLS1563957310060.png
UTOOLS1563957310060.png
作用
  1. 认证,鉴权(Authentication/Security)
  2. 预判(Insights)
  3. 压力测试(Stress Testing)
  4. 灰度/金丝雀测试(Canary Testing)
  5. 动态路由(Dynamic Routing)
  6. 服务迁移(Service Migration)
  7. 降低负载(Load Shedding)
  8. 静态响应处理(Static Response handling)
  9. 主动/主动交换管理(Active/Active traffic management)

关键配置:

The configuration property zuul.host.maxTotalConnections and zuul.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 filtersPost 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

后续更新--- 做一个好人。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Zuul(Router and Filter)
    • 作用
      • 创建mscx-ad-zuul
        • 添加依赖
        • 加注解
        • 改配置
        • 过滤器编写
    • Gateway
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档