海外开放平台技术点汇总

版权声明:话不在多,在于精 https://blog.csdn.net/qq_29857681/article/details/88663466

本地令牌桶限流(防刷)

  1. com.google.common.util.concurrent.RateLimiter
  2. 在afterPropertiesSet 中this.limiter = RateLimiter.create(qps.intValue());
  3. 在intercept 中if (!limiter.tryAcquire()) {throw exception()};

关于接口认证协议OAuth2 的代码实现

  1. ept-open-front  OpenAuthController#clientRequest() 传递 appKey redirectUrl(第三方的回调地址)pin  – > generateCode() 生成 code 并保存到缓存中,格式为 key : appkey-authcode value: pin expire: 60s
  2. 调用第三方回调地址并传递code
  3. ept-open-platform 第三方在回调中调用开放平台TokenAction#authorize() 
    1. checkParam() 校验code码是否正确
    2. authCodeGetAccessToken 根据code码获得accessToken
  4. ept-open-platform 第三方下次调用其他接口时会首先经过公共拦截器
    1. SystemParamValidInterceptor#checkAccessToken 校验token的正确性
    2. 根据token和appId查询pin 并替换。

自定义拦截器管道

  1. 设置前置后置拦截器
    private List<PreBusinessInterceptor> preList;
    
    private List<PostBusinessInterceptor> postList;
  2. 在spring.xml 中注入
  3. <bean id="interceptorPipeLine" class="com.jd.ept.open.router.interceptor.InterceptorPipeLine">
        <property name="preList">
            <list>
                <!-- 公共拦截-->
                <ref bean="systemRateLimiterInterceptor"/>
                <ref bean="systemParamValidInterceptor"/>
                <ref bean="appCountPerMinuteRateLimiterInterceptor"/>
  4. 在管道类中循环调用
  5. for (PreBusinessInterceptor interceptor : preList) {}

队列任务处理管理器

  1. 不同的写入入口提交任务(包含数据,处理类Handler)
  2. 任务管理器开启线程,每个处理器对应一个调用线程
  3. 队列任务处理调用,调用handler的执行方法
  4. 批量处理任务,不足100则添加,够100则处理
  5. 开启线程池,处理任务,
  6. 将future 交给任务监视者处理,超时监控
  7. 写入Hbase,将方法执行结果交给监控handler
  8. 监控统计方法的成功率和时间,并刷新JIMDB中的数据

心跳机制

监控机器是否存活,每5分钟发送一次心跳。

实现思路: 

  1. 定时
  2. 将appName 和 ip写入数据库
  3. 每5分钟更新一下时间。不存在则添加
  4. 当机器宕机时,不能获得ip,因此数据库中的数据就是机器存活的最近时间
@Scheduled(cron = "45 0/5 * * * ? ")   //每5分钟执行一次
@Override
public void afterPropertiesSet() throws Exception {
    try {
        registerService.refreshSysInfo(UmpConstant.APP_NAME, SystemUtil.getLocalhostIP());
    }catch(Exception e) {
        logger.error("afterPropertiesSet()",e);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29857681/article/details/88663466