海外开放平台技术点汇总
本地令牌桶限流(防刷)
- com.google.common.util.concurrent.RateLimiter
- 在afterPropertiesSet 中this.limiter = RateLimiter.create(qps.intValue());
- 在intercept 中if (!limiter.tryAcquire()) {throw exception()};
关于接口认证协议OAuth2 的代码实现
- ept-open-front OpenAuthController#clientRequest() 传递 appKey redirectUrl(第三方的回调地址)pin – > generateCode() 生成 code 并保存到缓存中,格式为 key : appkey-authcode value: pin expire: 60s
- 调用第三方回调地址并传递code
- ept-open-platform 第三方在回调中调用开放平台TokenAction#authorize()
- checkParam() 校验code码是否正确
- authCodeGetAccessToken 根据code码获得accessToken
- ept-open-platform 第三方下次调用其他接口时会首先经过公共拦截器
- SystemParamValidInterceptor#checkAccessToken 校验token的正确性
- 根据token和appId查询pin 并替换。
自定义拦截器管道
- 设置前置后置拦截器
private List<PreBusinessInterceptor> preList; private List<PostBusinessInterceptor> postList;
- 在spring.xml 中注入
-
<bean id="interceptorPipeLine" class="com.jd.ept.open.router.interceptor.InterceptorPipeLine"> <property name="preList"> <list> <!-- 公共拦截--> <ref bean="systemRateLimiterInterceptor"/> <ref bean="systemParamValidInterceptor"/> <ref bean="appCountPerMinuteRateLimiterInterceptor"/>
- 在管道类中循环调用
-
for (PreBusinessInterceptor interceptor : preList) {}
队列任务处理管理器
- 不同的写入入口提交任务(包含数据,处理类Handler)
- 任务管理器开启线程,每个处理器对应一个调用线程
- 队列任务处理调用,调用handler的执行方法
- 批量处理任务,不足100则添加,够100则处理
- 开启线程池,处理任务,
- 将future 交给任务监视者处理,超时监控
- 写入Hbase,将方法执行结果交给监控handler
- 监控统计方法的成功率和时间,并刷新JIMDB中的数据
心跳机制
监控机器是否存活,每5分钟发送一次心跳。
实现思路:
- 定时
- 将appName 和 ip写入数据库
- 每5分钟更新一下时间。不存在则添加
- 当机器宕机时,不能获得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); } }