Before:@LoginCheckAop(); //这里由于注解的存在,首先会AOP调用登陆校验的方法public void dosomething(){doA();//业务逻辑A,需要登陆检查doB();//业务逻辑B,不需要登陆检查doC();//业务逻辑C,不需要登陆检查after();// 全部均为串行操作,后一个操作需等待前一个任务结束才能开始执行}---------------------------------------------------------------------------After://将这里的LoginCheck注解AOP校验登录的方式去除public void dosomething(){async.doAsync(loginCheck()); // 异步调用登录校验async.doAsync(doB()); // 异步业务逻辑B,不需要登陆检查async.doAsync(doC()); // 异步业务逻辑C,不需要登陆检查timeWait(loginCheck); // 等待登录校验完成async.doAsync(doA()); // 异步业务逻辑A,需要登陆检查(这里已经在上面校验登录完成)timeWait(A,B,C); // 等待A,B,C全部执行完毕aggregateResponse(A,B,C) // 聚合结果返回}
before:public void getProductInfo(){dynmaicGetProduct() // 动态获取商品信息dynmaicGetProductPromotion() // 动态获取商品活动信息aggreateResponse() // 聚合数据返回}
after:public void getProductInfo(){staticGetProduct() // 获取商品信息静态数据dynmaicGetProductPromotion() // 动态获取商品活动信息aggreateResponse() // 聚合数据返回}private void staticGetProduct(){Product product = null;// 判断是否是特殊商品,比如大促商品走特殊缓存if(isSpecialProduct()) {//从本地缓存获取数据product = getSpecialProductFromLocalCache();}//是否开启本地缓存开关if(localCacheSwitch()) {//从本地缓存获取数据product = getProductFromLocalCache();}//是否开启Redis开关if(redisSwitch()) {//从Redis获取数据product = getProductFromRedis();}if(product == null) {product = dynmaicGetProduct() // 动态获取商品信息if(redisSwitch()) {setRedis(product) //设置Redis}}}
public void doVIP(){if(thread.currentNums < thread.vipNums) //重要任务设置高并发量doVIP();else//进行处理}public void doNormal(){if(thread.currentNums < thread.normalNums) //一般任务设置低并发量doNormal();else//进行处理}
/*** 自带限流控制线程池* @param semaphore* @param concurrency* @param runnable* @param pool* @return*/public static CompletableFuture<Void> flowControlFuture(TryableSemaphore semaphore, Integer concurrency, Runnable runnable, ExecutorService pool) {if (semaphore == null || INDEX_FLOW_CONTROL_SWITCH == null || !INDEX_FLOW_CONTROL_SWITCH.booleanValue()) {return CompletableFuture.runAsync(runnable, pool);}Runnable run = semaphore.tryAcquire(concurrency,runnable);if (run != null) {try {return CompletableFuture.runAsync(run, pool);} catch (Throwable e) {semaphore.release();}}return null;}public static CompletableFuture<Void> future(Runnable runnable, ExecutorService pool) {try {return CompletableFuture.runAsync(runnable, pool);} catch (Throwable e) {log.error("任务加入线程池失败!", e);return null;}}
public void dosomething(){try{data = callA();return data;//下游可用时直接获取}catch(Exception e){data = cache.getData(); //下游不可用时从缓存取兜底数据return data;}}
public void dosomething(){//判断是否首屏if(firstScreen()) {//获取活动数据getPromotion();}doA();}