├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── pack-api ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── api │ │ ├── core │ │ ├── callback │ │ │ └── IMyRequesCallback.java │ │ ├── dto │ │ │ ├── MyBaseRequest.java │ │ │ └── MyBaseResponse.java │ │ └── template │ │ │ └── MyFacadeTemplate.java │ │ ├── rpc │ │ ├── annotations │ │ │ └── RpcShardTableRoute.java │ │ └── dto │ │ │ └── BaseRpcRequest.java │ │ └── web │ │ └── dto │ │ └── MyBaseWebRequest.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── api │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-bom ├── .gitignore ├── all-bom │ ├── .gitignore │ └── pom.xml ├── logback-bom │ ├── .gitignore │ └── pom.xml ├── pom.xml └── test-bom │ ├── .gitignore │ └── pom.xml ├── pack-http ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── http │ │ ├── async │ │ ├── client │ │ │ ├── ISimpleAsyncHttpClient.java │ │ │ └── impl │ │ │ │ └── SimpleAsyncHttpClientDefaultImpl.java │ │ └── utils │ │ │ ├── MyAsyncHttpUtils.java │ │ │ └── helper │ │ │ ├── MyAbstractAsyncHttpClient.java │ │ │ ├── MyAsyncHttpClient.java │ │ │ └── MyAsyncHttpClientHelper.java │ │ ├── common │ │ ├── constants │ │ │ └── HttpConstants.java │ │ ├── exceptions │ │ │ └── NoServerAvailableException.java │ │ ├── service │ │ │ ├── loadbalance │ │ │ │ ├── ILoadBalancer.java │ │ │ │ └── impl │ │ │ │ │ ├── RandomLoadBalancerImpl.java │ │ │ │ │ └── RoundRobinLoadBalancerImpl.java │ │ │ └── server │ │ │ │ ├── IMyHttpServer.java │ │ │ │ ├── helper │ │ │ │ ├── server │ │ │ │ │ ├── ServerInstance.java │ │ │ │ │ ├── ServerStatus.java │ │ │ │ │ └── ServerStatusFactory.java │ │ │ │ └── serverlist │ │ │ │ │ ├── DefaultServerInstanceList.java │ │ │ │ │ └── IServerInstanceList.java │ │ │ │ └── impl │ │ │ │ └── MyHttpServerImpl.java │ │ └── utils │ │ │ ├── ConnectionMonitorUtils.java │ │ │ ├── HttpMetricMonitor.java │ │ │ └── HttpParamUtils.java │ │ └── sync │ │ ├── client │ │ ├── ISimpleHttpClient.java │ │ ├── SimpleHttpSupport.java │ │ └── impl │ │ │ ├── SimpleHttpClientDefaultImpl.java │ │ │ └── helper │ │ │ ├── IFunctionWrapper.java │ │ │ └── SimpleHttpClientHelper.java │ │ └── utils │ │ ├── MyHttpUtils.java │ │ └── helper │ │ ├── MyAbstractHttpClient.java │ │ ├── MyHttpClient.java │ │ ├── MyHttpRawUtils.java │ │ └── MyHttpRawUtilsHelper.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── http │ └── test │ ├── async │ └── BaiduHomePageAsyncFetchTestCase.java │ ├── common │ └── TestCommonTestCase.java │ └── sync │ └── BaiduHomePageFetchTestCase.java ├── pack-idem ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── idem │ │ ├── IIdemService.java │ │ ├── impl │ │ └── IdemServiceImpl.java │ │ ├── service │ │ ├── callback │ │ │ └── IIdemBizCallback.java │ │ └── helper │ │ │ ├── IIdemServiceHelper.java │ │ │ └── impl │ │ │ └── IdemServiceRedisHelperImpl.java │ │ └── support │ │ └── key │ │ └── IdemKeyUtils.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── idem │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-idgen ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── idgen │ │ ├── IIdgenService.java │ │ ├── aop │ │ └── IdGenAop.java │ │ └── impl │ │ └── IdgenServiceRedisImpl.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── idegen │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-lang ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── lang │ │ ├── ables │ │ ├── ICancelable.java │ │ ├── IIdAble.java │ │ ├── IInitAble.java │ │ ├── IStoreAble.java │ │ ├── ITimeoutAble.java │ │ └── data │ │ │ ├── IDataAware.java │ │ │ └── IExtAware.java │ │ ├── callback │ │ └── IMyMethodCallback.java │ │ ├── constants │ │ └── PackConstants.java │ │ ├── exceptions │ │ ├── enums │ │ │ └── MyErrorExceptionEnum.java │ │ └── exceptions │ │ │ ├── base │ │ │ └── AbstractBaseException.java │ │ │ ├── biz │ │ │ └── BizException.java │ │ │ ├── others │ │ │ ├── NpeException.java │ │ │ ├── SocketRuntimeException.java │ │ │ └── SystemException.java │ │ │ └── param │ │ │ ├── IdemException.java │ │ │ └── ParamException.java │ │ ├── future │ │ ├── IInvokeFuture.java │ │ ├── IInvokeFutureListener.java │ │ ├── InvokeFutureFactory.java │ │ └── impl │ │ │ └── InvokeFutureImpl.java │ │ ├── reflect │ │ └── MyReflectionUtil.java │ │ ├── security │ │ ├── BizParamAssertUtil.java │ │ └── MyAssertUtils.java │ │ └── templates │ │ └── NoErrorTemplate.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── lang │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-lock ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── lock │ │ ├── IMyDistributeLock.java │ │ └── impl │ │ └── MyRedisDistributeLockImpl.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── lock │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-log ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── log │ │ └── LoggerUtil.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── log │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-metrics ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── metrics │ │ ├── CostMetricMonitor.java │ │ ├── MonitorHelper.java │ │ ├── jvm │ │ └── JvmMetricService.java │ │ └── tomcat │ │ ├── TomcatMetricSchedule.java │ │ └── TomcatMetricService.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── metrics │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-msg ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── msg │ ├── domain │ ├── consumer │ │ ├── IMessageListener.java │ │ ├── dispatcher │ │ │ ├── IMessageDispatcher.java │ │ │ └── MessageDispatcherImpl.java │ │ ├── handler │ │ │ ├── IMessageFilter.java │ │ │ ├── IMessageHandler.java │ │ │ └── IMessageProcessService.java │ │ └── subscribe │ │ │ └── ISourceSubscribe.java │ └── domain │ │ └── KafkaOffset.java │ ├── impl │ └── kafka │ │ ├── consumer │ │ ├── config │ │ │ └── KafkaConsumeConfig.java │ │ ├── handler │ │ │ └── MessageHandlerImpl.java │ │ └── subscribe │ │ │ └── KafkaSequenceSubscribe.java │ │ └── producer │ │ └── KafkaProducerUtils.java │ └── support │ ├── constants │ └── LoggerNames.java │ ├── enums │ ├── MessageErrorEnum.java │ └── MessageProcessStatusEnum.java │ ├── exceptions │ └── MessageProcessorException.java │ └── helper │ ├── KafkaDataSerializer.java │ └── KafkaRouteSerializer.java ├── pack-redis ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── redis │ │ ├── IMyRedisBatchService.java │ │ ├── IMyRedisService.java │ │ ├── RedisServerFactory.java │ │ ├── aop │ │ └── RedisAop.java │ │ ├── impl │ │ ├── MyRedisBatchServiceImpl.java │ │ └── MyRedisServiceImpl.java │ │ └── schedule │ │ ├── RedisMetricSchedule.java │ │ └── helper │ │ └── RedisMetricService.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── redis │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-ruleengine ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── ruleengine │ │ ├── IExpressExecutor.java │ │ ├── factory │ │ └── ExpressExecutorFactory.java │ │ ├── impl │ │ └── ql │ │ │ ├── ExpressExecutorQlImpl.java │ │ │ ├── service │ │ │ └── operators │ │ │ │ └── OperatorBetween.java │ │ │ └── support │ │ │ └── helper │ │ │ └── MyQlExpressContext.java │ │ └── support │ │ └── constants │ │ └── RuleEngineConstants.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── ruleengine │ └── test │ ├── ExpressExecutorTestCase.java │ └── dto │ └── UserInfoTest.java ├── pack-springboot-ext ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── springboot │ └── ext │ └── bean │ └── TomcatConfigBean.java ├── pack-threadcontext ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── thread │ │ └── MyThreadContext.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── thread │ └── test │ └── common │ └── TestCommonTestCase.java ├── pack-trace ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── trace │ └── MyTraceUtils.java ├── pack-utils ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── utils │ │ ├── devlopment │ │ └── IdeaDetectUtil.java │ │ ├── io │ │ ├── ConfigLoaderUtils.java │ │ ├── MyFileUtils.java │ │ ├── OsUtil.java │ │ └── PropertiesReader.java │ │ ├── lang │ │ ├── ClassLoaderUtil.java │ │ ├── ClassUtils.java │ │ ├── ExceptionUtils.java │ │ ├── HtmlUtils.java │ │ ├── MapsUtils.java │ │ ├── MyDateUtils.java │ │ ├── MyStringUtils.java │ │ ├── RandomString.java │ │ ├── RandomUtil.java │ │ ├── StringUtil.java │ │ └── TokenUtil.java │ │ ├── log │ │ └── LogControllerTemplate.java │ │ ├── net │ │ ├── IpUtils.java │ │ └── NetUtils.java │ │ ├── thread │ │ ├── NamedThreadFactory.java │ │ └── ThreadUtil.java │ │ └── trans │ │ ├── JsonUtils.java │ │ ├── NetBytesSwitchUtil.java │ │ └── UnsignedSwitchUtil.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── utils │ └── test │ ├── common │ └── TestCommonTestCase.java │ └── lang │ ├── ClassUtilsTestCase.java │ └── StringUtilTestCase.java ├── pack-web ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── web │ ├── aop │ ├── QpsAnnotation.java │ ├── QpsInterceptor.java │ └── helper │ │ └── QpsAopLogUtils.java │ ├── filter │ ├── HttpServletRequestFilter.java │ └── HttpServletRequestFilterCallback.java │ └── utils │ ├── CookieUtils.java │ └── WebIpUtils.java ├── pack-zk ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── knightliao │ │ └── middle │ │ └── zk │ │ ├── domain │ │ ├── WatcherWrapper.java │ │ ├── select │ │ │ └── ISelectState.java │ │ └── zk │ │ │ └── IZkClient.java │ │ ├── impl │ │ ├── LeaderLatchServer.java │ │ └── ZkClient.java │ │ └── tmp.java │ └── test │ └── java │ └── com │ └── github │ └── knightliao │ └── middle │ └── zk │ └── MasterTestMain.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /.project 2 | /.settings 3 | /target 4 | /pom.xml.releaseBackup 5 | /*.iml 6 | /.idea 7 | /.DS_Store 8 | .idea 9 | /log 10 | /vpaas-lc/vpaas-lc-server/vpaas-lc-server-start/dependency-reduced-pom.xml 11 | *.iml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: trusty 3 | 4 | after_success: 5 | - mvn clean cobertura:cobertura coveralls:report -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # middle-libs 2 | 3 | some sdks or tools for developing. 一些开发合集 4 | 5 | [![Apache License 2](https://img.shields.io/badge/license-ASF2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.txt) 6 | [![Build Status](https://travis-ci.com/knightliao/middle-libs.svg?branch=main)](https://travis-ci.com/knightliao/middle-libs) 7 | [![Coverage Status](https://coveralls.io/repos/github/knightliao/middle-libs/badge.svg)](https://coveralls.io/github/knightliao/middle-libs) 8 | 9 | - pack-redis: redis的封装包 10 | - pack-api: api相关类封装 11 | - pack-bom: 基础软件必使用包,包括 logback, 单元测试test 等 12 | - pack-http: 简单的http请求client 13 | - pack-idem: 分布式幂等控制包 14 | - pack-idgen: Id生成器 15 | - pack-lang: 基础类及领域相关 16 | - pack-lock: 锁相关包,包括分布式锁 17 | - pack-log: 日志封装包 18 | - pack-metrics: 度量统计 19 | - pack-ruleengine 规则引擎 20 | - pack-trace: 跟踪相关 21 | - pack-threadcontext: 线程上下文相关包 22 | - pack-utils: 常见工具类包 23 | - pack-web: web工具相关 24 | 25 | -------------------------------------------------------------------------------- /pack-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-api 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.github.knightliao.middle 20 | pack-lang 21 | 0.1.16-SNAPSHOT 22 | 23 | 24 | 25 | com.github.knightliao.middle 26 | pack-log 27 | 0.1.16-SNAPSHOT 28 | 29 | 30 | 31 | org.apache.commons 32 | commons-lang3 33 | 34 | 35 | 36 | com.github.knightliao.middle 37 | pack-utils 38 | 0.1.16-SNAPSHOT 39 | 40 | 41 | 42 | com.github.knightliao.middle 43 | pack-metrics 44 | 0.1.16-SNAPSHOT 45 | 46 | 47 | 48 | com.github.knightliao.middle.bom 49 | test-bom 50 | 0.1.16-SNAPSHOT 51 | test 52 | pom 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/core/callback/IMyRequesCallback.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.core.callback; 2 | 3 | import com.github.knightliao.middle.api.core.dto.MyBaseResponse; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.biz.BizException; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/8/20 12:26 10 | */ 11 | public interface IMyRequesCallback { 12 | 13 | void checkParams() throws BizException; 14 | 15 | MyBaseResponse process(); 16 | } 17 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/core/dto/MyBaseRequest.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.core.dto; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/20 13:37 7 | */ 8 | public class MyBaseRequest { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/core/dto/MyBaseResponse.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.core.dto; 2 | 3 | import lombok.Getter; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/20 13:37 9 | */ 10 | public class MyBaseResponse { 11 | 12 | public static final int PARAM_ERROR = 400; 13 | public static final int SYS_ERROR = 500; 14 | public static final String SYS_ERROR_STRING = "system error"; 15 | public static final int STATUS_OK = 1; 16 | 17 | @Getter 18 | private T data; 19 | @Getter 20 | private String message; 21 | @Getter 22 | private int status = STATUS_OK; 23 | 24 | private MyBaseResponse(T data) { 25 | this.data = data; 26 | } 27 | 28 | private MyBaseResponse() { 29 | 30 | } 31 | 32 | private MyBaseResponse(int status, String message) { 33 | this.message = message; 34 | this.status = status; 35 | } 36 | 37 | public static MyBaseResponse success(T data) { 38 | return new MyBaseResponse<>(data); 39 | } 40 | 41 | public static MyBaseResponse success() { 42 | return new MyBaseResponse(); 43 | } 44 | 45 | public static MyBaseResponse fail(int status, String message) { 46 | return new MyBaseResponse(status, message); 47 | } 48 | 49 | public static MyBaseResponse failWithParm(String message) { 50 | return new MyBaseResponse(PARAM_ERROR, message); 51 | } 52 | 53 | public static MyBaseResponse failWithSys() { 54 | return new MyBaseResponse(SYS_ERROR, SYS_ERROR_STRING); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/core/template/MyFacadeTemplate.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.core.template; 2 | 3 | import com.github.knightliao.middle.api.core.callback.IMyRequesCallback; 4 | import com.github.knightliao.middle.api.core.dto.MyBaseRequest; 5 | import com.github.knightliao.middle.api.core.dto.MyBaseResponse; 6 | import com.github.knightliao.middle.lang.constants.PackConstants; 7 | import com.github.knightliao.middle.lang.exceptions.exceptions.biz.BizException; 8 | import com.github.knightliao.middle.log.LoggerUtil; 9 | 10 | import lombok.extern.slf4j.Slf4j; 11 | 12 | /** 13 | * @author knightliao 14 | * @email knightliao@gmail.com 15 | * @date 2021/8/20 12:29 16 | */ 17 | @Slf4j 18 | public class MyFacadeTemplate { 19 | 20 | public static MyBaseResponse execute(MyBaseRequest request, IMyRequesCallback callback) { 21 | 22 | try { 23 | 24 | callback.checkParams(); 25 | 26 | return callback.process(); 27 | 28 | } catch (BizException bizException) { 29 | 30 | LoggerUtil.warn(log, "业务异常. request: {0} {1}", 31 | getBizErrorCode(bizException), request.toString()); 32 | return MyBaseResponse.failWithParm(bizException.getMessage()); 33 | 34 | } catch (Throwable throwable) { 35 | 36 | LoggerUtil.error(log, "系统异常. request: {0} {1}", 37 | PackConstants.DEFAULT_ERROR_VALUE_INT, request.toString()); 38 | return MyBaseResponse.failWithParm(throwable.toString()); 39 | } 40 | } 41 | 42 | private static String getBizErrorCode(BizException bizException) { 43 | 44 | if (bizException == null) { 45 | return PackConstants.DEFAULT_ERROR_VALUE_STRING; 46 | } 47 | 48 | return bizException.getBizErrorCode(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/rpc/annotations/RpcShardTableRoute.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.rpc.annotations; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/8/20 01:33 13 | */ 14 | @Target(ElementType.METHOD) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | public @interface RpcShardTableRoute { 18 | } 19 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/rpc/dto/BaseRpcRequest.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.rpc.dto; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/20 01:27 9 | */ 10 | @Data 11 | public class BaseRpcRequest { 12 | 13 | private long uid; 14 | 15 | public static BaseRpcRequest build(long uid) { 16 | BaseRpcRequest baseRpcRequest = new BaseRpcRequest(); 17 | baseRpcRequest.setUid(uid); 18 | return baseRpcRequest; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /pack-api/src/main/java/com/github/knightliao/middle/api/web/dto/MyBaseWebRequest.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.web.dto; 2 | 3 | import com.github.knightliao.middle.api.core.dto.MyBaseRequest; 4 | 5 | import lombok.Data; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/20 00:46 11 | */ 12 | @Data 13 | public class MyBaseWebRequest extends MyBaseRequest { 14 | 15 | private Long uid; 16 | 17 | // 系统手机语言 18 | private String lang; 19 | 20 | private String utdid; 21 | 22 | private String ver; 23 | 24 | private String country; 25 | 26 | private String ticket; 27 | 28 | private String ip; 29 | 30 | private int debug; 31 | 32 | // 分辨率 33 | private String ss; 34 | 35 | // 纬度 36 | private String lat; 37 | 38 | // 经度 39 | private String lon; 40 | } 41 | -------------------------------------------------------------------------------- /pack-api/src/test/java/com/github/knightliao/middle/api/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.api.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.api"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-bom/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /*.iml 3 | /.project 4 | /target/ 5 | /.classpath 6 | -------------------------------------------------------------------------------- /pack-bom/all-bom/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /*.iml 3 | /.project 4 | /target/ 5 | /.classpath 6 | -------------------------------------------------------------------------------- /pack-bom/all-bom/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | pack-bom 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.github.knightliao.middle.bom 13 | all-bom 14 | pom 15 | 0.1.16-SNAPSHOT 16 | 17 | 18 | 19 | 20 | com.github.knightliao.middle.bom 21 | test-bom 22 | 0.1.16-SNAPSHOT 23 | pom 24 | 25 | 26 | 27 | com.github.knightliao.middle.bom 28 | logback-bom 29 | 0.1.16-SNAPSHOT 30 | pom 31 | 32 | 33 | 34 | 35 | org.slf4j 36 | slf4j-ext 37 | ${slf4j.version} 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /pack-bom/logback-bom/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /*.iml 3 | /.project 4 | /target/ 5 | /.classpath 6 | -------------------------------------------------------------------------------- /pack-bom/logback-bom/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | pack-bom 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.github.knightliao.middle.bom 13 | logback-bom 14 | pom 15 | 0.1.16-SNAPSHOT 16 | 17 | 18 | 19 | 20 | org.slf4j 21 | jcl-over-slf4j 22 | 23 | 24 | 25 | org.slf4j 26 | jul-to-slf4j 27 | 28 | 29 | 30 | ch.qos.logback 31 | logback-classic 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | ch.qos.logback 41 | logback-classic 42 | ${logback.version} 43 | 44 | 45 | 46 | org.slf4j 47 | jcl-over-slf4j 48 | ${slf4j.version} 49 | 50 | 51 | 52 | org.slf4j 53 | jul-to-slf4j 54 | ${slf4j.version} 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /pack-bom/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-bom 13 | 0.1.16-SNAPSHOT 14 | pom 15 | 16 | 17 | logback-bom 18 | test-bom 19 | all-bom 20 | 21 | 22 | 23 | 1.7.25 24 | 1.2.5 25 | 1.0.4-SNAPSHOT 26 | 1.3 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /pack-bom/test-bom/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /*.iml 3 | /.project 4 | /target/ 5 | /.classpath 6 | -------------------------------------------------------------------------------- /pack-bom/test-bom/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | pack-bom 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.github.knightliao.middle.bom 13 | test-bom 14 | pom 15 | 0.1.16-SNAPSHOT 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 23 | 24 | 25 | org.hamcrest 26 | hamcrest-library 27 | 28 | 29 | 30 | com.github.knightliao.test 31 | jutf 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | junit 42 | junit 43 | ${junit.version} 44 | 45 | 46 | 47 | org.hamcrest 48 | hamcrest-library 49 | ${hamcrest-library.version} 50 | 51 | 52 | 53 | com.github.knightliao.test 54 | jutf 55 | ${jutf.version} 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /pack-http/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-http 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.httpcomponents 20 | httpclient 21 | 4.5.13 22 | 23 | 24 | 25 | org.apache.httpcomponents 26 | httpasyncclient 27 | 4.1.4 28 | 29 | 30 | 31 | org.apache.commons 32 | commons-lang3 33 | 34 | 35 | 36 | com.codahale.metrics 37 | metrics-core 38 | 3.0.1 39 | 40 | 41 | 42 | commons-io 43 | commons-io 44 | 2.5 45 | 46 | 47 | 48 | com.github.knightliao.middle.bom 49 | test-bom 50 | 0.1.16-SNAPSHOT 51 | pom 52 | test 53 | 54 | 55 | 56 | com.github.knightliao.middle.bom 57 | test-bom 58 | 0.1.16-SNAPSHOT 59 | test 60 | pom 61 | 62 | 63 | 64 | com.github.knightliao.middle 65 | pack-utils 66 | 0.1.16-SNAPSHOT 67 | 68 | 69 | 70 | com.github.knightliao.middle 71 | pack-metrics 72 | 0.1.16-SNAPSHOT 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/async/client/ISimpleAsyncHttpClient.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.async.client; 2 | 3 | import java.util.Map; 4 | import java.util.concurrent.CompletableFuture; 5 | import java.util.concurrent.CompletionStage; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/27 00:06 11 | */ 12 | public interface ISimpleAsyncHttpClient { 13 | 14 | CompletionStage get(String url, int timeoutMs); 15 | 16 | CompletableFuture get(String url, Map params); 17 | 18 | CompletableFuture get(String url, Map params, int timeoutMs); 19 | 20 | CompletableFuture get(String url, Map params, Map headers, int timeoutMs); 21 | 22 | CompletableFuture post(String url, Map params); 23 | 24 | CompletableFuture post(String url, Map params, int timeoutMs); 25 | 26 | CompletableFuture post(String url, Map params, String body, Map headers, 27 | int timeoutMs); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/async/client/impl/SimpleAsyncHttpClientDefaultImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.async.client.impl; 2 | 3 | import java.util.Map; 4 | import java.util.concurrent.CompletableFuture; 5 | import java.util.concurrent.CompletionStage; 6 | 7 | import com.github.knightliao.middle.http.async.client.ISimpleAsyncHttpClient; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/8/27 00:16 13 | */ 14 | public class SimpleAsyncHttpClientDefaultImpl implements ISimpleAsyncHttpClient { 15 | 16 | @Override 17 | public CompletionStage get(String url, int timeoutMs) { 18 | return null; 19 | } 20 | 21 | @Override 22 | public CompletableFuture get(String url, Map params) { 23 | return null; 24 | } 25 | 26 | @Override 27 | public CompletableFuture get(String url, Map params, int timeoutMs) { 28 | return null; 29 | } 30 | 31 | @Override 32 | public CompletableFuture get(String url, Map params, Map headers, 33 | int timeoutMs) { 34 | return null; 35 | } 36 | 37 | @Override 38 | public CompletableFuture post(String url, Map params) { 39 | return null; 40 | } 41 | 42 | @Override 43 | public CompletableFuture post(String url, Map params, int timeoutMs) { 44 | return null; 45 | } 46 | 47 | @Override 48 | public CompletableFuture post(String url, Map params, String body, 49 | Map headers, int timeoutMs) { 50 | return null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/async/utils/MyAsyncHttpUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.async.utils; 2 | 3 | import java.util.Map; 4 | import java.util.concurrent.CompletableFuture; 5 | import java.util.concurrent.CompletionStage; 6 | 7 | import org.apache.commons.io.IOUtils; 8 | import org.apache.http.HttpEntity; 9 | import org.apache.http.HttpResponse; 10 | 11 | import com.github.knightliao.middle.http.async.utils.helper.MyAsyncHttpClient; 12 | import com.github.knightliao.middle.http.common.constants.HttpConstants; 13 | import com.github.knightliao.middle.http.common.utils.HttpParamUtils; 14 | 15 | import lombok.extern.slf4j.Slf4j; 16 | 17 | /** 18 | * @author knightliao 19 | * @email knightliao@gmail.com 20 | * @date 2021/8/28 12:17 21 | */ 22 | @Slf4j 23 | public class MyAsyncHttpUtils { 24 | 25 | private static MyAsyncHttpClient[] asyncHttpClients; 26 | private static int asyncHttpCount = HttpParamUtils.asyncHttpClientCount; 27 | private static int currentWorker = 0; 28 | 29 | static { 30 | 31 | asyncHttpClients = new MyAsyncHttpClient[asyncHttpCount]; 32 | for (int i = 0; i < asyncHttpCount; ++i) { 33 | MyAsyncHttpClient newHttp = new MyAsyncHttpClient(HttpParamUtils.asyncIoPerClient, 34 | HttpParamUtils.asyncMaxConnTotal, HttpParamUtils.asyncConnPerRoute, "async-httpclient" + i); 35 | asyncHttpClients[i] = newHttp; 36 | } 37 | } 38 | 39 | public static CompletionStage get(String url, int timeoutMs) { 40 | 41 | return getMyAsyncHttpClient().get(url, timeoutMs).thenApply(httpResponse -> getContent(url, httpResponse)); 42 | } 43 | 44 | public static CompletableFuture get(String url, Map params) { 45 | 46 | return getMyAsyncHttpClient().get(url, params).thenApply(httpResponse -> getContent(url, httpResponse)); 47 | } 48 | 49 | public static CompletableFuture get(String url, Map params, int timeoutMs) { 50 | 51 | return getMyAsyncHttpClient().get(url, params, timeoutMs) 52 | .thenApply(httpResponse -> getContent(url, httpResponse)); 53 | } 54 | 55 | public static CompletionStage post(String url, Map params, String body, int timeoutMs) { 56 | 57 | return getMyAsyncHttpClient().post(url, params, body, timeoutMs).thenApply(httpResponse -> getContent(url, 58 | httpResponse)); 59 | } 60 | 61 | private static MyAsyncHttpClient getMyAsyncHttpClient() { 62 | return asyncHttpClients[Math.abs(currentWorker++ % asyncHttpCount)]; 63 | } 64 | 65 | private static String getContent(String url, HttpResponse httpResponse) { 66 | 67 | try { 68 | String encoding = processEncode(httpResponse.getEntity()); 69 | String content = IOUtils.toString(httpResponse.getEntity().getContent(), encoding); 70 | 71 | log.debug("url={} content_size={} encoding={}", url, content.length(), encoding); 72 | return content; 73 | } catch (Exception e) { 74 | log.error(e.toString(), e); 75 | return "{}"; 76 | } 77 | } 78 | 79 | private static String processEncode(HttpEntity entity) { 80 | 81 | try { 82 | return entity.getContentType().getValue().split(";")[1].split("=")[1]; 83 | } catch (Exception e) { 84 | return HttpConstants.CHARSET; 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/async/utils/helper/MyAsyncHttpClientHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.async.utils.helper; 2 | 3 | import java.io.IOException; 4 | import java.util.concurrent.CancellationException; 5 | import java.util.concurrent.CompletableFuture; 6 | import java.util.concurrent.CompletionStage; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.ScheduledExecutorService; 9 | import java.util.concurrent.TimeUnit; 10 | import java.util.concurrent.TimeoutException; 11 | 12 | import org.apache.http.HttpException; 13 | import org.apache.http.HttpRequest; 14 | import org.apache.http.HttpRequestInterceptor; 15 | import org.apache.http.HttpResponse; 16 | import org.apache.http.HttpResponseInterceptor; 17 | import org.apache.http.protocol.HttpContext; 18 | 19 | import com.github.knightliao.middle.http.common.utils.HttpParamUtils; 20 | import com.github.knightliao.middle.utils.thread.NamedThreadFactory; 21 | 22 | import lombok.extern.slf4j.Slf4j; 23 | 24 | /** 25 | * @author knightliao 26 | * @email knightliao@gmail.com 27 | * @date 2021/8/28 12:35 28 | */ 29 | @Slf4j 30 | public class MyAsyncHttpClientHelper { 31 | 32 | private static ScheduledExecutorService scheduledThreadPoolExecutor = Executors.newScheduledThreadPool( 33 | HttpParamUtils.cpuProcessorCount, new NamedThreadFactory("async-timeout", true)); 34 | 35 | private static CompletionStage timeout(long timeout) { 36 | 37 | CompletableFuture completableFuture = new CompletableFuture<>(); 38 | scheduledThreadPoolExecutor.schedule(() -> { 39 | completableFuture.completeExceptionally(new TimeoutException()); 40 | }, timeout, TimeUnit.MILLISECONDS); 41 | 42 | return completableFuture; 43 | } 44 | 45 | public static CompletableFuture timeout(CompletionStage completionStage, long timeoutMs, String... msg) { 46 | 47 | if (completionStage.toCompletableFuture().isDone()) { 48 | return completionStage.toCompletableFuture(); 49 | } 50 | 51 | CompletionStage resultCompleteStage = completionStage.applyToEither(timeout(timeoutMs), t -> t); 52 | 53 | resultCompleteStage.exceptionally(e -> { 54 | try { 55 | completionStage.toCompletableFuture().cancel(true); 56 | } catch (CancellationException ignore) { 57 | 58 | } 59 | if (msg.length > 0) { 60 | log.warn("{} timeout {}", msg, e.toString()); 61 | } 62 | return null; 63 | }); 64 | 65 | CompletableFuture future = resultCompleteStage.toCompletableFuture(); 66 | 67 | return future; 68 | } 69 | 70 | public static HttpRequestInterceptor createRequestInterceptor() { 71 | 72 | return new HttpRequestInterceptor() { 73 | 74 | @Override 75 | public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { 76 | } 77 | }; 78 | } 79 | 80 | public static HttpResponseInterceptor createResponseInterceptor() { 81 | return new HttpResponseInterceptor() { 82 | 83 | @Override 84 | public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { 85 | 86 | } 87 | }; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/constants/HttpConstants.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.constants; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/8/23 10:39 10 | */ 11 | public class HttpConstants { 12 | 13 | public static final Logger logger = LoggerFactory.getLogger("SIMPLE_HTTP_CLIENT"); 14 | 15 | public static final String CHARSET = "UTF-8"; 16 | } 17 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/exceptions/NoServerAvailableException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.exceptions; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/23 12:41 7 | */ 8 | public class NoServerAvailableException extends RuntimeException { 9 | 10 | public NoServerAvailableException() { 11 | super(); 12 | } 13 | 14 | public NoServerAvailableException(String message, Throwable cause) { 15 | super(message, cause); 16 | } 17 | 18 | public NoServerAvailableException(String message) { 19 | super(message); 20 | } 21 | 22 | public NoServerAvailableException(Throwable throwable) { 23 | super(throwable); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/loadbalance/ILoadBalancer.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.loadbalance; 2 | 3 | import java.util.List; 4 | 5 | import com.github.knightliao.middle.http.common.service.server.helper.server.ServerStatus; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/23 11:49 11 | */ 12 | public interface ILoadBalancer { 13 | 14 | ServerStatus select(List serverStatusList); 15 | } 16 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/loadbalance/impl/RandomLoadBalancerImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.loadbalance.impl; 2 | 3 | import java.util.List; 4 | import java.util.Random; 5 | 6 | import com.github.knightliao.middle.http.common.service.loadbalance.ILoadBalancer; 7 | import com.github.knightliao.middle.http.common.service.server.helper.server.ServerStatus; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/8/23 11:53 13 | */ 14 | public class RandomLoadBalancerImpl implements ILoadBalancer { 15 | 16 | private Random random = new Random(this.hashCode()); 17 | 18 | @Override 19 | public ServerStatus select(List serverStatusList) { 20 | 21 | int index = random.nextInt(serverStatusList.size()); 22 | return serverStatusList.get(index); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/loadbalance/impl/RoundRobinLoadBalancerImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.loadbalance.impl; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | import com.github.knightliao.middle.http.common.service.loadbalance.ILoadBalancer; 7 | import com.github.knightliao.middle.http.common.service.server.helper.server.ServerStatus; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/8/23 11:50 13 | */ 14 | public class RoundRobinLoadBalancerImpl implements ILoadBalancer { 15 | 16 | private static AtomicInteger counter = new AtomicInteger(); 17 | 18 | @Override 19 | public ServerStatus select(List serverStatusList) { 20 | 21 | int count = counter.incrementAndGet(); 22 | if (count < 0) { 23 | count = 0; 24 | counter.set(0); 25 | } 26 | 27 | int index = count % serverStatusList.size(); 28 | return serverStatusList.get(index); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/server/IMyHttpServer.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.server; 2 | 3 | import java.util.List; 4 | 5 | import com.github.knightliao.middle.http.common.service.server.helper.server.ServerStatus; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/23 10:32 11 | */ 12 | public interface IMyHttpServer { 13 | 14 | ServerStatus getServer(); 15 | 16 | List getAllAvailable(); 17 | 18 | String getName(); 19 | } 20 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/server/helper/server/ServerInstance.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.server.helper.server; 2 | 3 | import java.io.IOException; 4 | import java.net.InetSocketAddress; 5 | import java.net.Socket; 6 | 7 | import com.github.knightliao.middle.http.common.constants.HttpConstants; 8 | 9 | import lombok.Getter; 10 | import lombok.Setter; 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | /** 14 | * @author knightliao 15 | * @email knightliao@gmail.com 16 | * @date 2021/8/23 10:33 17 | */ 18 | @Getter 19 | @Slf4j 20 | public class ServerInstance { 21 | 22 | private String serviceName; 23 | private String id; 24 | private String hostAndPort; 25 | private String url; 26 | 27 | @Setter 28 | private boolean available; 29 | 30 | // 不可用持续时长 31 | private long shortCircuitDuration; 32 | // 是否不可用 33 | private volatile boolean shortCircuited = false; 34 | // 不可用超时时间 35 | private volatile long shortCircuitExpiration; 36 | 37 | public ServerInstance(String serviceName, String hostPort, boolean isSec, long duration) { 38 | this.serviceName = serviceName; 39 | this.id = hostPort; 40 | this.shortCircuitDuration = duration; 41 | this.hostAndPort = hostPort; 42 | 43 | if (isSec) { 44 | this.url = "https://" + hostPort; 45 | } else { 46 | this.url = "http://" + hostPort; 47 | } 48 | } 49 | 50 | public void triggerShortOff() { 51 | if (shortCircuited) { 52 | return; 53 | } 54 | 55 | final long now = System.currentTimeMillis(); 56 | shortCircuitExpiration = now + shortCircuitDuration; 57 | shortCircuited = true; 58 | 59 | HttpConstants.logger.info("triggerShortOff {} TRIGGER_OFFLINE {} CAN_ONLINE {} EXPIRATION", 60 | hostAndPort, shortCircuitDuration, shortCircuitExpiration); 61 | } 62 | 63 | public boolean checkAvaiable() { 64 | return available && isShortCircuitOver(); 65 | } 66 | 67 | private boolean isShortCircuitOver() { 68 | if (shortCircuited) { 69 | long now = System.currentTimeMillis() - shortCircuitExpiration; 70 | if (now >= 0 && ping()) { 71 | shortCircuited = false; 72 | HttpConstants.logger.info("isShortCircuitOver {} online", hostAndPort); 73 | } 74 | } 75 | 76 | return !shortCircuited; 77 | } 78 | 79 | public boolean ping() { 80 | 81 | Socket socket = new Socket(); 82 | 83 | try { 84 | 85 | String[] strs = hostAndPort.split(":"); 86 | InetSocketAddress inetSocketAddress = new InetSocketAddress(strs[0], Integer.parseInt(strs[1])); 87 | socket.connect(inetSocketAddress, 100); 88 | return socket.isConnected(); 89 | 90 | } catch (Exception ex) { 91 | return false; 92 | } finally { 93 | 94 | if (socket != null) { 95 | try { 96 | socket.close(); 97 | } catch (IOException e) { 98 | } 99 | } 100 | } 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/server/helper/server/ServerStatus.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.server.helper.server; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import com.codahale.metrics.Clock; 6 | import com.codahale.metrics.SlidingTimeWindowReservoir; 7 | 8 | import lombok.Getter; 9 | 10 | /** 11 | * @author knightliao 12 | * @email knightliao@gmail.com 13 | * @date 2021/8/23 10:32 14 | */ 15 | public class ServerStatus { 16 | 17 | private static final int ERROR_THRESHOLD = 10; 18 | 19 | @Getter 20 | protected ServerInstance serverInstance; 21 | 22 | protected SlidingTimeWindowReservoir errorPerSecond; 23 | 24 | public void init(ServerInstance serverInstance) { 25 | this.serverInstance = serverInstance; 26 | this.errorPerSecond = new SlidingTimeWindowReservoir(1, TimeUnit.SECONDS, new Clock() { 27 | private long start = System.nanoTime(); 28 | 29 | @Override 30 | public long getTick() { 31 | return System.nanoTime() - start; 32 | } 33 | }); 34 | } 35 | 36 | public void incrErrors() { 37 | 38 | errorPerSecond.update(1); 39 | 40 | int errorCount = errorPerSecond.size(); 41 | if (errorCount >= ERROR_THRESHOLD) { 42 | serverInstance.triggerShortOff(); 43 | } 44 | } 45 | 46 | public void connectTimeout() { 47 | serverInstance.triggerShortOff(); 48 | } 49 | 50 | public void checkAfterException() { 51 | if (!serverInstance.ping()) { 52 | serverInstance.triggerShortOff(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/server/helper/server/ServerStatusFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.server.helper.server; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/23 11:43 7 | */ 8 | public class ServerStatusFactory { 9 | 10 | public static ServerStatus getServerStatus(ServerInstance serverInstance) { 11 | ServerStatus serverStatus = new ServerStatus(); 12 | serverStatus.init(serverInstance); 13 | return serverStatus; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/server/helper/serverlist/DefaultServerInstanceList.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.server.helper.serverlist; 2 | 3 | import static java.util.Arrays.stream; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.stream.Collectors; 8 | 9 | import org.apache.commons.lang3.StringUtils; 10 | 11 | import com.github.knightliao.middle.http.common.service.server.helper.server.ServerInstance; 12 | 13 | /** 14 | * @author knightliao 15 | * @email knightliao@gmail.com 16 | * @date 2021/8/23 11:03 17 | */ 18 | public class DefaultServerInstanceList implements IServerInstanceList { 19 | 20 | private String serverName; 21 | private List list = new ArrayList<>(); 22 | 23 | public DefaultServerInstanceList(String serverName, String serverList, boolean isSec, long offDuration) { 24 | 25 | this.serverName = serverName; 26 | 27 | if (StringUtils.isEmpty(serverList)) { 28 | throw new RuntimeException("serverList is empty"); 29 | } 30 | 31 | // 32 | list = stream(serverList.split(",")) 33 | .map(host -> host.split(":")) 34 | .filter(strings -> strings.length == 2) 35 | .map(strings -> 36 | new ServerInstance(serverName, 37 | strings[0] + ":" + Integer.valueOf(strings[1]), 38 | isSec, offDuration)) 39 | .peek(item -> item.setAvailable(true)) 40 | .collect(Collectors.toList()); 41 | } 42 | 43 | @Override 44 | public String serverName() { 45 | return serverName; 46 | } 47 | 48 | @Override 49 | public List serverInstanceList() { 50 | return list; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/service/server/helper/serverlist/IServerInstanceList.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.service.server.helper.serverlist; 2 | 3 | import java.util.List; 4 | 5 | import com.github.knightliao.middle.http.common.service.server.helper.server.ServerInstance; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/23 11:01 11 | */ 12 | public interface IServerInstanceList { 13 | 14 | String serverName(); 15 | 16 | List serverInstanceList(); 17 | } 18 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/utils/ConnectionMonitorUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.utils; 2 | 3 | import java.util.concurrent.ScheduledThreadPoolExecutor; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 7 | import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; 8 | import org.apache.http.pool.PoolStats; 9 | 10 | import com.github.knightliao.middle.utils.thread.NamedThreadFactory; 11 | 12 | import lombok.extern.slf4j.Slf4j; 13 | 14 | /** 15 | * @author knightliao 16 | * @email knightliao@gmail.com 17 | * @date 2021/8/28 12:46 18 | */ 19 | @Slf4j 20 | public class ConnectionMonitorUtils { 21 | 22 | private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = 23 | new ScheduledThreadPoolExecutor(1, 24 | new NamedThreadFactory(ConnectionMonitorUtils.class.getName(), true)); 25 | 26 | public static void watchAsyncPool(String name, 27 | PoolingNHttpClientConnectionManager poolingNHttpClientConnectionManager) { 28 | 29 | scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> { 30 | 31 | PoolStats poolStats = poolingNHttpClientConnectionManager.getTotalStats(); 32 | 33 | // 34 | printPoolStatus("watchAsyncPool", name + "-" + "total", poolStats); 35 | poolingNHttpClientConnectionManager.getRoutes().forEach(route -> { 36 | PoolStats subStats = poolingNHttpClientConnectionManager.getStats(route); 37 | printPoolStatus("watchAsyncPool", name + "-" + route.getTargetHost().getHostName(), subStats); 38 | }); 39 | }, 1, 1, TimeUnit.MINUTES); 40 | } 41 | 42 | public static void watchPool(String name, 43 | PoolingHttpClientConnectionManager poolingHttpClientConnectionManager) { 44 | 45 | scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> { 46 | 47 | PoolStats poolStats = poolingHttpClientConnectionManager.getTotalStats(); 48 | 49 | // 50 | printPoolStatus("watchPool", name + "-" + "total", poolStats); 51 | poolingHttpClientConnectionManager.getRoutes().forEach(route -> { 52 | PoolStats subStats = poolingHttpClientConnectionManager.getStats(route); 53 | printPoolStatus("watchPool", name + "-" + route.getTargetHost().getHostName(), subStats); 54 | }); 55 | }, 1, 1, TimeUnit.MINUTES); 56 | 57 | } 58 | 59 | private static void printPoolStatus(String type, String name, PoolStats poolStats) { 60 | 61 | log.debug("connection_monitor={} name={}, pending={} available={} leased={} max={}", 62 | type, 63 | name, poolStats.getPending(), 64 | poolStats.getAvailable(), 65 | poolStats.getLeased(), poolStats.getMax()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/common/utils/HttpMetricMonitor.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.common.utils; 2 | 3 | import com.github.knightliao.middle.metrics.MonitorHelper; 4 | 5 | import lombok.extern.slf4j.Slf4j; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/28 13:51 11 | */ 12 | @Slf4j 13 | public class HttpMetricMonitor { 14 | 15 | public static void commitMonitor(String prefix, String node, long cost, boolean isSucc) { 16 | 17 | try { 18 | 19 | String costStr = null; 20 | if (cost == 0) { 21 | costStr = "0"; 22 | } else if (cost <= 1) { 23 | costStr = "0_1"; 24 | } else if (cost <= 5) { 25 | costStr = "1_5"; 26 | } else if (cost <= 10) { 27 | costStr = "5_10"; 28 | } else { 29 | costStr = "10_"; 30 | } 31 | 32 | MonitorHelper.doMeter(prefix, isSucc, node, costStr); 33 | 34 | } catch (Exception e) { 35 | log.error(e.toString(), e); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/client/ISimpleHttpClient.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.client; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.apache.http.NameValuePair; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/23 10:25 12 | */ 13 | public interface ISimpleHttpClient { 14 | 15 | String post(String url, List params, int connectionTimeout, int readTimeout); 16 | 17 | String post(String url, List params); 18 | 19 | String post(String url, Map params, int connectionTimeout, int readTimeout); 20 | 21 | String post(String url, Map params, Map headers, 22 | int connectionTimeout, int readTimeout); 23 | 24 | String post(String url, String content, 25 | int connectionTimeout, int readTimeout); 26 | 27 | String get(String url, List params, int connectionTimeout, int readTimeout); 28 | 29 | String get(String url, int connectionTimeout, int readTimeout); 30 | } 31 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/client/SimpleHttpSupport.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.client; 2 | 3 | import com.github.knightliao.middle.http.sync.client.impl.SimpleHttpClientDefaultImpl; 4 | import com.github.knightliao.middle.http.common.service.loadbalance.ILoadBalancer; 5 | import com.github.knightliao.middle.http.common.service.loadbalance.impl.RoundRobinLoadBalancerImpl; 6 | import com.github.knightliao.middle.http.common.service.server.IMyHttpServer; 7 | import com.github.knightliao.middle.http.common.service.server.helper.serverlist.DefaultServerInstanceList; 8 | import com.github.knightliao.middle.http.common.service.server.helper.serverlist.IServerInstanceList; 9 | import com.github.knightliao.middle.http.common.service.server.impl.MyHttpServerImpl; 10 | 11 | /** 12 | * @author knightliao 13 | * @email knightliao@gmail.com 14 | * @date 2021/8/23 14:25 15 | */ 16 | public abstract class SimpleHttpSupport { 17 | 18 | protected ISimpleHttpClient simpleHttpClient; 19 | protected int connectionTimeout = 200; 20 | protected int readTimeout = 200; 21 | 22 | protected void init(String serverName, String host, boolean isHttps, int numRetries) { 23 | 24 | // 25 | IServerInstanceList serverInstanceList = new DefaultServerInstanceList(serverName, host, isHttps, 5000); 26 | ILoadBalancer loadBalancer = new RoundRobinLoadBalancerImpl(); 27 | 28 | // 29 | MyHttpServerImpl.Builder builder = new MyHttpServerImpl.Builder(); 30 | builder.setLoadBalancer(loadBalancer); 31 | builder.setRefreshIntervalTime(10 * 1000); 32 | builder.setServerList(serverInstanceList); 33 | builder.setServiceName(serverName); 34 | IMyHttpServer myHttpServer = builder.build(); 35 | 36 | // 37 | simpleHttpClient = new SimpleHttpClientDefaultImpl(myHttpServer, numRetries); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/client/impl/helper/IFunctionWrapper.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.client.impl.helper; 2 | 3 | import java.io.IOException; 4 | 5 | import org.apache.http.client.methods.CloseableHttpResponse; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/23 12:21 11 | */ 12 | public interface IFunctionWrapper { 13 | 14 | CloseableHttpResponse execute(String url) throws IOException; 15 | } 16 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/utils/MyHttpUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.utils; 2 | 3 | import java.io.IOException; 4 | import java.rmi.ServerException; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.apache.commons.io.IOUtils; 9 | import org.apache.http.HttpEntity; 10 | import org.apache.http.NameValuePair; 11 | import org.apache.http.StatusLine; 12 | import org.apache.http.client.methods.CloseableHttpResponse; 13 | import org.apache.http.util.EntityUtils; 14 | 15 | import com.github.knightliao.middle.http.sync.utils.helper.MyHttpRawUtils; 16 | 17 | import lombok.extern.slf4j.Slf4j; 18 | 19 | /** 20 | * @author knightliao 21 | * @email knightliao@gmail.com 22 | * @date 2021/8/23 21:15 23 | */ 24 | @Slf4j 25 | public class MyHttpUtils { 26 | 27 | // 28 | public static String get(String url, List params, int connectTimeout, 29 | int readTimeout) throws IOException { 30 | 31 | return getResult(url, MyHttpRawUtils.get(url, params, connectTimeout, readTimeout)); 32 | } 33 | 34 | // 35 | public static String get(String url, int connectTimeout, 36 | int readTimeout) throws IOException { 37 | 38 | return getResult(url, MyHttpRawUtils.get(url, connectTimeout, readTimeout)); 39 | } 40 | 41 | // 42 | public static String post(String url, List params, int connectTimeout, 43 | int readTimeout) throws IOException { 44 | return getResult(url, MyHttpRawUtils.post(url, params, connectTimeout, readTimeout)); 45 | } 46 | 47 | public static String post(String url, Map params, int connectTimeout, 48 | int readTimeout) throws IOException { 49 | return getResult(url, MyHttpRawUtils.post(url, params, connectTimeout, readTimeout)); 50 | } 51 | 52 | public static String post(String url, Map params, 53 | Map headers, int connectTimeout, 54 | int readTimeout) throws IOException { 55 | return getResult(url, MyHttpRawUtils.post(url, params, headers, connectTimeout, readTimeout)); 56 | } 57 | 58 | public static String post(String url, String content, int connectTimeout, 59 | int readTimeout) throws IOException { 60 | return getResult(url, MyHttpRawUtils.post(url, content, connectTimeout, readTimeout)); 61 | } 62 | 63 | private static String getResult(String url, CloseableHttpResponse closeableHttpResponse) throws IOException { 64 | 65 | try { 66 | 67 | // 68 | StatusLine statusLine = closeableHttpResponse.getStatusLine(); 69 | if (statusLine.getStatusCode() != 200) { 70 | log.warn("http status {}", statusLine.getStatusCode()); 71 | throw new ServerException("server internal error " + url); 72 | } 73 | 74 | // 75 | HttpEntity entity = closeableHttpResponse.getEntity(); 76 | if (entity != null) { 77 | String data = EntityUtils.toString(entity); 78 | EntityUtils.consume(entity); 79 | return data; 80 | } else { 81 | log.warn("http entity null {}", url); 82 | } 83 | 84 | } finally { 85 | 86 | IOUtils.closeQuietly(closeableHttpResponse); 87 | } 88 | 89 | return null; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/utils/helper/MyHttpClient.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.utils.helper; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/28 13:05 9 | */ 10 | @Slf4j 11 | public class MyHttpClient extends MyAbstractHttpClient { 12 | 13 | public MyHttpClient(int total, int connPerRoute, String prefix) { 14 | super(total, connPerRoute, prefix); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/utils/helper/MyHttpRawUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.utils.helper; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.apache.http.NameValuePair; 9 | import org.apache.http.client.methods.CloseableHttpResponse; 10 | import org.apache.http.message.BasicNameValuePair; 11 | 12 | import com.github.knightliao.middle.http.common.utils.HttpParamUtils; 13 | 14 | import lombok.extern.slf4j.Slf4j; 15 | 16 | /** 17 | * @author knightliao 18 | * @email knightliao@gmail.com 19 | * @date 2021/8/23 01:10 20 | */ 21 | @Slf4j 22 | public class MyHttpRawUtils { 23 | 24 | // 每个连接最多的并发 25 | private static final int MAX_PRE_ROUTE = HttpParamUtils.syncConnPerRoute; 26 | // 全局最多的并发 27 | private static final int MAX_TOTAL_ROUTE = HttpParamUtils.syncMaxConnTotal; 28 | 29 | private static MyHttpClient httpClient; 30 | 31 | static { 32 | 33 | httpClient = new MyHttpClient(MAX_TOTAL_ROUTE, MAX_PRE_ROUTE, "my_http_client"); 34 | } 35 | 36 | // 37 | public static CloseableHttpResponse get(String url, List params, int connectTimeout, 38 | int readTimeout) throws IOException { 39 | 40 | return httpClient.httpExe( 41 | MyHttpRawUtilsHelper.getHttpGet(url, params), connectTimeout, readTimeout); 42 | } 43 | 44 | // 45 | public static CloseableHttpResponse get(String url, int connectTimeout, 46 | int readTimeout) throws IOException { 47 | 48 | return get(url, null, connectTimeout, readTimeout); 49 | } 50 | 51 | // 52 | public static CloseableHttpResponse post(String url, List params, int connectTimeout, 53 | int readTimeout) throws IOException { 54 | 55 | return httpClient.httpExe( 56 | MyHttpRawUtilsHelper.getHttpPost(url, params, null), connectTimeout, readTimeout); 57 | } 58 | 59 | public static CloseableHttpResponse post(String url, Map params, int connectTimeout, 60 | int readTimeout) throws IOException { 61 | 62 | return post(url, params, null, connectTimeout, readTimeout); 63 | } 64 | 65 | public static CloseableHttpResponse post(String url, Map params, 66 | Map headers, int connectTimeout, 67 | int readTimeout) throws IOException { 68 | 69 | List pairs = new ArrayList<>(params.size()); 70 | for (String key : params.keySet()) { 71 | pairs.add(new BasicNameValuePair(key, params.get(key).toString())); 72 | } 73 | 74 | return httpClient.httpExe( 75 | MyHttpRawUtilsHelper.getHttpPost(url, pairs, headers), connectTimeout, readTimeout); 76 | } 77 | 78 | public static CloseableHttpResponse post(String url, String content, int connectTimeout, 79 | int readTimeout) throws IOException { 80 | 81 | return httpClient.httpExe( 82 | MyHttpRawUtilsHelper.getHttpPost(url, content), connectTimeout, readTimeout); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /pack-http/src/main/java/com/github/knightliao/middle/http/sync/utils/helper/MyHttpRawUtilsHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.sync.utils.helper; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.apache.commons.lang3.StringUtils; 7 | import org.apache.http.NameValuePair; 8 | import org.apache.http.client.entity.UrlEncodedFormEntity; 9 | import org.apache.http.client.methods.HttpGet; 10 | import org.apache.http.client.methods.HttpPost; 11 | import org.apache.http.client.utils.URLEncodedUtils; 12 | import org.apache.http.entity.ContentType; 13 | import org.apache.http.entity.StringEntity; 14 | 15 | import com.github.knightliao.middle.http.common.constants.HttpConstants; 16 | 17 | import lombok.extern.slf4j.Slf4j; 18 | 19 | /** 20 | * @author knightliao 21 | * @email knightliao@gmail.com 22 | * @date 2021/8/23 09:51 23 | */ 24 | @Slf4j 25 | public class MyHttpRawUtilsHelper { 26 | 27 | protected static HttpGet getHttpGet(String url, List params) { 28 | 29 | StringBuilder sb = new StringBuilder(url); 30 | if (StringUtils.containsNone(url, "?")) { 31 | sb.append("?"); 32 | } 33 | 34 | if (params != null) { 35 | String paramStr = URLEncodedUtils.format(params, HttpConstants.CHARSET); 36 | sb.append(paramStr); 37 | } 38 | 39 | return new HttpGet(sb.toString()); 40 | } 41 | 42 | protected static HttpPost getHttpPost(String url, List params, Map headers) { 43 | 44 | try { 45 | HttpPost post = new HttpPost(url); 46 | if (headers != null && headers.size() > 0) { 47 | headers.forEach(post::setHeader); 48 | } 49 | 50 | post.setEntity(new UrlEncodedFormEntity(params, HttpConstants.CHARSET)); 51 | return post; 52 | } catch (Throwable ex) { 53 | log.error(ex.getMessage(), ex); 54 | return null; 55 | } 56 | } 57 | 58 | protected static HttpPost getHttpPost(String url, String content) { 59 | 60 | HttpPost post = new HttpPost(url); 61 | 62 | ContentType contentType = ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), HttpConstants.CHARSET); 63 | post.setEntity(new StringEntity(content, contentType)); 64 | 65 | return post; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /pack-http/src/test/java/com/github/knightliao/middle/http/test/async/BaiduHomePageAsyncFetchTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.test.async; 2 | 3 | import java.util.concurrent.CompletionStage; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import com.github.knightliao.middle.http.async.utils.MyAsyncHttpUtils; 9 | 10 | /** 11 | * @author knightliao 12 | * @email knightliao@gmail.com 13 | * @date 2021/8/23 14:39 14 | */ 15 | public class BaiduHomePageAsyncFetchTestCase { 16 | 17 | @Test 18 | public void test() { 19 | 20 | CompletionStage content = MyAsyncHttpUtils.get("http://www.baidu.com", 5000); 21 | 22 | try { 23 | 24 | String data = content.toCompletableFuture().get(); 25 | Assert.assertNotEquals(data, ""); 26 | 27 | } catch (Exception e) { 28 | 29 | System.out.println(e.toString()); 30 | Assert.fail(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /pack-http/src/test/java/com/github/knightliao/middle/http/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.http"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-http/src/test/java/com/github/knightliao/middle/http/test/sync/BaiduHomePageFetchTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.http.test.sync; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import com.github.knightliao.middle.http.sync.client.SimpleHttpSupport; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/23 14:39 12 | */ 13 | public class BaiduHomePageFetchTestCase { 14 | 15 | public static class BaiduHomePageFetcher extends SimpleHttpSupport { 16 | 17 | public BaiduHomePageFetcher(String apiHost, int numRetries) { 18 | init("baidu", apiHost, false, numRetries); 19 | } 20 | 21 | public String getContent() { 22 | 23 | String content = simpleHttpClient.get("/", 200, 200); 24 | 25 | return content; 26 | } 27 | } 28 | 29 | @Test 30 | public void test() { 31 | 32 | BaiduHomePageFetcher baiduHomePageFetcher = new BaiduHomePageFetcher("www.baidu.com:80", 3); 33 | 34 | String content = baiduHomePageFetcher.getContent(); 35 | 36 | Assert.assertNotEquals(content, ""); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pack-idem/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-idem 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.github.knightliao.middle 20 | pack-redis 21 | 0.1.16-SNAPSHOT 22 | 23 | 24 | 25 | com.github.knightliao.middle 26 | pack-lock 27 | 0.1.16-SNAPSHOT 28 | 29 | 30 | 31 | com.github.knightliao.middle.bom 32 | test-bom 33 | 0.1.16-SNAPSHOT 34 | test 35 | pom 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /pack-idem/src/main/java/com/github/knightliao/middle/idem/IIdemService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idem; 2 | 3 | import com.github.knightliao.middle.idem.service.callback.IIdemBizCallback; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.param.IdemException; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/8/12 12:18 10 | */ 11 | public interface IIdemService { 12 | 13 | T executeWithResult(String idemKey, IIdemBizCallback callback, int lockTimeMills, Class myClass); 14 | 15 | boolean execute(String idemKey, IIdemBizCallback callback, int lockTimeMills) throws IdemException; 16 | } 17 | -------------------------------------------------------------------------------- /pack-idem/src/main/java/com/github/knightliao/middle/idem/service/callback/IIdemBizCallback.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idem.service.callback; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/12 14:26 7 | */ 8 | public interface IIdemBizCallback { 9 | 10 | // 执行 11 | T process(); 12 | } 13 | -------------------------------------------------------------------------------- /pack-idem/src/main/java/com/github/knightliao/middle/idem/service/helper/IIdemServiceHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idem.service.helper; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.exceptions.param.IdemException; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/12 14:39 9 | */ 10 | public interface IIdemServiceHelper { 11 | 12 | // 幂等性校验 13 | String checkIdem(String key) throws IdemException; 14 | 15 | // 存储幂等 16 | void saveIdem(String key, String value); 17 | } 18 | -------------------------------------------------------------------------------- /pack-idem/src/main/java/com/github/knightliao/middle/idem/service/helper/impl/IdemServiceRedisHelperImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idem.service.helper.impl; 2 | 3 | import com.github.knightliao.middle.idem.service.helper.IIdemServiceHelper; 4 | import com.github.knightliao.middle.idem.support.key.IdemKeyUtils; 5 | import com.github.knightliao.middle.lang.exceptions.exceptions.param.IdemException; 6 | import com.github.knightliao.middle.redis.IMyRedisService; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/12 14:40 12 | */ 13 | public class IdemServiceRedisHelperImpl implements IIdemServiceHelper { 14 | 15 | private IMyRedisService myRedisService; 16 | 17 | public IdemServiceRedisHelperImpl(IMyRedisService myRedisService) { 18 | this.myRedisService = myRedisService; 19 | } 20 | 21 | @Override 22 | public String checkIdem(String key) throws IdemException { 23 | 24 | String redisKey = IdemKeyUtils.getIdemKey(key); 25 | 26 | return myRedisService.get(redisKey); 27 | } 28 | 29 | @Override 30 | public void saveIdem(String key, String value) { 31 | 32 | String redisKey = IdemKeyUtils.getIdemKey(key); 33 | 34 | myRedisService.set(redisKey, IdemKeyUtils.getIdemKeyExpire, value); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pack-idem/src/main/java/com/github/knightliao/middle/idem/support/key/IdemKeyUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idem.support.key; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/12 14:43 7 | */ 8 | public class IdemKeyUtils { 9 | 10 | // 3小时 11 | public static final int getIdemKeyExpire = 60 * 60 * 3; 12 | 13 | // 14 | public static String getIdemKey(String idemKey) { 15 | 16 | return String.format("idem_%s", idemKey); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-idem/src/test/java/com/github/knightliao/middle/idem/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idem.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.idem"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-idgen/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-idgen 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.github.knightliao.middle 20 | pack-redis 21 | 0.1.16-SNAPSHOT 22 | 23 | 24 | 25 | com.github.knightliao.middle.bom 26 | test-bom 27 | 0.1.16-SNAPSHOT 28 | test 29 | pom 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /pack-idgen/src/main/java/com/github/knightliao/middle/idgen/IIdgenService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idgen; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/13 15:04 7 | */ 8 | public interface IIdgenService { 9 | 10 | Long getSequenceId(long key); 11 | 12 | Long getSequenceId(String key); 13 | } 14 | -------------------------------------------------------------------------------- /pack-idgen/src/main/java/com/github/knightliao/middle/idgen/aop/IdGenAop.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idgen.aop; 2 | 3 | import org.apache.commons.lang3.time.StopWatch; 4 | import org.aspectj.lang.ProceedingJoinPoint; 5 | import org.aspectj.lang.annotation.Around; 6 | import org.aspectj.lang.annotation.Aspect; 7 | import org.aspectj.lang.annotation.Pointcut; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import com.github.knightliao.middle.log.LoggerUtil; 12 | import com.github.knightliao.middle.metrics.MonitorHelper; 13 | 14 | import lombok.Setter; 15 | import lombok.extern.slf4j.Slf4j; 16 | 17 | /** 18 | * @author knightliao 19 | * @email knightliao@gmail.com 20 | * @date 2021/8/19 17:00 21 | */ 22 | @Slf4j 23 | @Aspect 24 | public class IdGenAop { 25 | 26 | private static final Logger logger = LoggerFactory.getLogger("MIDDLE_IDGEN_LOG"); 27 | 28 | @Setter 29 | private boolean debug = false; 30 | 31 | @Setter 32 | private boolean metricStatistic = false; 33 | 34 | public IdGenAop() { 35 | } 36 | 37 | @Pointcut("execution(public * com.github.knightliao.middle.idgen.IIdgenService.*(..)) ") 38 | public void iidgenServicePoint() { 39 | 40 | } 41 | 42 | @Around("iidgenServicePoint()") 43 | public Object logExecuteJob(ProceedingJoinPoint joinPoint) throws Throwable { 44 | 45 | StopWatch stopWatch = new StopWatch(); 46 | stopWatch.start(); 47 | 48 | String key = ""; 49 | Object ret = ""; 50 | boolean success = true; 51 | 52 | try { 53 | 54 | // 55 | ret = joinPoint.proceed(); 56 | return ret; 57 | 58 | } catch (Throwable ex) { 59 | 60 | success = false; 61 | throw ex; 62 | 63 | } finally { 64 | 65 | if (debug) { 66 | try { 67 | doLog(joinPoint, key, ret, stopWatch, success); 68 | } catch (Exception ex) { 69 | log.error(ex.toString(), ex); 70 | } 71 | } 72 | } 73 | } 74 | 75 | protected void doLog(final ProceedingJoinPoint joinPoint, final String key, Object ret, 76 | final StopWatch stopWatch, final boolean success) { 77 | 78 | String methodName = joinPoint.getSignature().getName(); 79 | stopWatch.stop(); 80 | String result = String.valueOf(ret); 81 | 82 | if (debug) { 83 | LoggerUtil.info(logger, "* [{0}] {1} {2} {3} {4}", 84 | methodName, key, result, stopWatch.getTime(), success); 85 | } else { 86 | LoggerUtil.infoIfNeed(logger, "* [{0}] {1} {2} {3} {4}", 87 | methodName, key, result, stopWatch.getTime(), success); 88 | } 89 | 90 | if (this.metricStatistic) { 91 | MonitorHelper.fastCompassOneKey("MIDDLE_IDGEN", methodName, 1, stopWatch.getTime(), success); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /pack-idgen/src/main/java/com/github/knightliao/middle/idgen/impl/IdgenServiceRedisImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idgen.impl; 2 | 3 | import com.github.knightliao.middle.idgen.IIdgenService; 4 | import com.github.knightliao.middle.redis.IMyRedisService; 5 | 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/13 15:05 12 | */ 13 | @Slf4j 14 | public class IdgenServiceRedisImpl implements IIdgenService { 15 | 16 | private IMyRedisService myRedisService; 17 | 18 | public IdgenServiceRedisImpl(IMyRedisService myRedisService) { 19 | this.myRedisService = myRedisService; 20 | } 21 | 22 | @Override 23 | public Long getSequenceId(long key) { 24 | return getSequenceId(String.valueOf(key)); 25 | } 26 | 27 | @Override 28 | public Long getSequenceId(String key) { 29 | 30 | String innerKey = getInnerKey(key); 31 | 32 | Long ret = myRedisService.incr(innerKey, 1, null); 33 | 34 | return ret; 35 | } 36 | 37 | private String getInnerKey(String key) { 38 | return String.format("%s:%s", "ID_GEN", key); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /pack-idgen/src/test/java/com/github/knightliao/middle/idegen/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.idegen.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.idgen"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-lang/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-lang 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.github.knightliao.middle.bom 20 | test-bom 21 | 0.1.16-SNAPSHOT 22 | test 23 | pom 24 | 25 | 26 | 27 | org.apache.commons 28 | commons-lang3 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/ICancelable.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/5 20:40 7 | */ 8 | public interface ICancelable { 9 | 10 | void cancel(); 11 | } 12 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/IIdAble.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/9/11 16:36 7 | */ 8 | public interface IIdAble { 9 | 10 | String getId(); 11 | } 12 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/IInitAble.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/9/11 16:36 7 | */ 8 | public interface IInitAble { 9 | 10 | void init(); 11 | } 12 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/IStoreAble.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/9/11 16:35 7 | */ 8 | public interface IStoreAble { 9 | 10 | String getStoreKey(); 11 | } 12 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/ITimeoutAble.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/9/11 16:35 7 | */ 8 | public interface ITimeoutAble { 9 | 10 | int getTimeoutMs(); 11 | } 12 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/data/IDataAware.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables.data; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/9/21 02:02 7 | */ 8 | public interface IDataAware extends IExtAware { 9 | 10 | String getId(); 11 | 12 | void setId(); 13 | 14 | String getType(); 15 | 16 | void setType(); 17 | 18 | DataType getData(); 19 | 20 | void setData(DataType data); 21 | 22 | boolean isEmpty(); 23 | 24 | default boolean isNoEmpty() { 25 | return !isEmpty(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/ables/data/IExtAware.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.ables.data; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/9/21 02:02 9 | */ 10 | public interface IExtAware { 11 | 12 | Map getExtMap(); 13 | 14 | void setExtMap(); 15 | 16 | void putExt(String key, Object value); 17 | 18 | Ext getExt(String key); 19 | 20 | Ext getExt(String key, Class t); 21 | 22 | default Ext getExt(String key, Ext defaultVal) { 23 | Ext ext = getExt(key); 24 | if (ext == null) { 25 | return defaultVal; 26 | } 27 | return ext; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/callback/IMyMethodCallback.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.callback; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/22 18:40 7 | */ 8 | public interface IMyMethodCallback { 9 | 10 | void preDo(T t); 11 | 12 | void afterDo(T t); 13 | } 14 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/constants/PackConstants.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.constants; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/20 13:52 7 | */ 8 | public class PackConstants { 9 | 10 | // default 11 | public static final int DEFAULT_ERROR_VALUE_INT = -1; 12 | public static final long DEFAULT_ERROR_VALUE_LONG = -1; 13 | public static final String DEFAULT_ERROR_VALUE_STRING = ""; 14 | 15 | // 16 | public static final String NULL_LOWERCASE = "null"; 17 | public static final String NULL = "NULL"; 18 | 19 | // 20 | public static final long DEFAULT_ERROR_UID = -1; 21 | } 22 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/enums/MyErrorExceptionEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.enums; 2 | 3 | import java.util.Objects; 4 | 5 | import lombok.Getter; 6 | 7 | /** 8 | * 000 未知异常 9 | * 001 系统异常 10 | * 002 NPE 11 | * 010~099 网络异常 12 | * 100-149 请求参数检验异常 13 | * 150-199 程序控制异常 14 | * 15 | * @author knightliao 16 | * @email knightliao@gmail.com 17 | * @date 2021/8/20 12:35 18 | */ 19 | @Getter 20 | public enum MyErrorExceptionEnum { 21 | 22 | UNKNOWN_ERROR("000", "UNKNOWN_ERROR"), 23 | SYSTEM_ERROR("001", "SYSTEM_ERROR"), 24 | NPE_ERROR("002", "NPE_ERROR"), 25 | 26 | NET_SOCKET_ERROR("010", "NET_SOCKET_ERROR"), 27 | 28 | PARAM_ERROR("100", "PARAM_ERROR"), 29 | PARAM_IDEM_DUPLICATION("101", "IDEM_DUPLICATION"), 30 | PARAM_IDEM_CONCURRENT("102", "IDEM_CONCURRENT"), 31 | 32 | BIZ_EXCEPTION("150", "BIZ_EXCEPTION"); 33 | 34 | private final String key; 35 | private final String desc; 36 | 37 | MyErrorExceptionEnum(String key, String desc) { 38 | this.key = key; 39 | this.desc = desc; 40 | } 41 | 42 | public static MyErrorExceptionEnum getByValue(String key) { 43 | for (MyErrorExceptionEnum value : MyErrorExceptionEnum.values()) { 44 | if (Objects.equals(value.getKey(), key)) { 45 | return value; 46 | } 47 | } 48 | 49 | return null; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/base/AbstractBaseException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.base; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | 5 | import lombok.Getter; 6 | 7 | /** 8 | * 异常基类 9 | * 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/9/11 12:01 13 | */ 14 | public abstract class AbstractBaseException extends RuntimeException { 15 | 16 | @Getter 17 | protected String code; 18 | 19 | @Getter 20 | protected String msg; 21 | 22 | public AbstractBaseException(MyErrorExceptionEnum myErrorExceptionEnum, Exception ex) { 23 | super(myErrorExceptionEnum.getDesc(), ex); 24 | this.msg = myErrorExceptionEnum.getDesc(); 25 | this.code = myErrorExceptionEnum.getKey(); 26 | } 27 | 28 | public AbstractBaseException(MyErrorExceptionEnum myErrorExceptionEnum, Throwable throwable) { 29 | super(myErrorExceptionEnum.getDesc(), throwable); 30 | this.msg = myErrorExceptionEnum.getDesc(); 31 | this.code = myErrorExceptionEnum.getKey(); 32 | } 33 | 34 | public AbstractBaseException(MyErrorExceptionEnum myErrorExceptionEnum, String message, Exception ex) { 35 | super(message, ex); 36 | this.msg = message; 37 | this.code = myErrorExceptionEnum.getKey(); 38 | } 39 | 40 | public AbstractBaseException(String message, Exception ex) { 41 | super(message, ex); 42 | this.msg = message; 43 | } 44 | 45 | public AbstractBaseException(String code, String message, Exception ex) { 46 | super(message, ex); 47 | this.msg = message; 48 | this.code = code; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/biz/BizException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.biz; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.base.AbstractBaseException; 5 | 6 | import lombok.Getter; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/20 12:33 12 | */ 13 | public class BizException extends AbstractBaseException { 14 | 15 | @Getter 16 | private String bizErrorCode; 17 | 18 | public BizException(MyErrorExceptionEnum myErrorExceptionEnum, String bizCode, String message, Exception ex) { 19 | super(myErrorExceptionEnum, message, ex); 20 | this.bizErrorCode = bizCode; 21 | } 22 | 23 | public BizException(MyErrorExceptionEnum myErrorExceptionEnum, String bizCode, Exception ex) { 24 | super(myErrorExceptionEnum, ex); 25 | this.bizErrorCode = bizCode; 26 | } 27 | 28 | public static BizException getParamError(String bizErrorCode, String message) { 29 | 30 | return new BizException(MyErrorExceptionEnum.PARAM_ERROR, bizErrorCode, 31 | message, null); 32 | } 33 | 34 | public static BizException getParamError(String message, Exception ex) { 35 | return new BizException(MyErrorExceptionEnum.PARAM_ERROR, "", message, ex); 36 | } 37 | 38 | public static BizException getParamError(String bizErrorCode) { 39 | return new BizException(MyErrorExceptionEnum.PARAM_ERROR, "", null); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/others/NpeException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.others; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.base.AbstractBaseException; 5 | 6 | /** 7 | * 断言 8 | * 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/4 15:29 12 | */ 13 | public class NpeException extends AbstractBaseException { 14 | 15 | public NpeException(String message, Exception ex) { 16 | super(MyErrorExceptionEnum.NPE_ERROR, message, ex); 17 | } 18 | 19 | public NpeException(Exception ex) { 20 | super(MyErrorExceptionEnum.NPE_ERROR, ex); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/others/SocketRuntimeException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.others; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.base.AbstractBaseException; 5 | 6 | /** 7 | * 断言 8 | * 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/4 15:29 12 | */ 13 | public class SocketRuntimeException extends AbstractBaseException { 14 | 15 | public SocketRuntimeException(String message, Exception ex) { 16 | super(MyErrorExceptionEnum.NET_SOCKET_ERROR, message, ex); 17 | } 18 | 19 | public SocketRuntimeException(Exception ex) { 20 | super(MyErrorExceptionEnum.NET_SOCKET_ERROR, ex); 21 | } 22 | 23 | public SocketRuntimeException(Throwable ex) { 24 | super(MyErrorExceptionEnum.NET_SOCKET_ERROR, ex); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/others/SystemException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.others; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.base.AbstractBaseException; 5 | 6 | /** 7 | * 断言 8 | * 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/4 15:29 12 | */ 13 | public class SystemException extends AbstractBaseException { 14 | 15 | public SystemException(String message, Exception ex) { 16 | super(MyErrorExceptionEnum.SYSTEM_ERROR, message, ex); 17 | } 18 | 19 | public SystemException(Exception ex) { 20 | super(MyErrorExceptionEnum.SYSTEM_ERROR, ex); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/param/IdemException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.param; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.base.AbstractBaseException; 5 | 6 | /** 7 | * 幂等异常 8 | * 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/12 14:29 12 | */ 13 | public class IdemException extends AbstractBaseException { 14 | 15 | public IdemException(MyErrorExceptionEnum myErrorExceptionEnum, String message, Exception ex) { 16 | super(myErrorExceptionEnum, message, ex); 17 | } 18 | 19 | public IdemException(MyErrorExceptionEnum myErrorExceptionEnum, Exception ex) { 20 | super(myErrorExceptionEnum, ex); 21 | } 22 | 23 | // 重复异常 24 | public static IdemException getIdemDuplicationException() { 25 | return new IdemException(MyErrorExceptionEnum.PARAM_IDEM_DUPLICATION, null); 26 | } 27 | 28 | // 并发处理 29 | public static IdemException getIdemConcurrentException() { 30 | return new IdemException(MyErrorExceptionEnum.PARAM_IDEM_CONCURRENT, null); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/exceptions/exceptions/param/ParamException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.exceptions.exceptions.param; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.enums.MyErrorExceptionEnum; 4 | import com.github.knightliao.middle.lang.exceptions.exceptions.base.AbstractBaseException; 5 | 6 | import lombok.Getter; 7 | 8 | /** 9 | * 参数异常 10 | * 11 | * @author knightliao 12 | * @email knightliao@gmail.com 13 | * @date 2021/9/11 11:51 14 | */ 15 | public class ParamException extends AbstractBaseException { 16 | 17 | @Getter 18 | private String field; 19 | 20 | public ParamException(String field, String message, Exception ex) { 21 | super(MyErrorExceptionEnum.PARAM_ERROR, message, ex); 22 | this.field = field; 23 | } 24 | 25 | public ParamException(String field, Exception ex) { 26 | super(MyErrorExceptionEnum.PARAM_ERROR, ex); 27 | this.field = field; 28 | } 29 | 30 | public ParamException(String field) { 31 | super(MyErrorExceptionEnum.PARAM_ERROR, null); 32 | this.field = field; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/future/IInvokeFuture.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.future; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import com.github.knightliao.middle.lang.exceptions.exceptions.others.SocketRuntimeException; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/4 15:34 11 | */ 12 | public interface IInvokeFuture { 13 | 14 | void addListener(IInvokeFutureListener listener); 15 | 16 | boolean isDone(); 17 | 18 | Object getResult() throws SocketRuntimeException; 19 | 20 | void setResult(Object result); 21 | 22 | Object getResult(long timeoutMs, TimeUnit unit); 23 | 24 | void setCause(Throwable cause); 25 | 26 | boolean isSuccess(); 27 | 28 | Throwable getCause(); 29 | } 30 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/future/IInvokeFutureListener.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.future; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/4 15:34 7 | */ 8 | public interface IInvokeFutureListener { 9 | 10 | void operationComplete(IInvokeFuture future) throws Exception; 11 | } 12 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/future/InvokeFutureFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.future; 2 | 3 | import com.github.knightliao.middle.lang.future.impl.InvokeFutureImpl; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/4 15:40 9 | */ 10 | public class InvokeFutureFactory { 11 | 12 | public static IInvokeFuture getInvokeFutureDefaultImpl() { 13 | 14 | return new InvokeFutureImpl(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/reflect/MyReflectionUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.reflect; 2 | 3 | import java.lang.reflect.Constructor; 4 | 5 | import com.github.knightliao.middle.lang.exceptions.exceptions.others.SystemException; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/9/12 01:57 11 | */ 12 | public class MyReflectionUtil { 13 | 14 | public static T newWithConstructor(String className) { 15 | 16 | try { 17 | 18 | Class metaClass = (Class) Class.forName(className); 19 | Constructor con = metaClass.getConstructor(); 20 | return con.newInstance(); 21 | 22 | } catch (Exception ex) { 23 | 24 | throw new SystemException("new instance failed " + className, ex); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/security/BizParamAssertUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.security; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.exceptions.biz.BizException; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/4 15:28 9 | */ 10 | public class BizParamAssertUtil { 11 | 12 | public static void assertArgumentValid(boolean condition, String message) { 13 | 14 | if (!condition) { 15 | throw BizException.getParamError("", message); 16 | } 17 | } 18 | 19 | public static void assertArgumentNotNull(Object object, String field) { 20 | if (object == null) { 21 | throw BizException.getParamError(field); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/security/MyAssertUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.security; 2 | 3 | import com.github.knightliao.middle.lang.exceptions.exceptions.param.ParamException; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/9/11 11:37 9 | */ 10 | public class MyAssertUtils { 11 | 12 | public static void assertTrue(boolean condition, String desc) { 13 | if (!condition) { 14 | throw new ParamException(desc); 15 | } 16 | } 17 | 18 | public static void assertNotNull(Object obj, String desc) { 19 | if (obj == null) { 20 | throw new ParamException(desc); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /pack-lang/src/main/java/com/github/knightliao/middle/lang/templates/NoErrorTemplate.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.templates; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/8/6 21:08 10 | */ 11 | public class NoErrorTemplate { 12 | 13 | private static final Logger log = LoggerFactory.getLogger(NoErrorTemplate.class); 14 | 15 | public NoErrorTemplate() { 16 | 17 | } 18 | 19 | public static void handle(Runnable callback) { 20 | try { 21 | callback.run(); 22 | } catch (Throwable throwable) { 23 | log.error("处理回调失败", throwable); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /pack-lang/src/test/java/com/github/knightliao/middle/lang/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lang.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.lang"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-lock/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-lock 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.commons 20 | commons-lang3 21 | 22 | 23 | 24 | redis.clients 25 | jedis 26 | 27 | 28 | 29 | com.github.knightliao.middle.bom 30 | test-bom 31 | 0.1.16-SNAPSHOT 32 | test 33 | pom 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /pack-lock/src/main/java/com/github/knightliao/middle/lock/IMyDistributeLock.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lock; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/8/12 14:51 7 | */ 8 | public interface IMyDistributeLock { 9 | 10 | // 根据 loopTryTime 循环重试 11 | boolean tryLock(String lockKey, String lockVal, long expireTimeMills, long loopTryTime); 12 | 13 | // 14 | boolean tryLock(String lockKey, String lockVal, long expireTimeMills, int retryTime, long stepTime); 15 | 16 | // 一次尝试,快速失败,不支持重入 17 | boolean tryLock(String lockKey, String lockVal, long expireTimeMills); 18 | 19 | // 释放分布式锁,释放失败可能是业务执行时间长于lockkey过期时间,应当结合业务场景调整过期时间 20 | boolean tryUnlock(String lockKey, String lockVal); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /pack-lock/src/test/java/com/github/knightliao/middle/lock/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.lock.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.lock"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-log/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-log 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.github.knightliao.middle 20 | pack-lang 21 | 0.1.16-SNAPSHOT 22 | 23 | 24 | 25 | com.github.knightliao.middle 26 | pack-threadcontext 27 | 0.1.16-SNAPSHOT 28 | 29 | 30 | 31 | com.github.knightliao.middle.bom 32 | test-bom 33 | 0.1.16-SNAPSHOT 34 | test 35 | pom 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /pack-log/src/main/java/com/github/knightliao/middle/log/LoggerUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.log; 2 | 3 | import java.text.MessageFormat; 4 | 5 | import org.slf4j.Logger; 6 | 7 | import com.github.knightliao.middle.lang.templates.NoErrorTemplate; 8 | import com.github.knightliao.middle.thread.MyThreadContext; 9 | 10 | /** 11 | * @author knightliao 12 | * @email knightliao@gmail.com 13 | * @date 2021/8/6 21:08 14 | */ 15 | public class LoggerUtil { 16 | 17 | public static void debug(Logger logger, String template, Object... parameters) { 18 | 19 | if (logger.isDebugEnabled()) { 20 | NoErrorTemplate.handle(() -> { 21 | logger.debug(render(template, parameters)); 22 | }); 23 | } 24 | } 25 | 26 | public static void info(Logger logger, String template, Object... parameters) { 27 | 28 | if (logger.isInfoEnabled()) { 29 | NoErrorTemplate.handle(() -> { 30 | logger.info(render(template, parameters)); 31 | }); 32 | } 33 | } 34 | 35 | public static void warn(Logger logger, String template, Object... parameters) { 36 | 37 | if (logger.isInfoEnabled()) { 38 | NoErrorTemplate.handle(() -> { 39 | logger.warn(render(template, parameters)); 40 | }); 41 | } 42 | } 43 | 44 | // 精细化打日志 45 | public static void infoIfNeed(Logger logger, String template, Object... parameters) { 46 | 47 | if (logger.isInfoEnabled() && MyThreadContext.isPrintLog()) { 48 | NoErrorTemplate.handle(() -> { 49 | logger.info(render(template, parameters)); 50 | }); 51 | } 52 | } 53 | 54 | public static void error(Throwable e, Logger logger, String template, Object... parameters) { 55 | 56 | NoErrorTemplate.handle(() -> { 57 | logger.error(render(template, parameters), e); 58 | }); 59 | } 60 | 61 | public static void error(Logger logger, String template, Object... parameters) { 62 | 63 | NoErrorTemplate.handle(() -> { 64 | logger.error(render(template, parameters)); 65 | }); 66 | } 67 | 68 | private static String render(String tpl, Object... params) { 69 | 70 | return params != null && params.length != 0 ? MessageFormat.format(tpl, numberToString(params)) : tpl; 71 | } 72 | 73 | private static Object[] numberToString(Object[] params) { 74 | 75 | for (int i = 0; i < params.length; ++i) { 76 | 77 | if (params[i] instanceof Number) { 78 | params[i] = String.valueOf(params[i]); 79 | } 80 | } 81 | 82 | return params; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /pack-log/src/test/java/com/github/knightliao/middle/log/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.log.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.log"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-metrics/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-metrics 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 2.0.1 18 | 19 | 20 | 21 | 22 | 23 | com.alibaba.middleware 24 | metrics-core-api 25 | ${metrics.version} 26 | 27 | 28 | 29 | com.alibaba.middleware 30 | metrics-core-impl 31 | ${metrics.version} 32 | 33 | 34 | 35 | com.github.knightliao.middle 36 | pack-utils 37 | 0.1.16-SNAPSHOT 38 | 39 | 40 | 41 | com.github.knightliao.middle.bom 42 | test-bom 43 | 0.1.16-SNAPSHOT 44 | test 45 | pom 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /pack-metrics/src/main/java/com/github/knightliao/middle/metrics/CostMetricMonitor.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.metrics; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/8/28 13:51 9 | */ 10 | @Slf4j 11 | public class CostMetricMonitor { 12 | 13 | public static void commitMonitor(String prefix, String node, long cost, boolean isSucc) { 14 | 15 | try { 16 | 17 | String costStr; 18 | if (cost == 0) { 19 | costStr = "0"; 20 | } else if (cost <= 1) { 21 | costStr = "0_1"; 22 | } else if (cost <= 5) { 23 | costStr = "1_5"; 24 | } else if (cost <= 10) { 25 | costStr = "5_10"; 26 | } else { 27 | costStr = "10_"; 28 | } 29 | 30 | MonitorHelper.doMeter(prefix, isSucc, node, costStr); 31 | 32 | } catch (Exception e) { 33 | log.error(e.toString(), e); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pack-metrics/src/main/java/com/github/knightliao/middle/metrics/MonitorHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.metrics; 2 | 3 | import com.alibaba.metrics.FastCompass; 4 | import com.alibaba.metrics.Meter; 5 | import com.alibaba.metrics.MetricLevel; 6 | import com.alibaba.metrics.MetricManager; 7 | import com.alibaba.metrics.MetricName; 8 | 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | /** 12 | * @author knightliao 13 | * @email knightliao@gmail.com 14 | * @date 2021/8/17 17:40 15 | */ 16 | @Slf4j 17 | public class MonitorHelper { 18 | 19 | private final static String KEY = "key"; 20 | private final static String KEY2 = "key2"; 21 | private final static String STATUS = "status"; 22 | 23 | public static void fastCompassOneKey(String metricName, String key, int status, long useTime, boolean isSuccess) { 24 | 25 | FastCompass fastCompass = MetricManager.getFastCompass("", 26 | new MetricName( 27 | metricName, MetricLevel.TRIVIAL). 28 | tagged(KEY, key). 29 | tagged(STATUS, String.valueOf(status) 30 | ) 31 | ); 32 | 33 | fastCompass.record(useTime, getSuccess(isSuccess)); 34 | } 35 | 36 | public static void fastCompassTwoKey(String metricName, String key, String key2, int status, long useTime, 37 | boolean isSuccess) { 38 | 39 | FastCompass fastCompass = MetricManager.getFastCompass("", 40 | new MetricName( 41 | metricName, MetricLevel.TRIVIAL). 42 | tagged(KEY, key). 43 | tagged(KEY2, key2). 44 | tagged(STATUS, String.valueOf(status) 45 | ) 46 | ); 47 | 48 | fastCompass.record(useTime, getSuccess(isSuccess)); 49 | } 50 | 51 | private static String getSuccess(boolean isSuccess) { 52 | 53 | return isSuccess ? "success" : "error"; 54 | } 55 | 56 | public static void doMeter(String prefix, boolean isSuccess, String node, String cost) { 57 | 58 | Meter meter = MetricManager.getMeter(prefix, new MetricName("node-cost", MetricLevel.TRIVIAL) 59 | .tagged("isSucc", isSuccess ? "true" : "false") 60 | .tagged("node", node) 61 | .tagged("cost", cost)); 62 | meter.mark(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /pack-metrics/src/main/java/com/github/knightliao/middle/metrics/jvm/JvmMetricService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.metrics.jvm; 2 | 3 | import java.lang.management.ManagementFactory; 4 | import java.lang.management.MemoryMXBean; 5 | import java.lang.management.MemoryUsage; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | /** 12 | * @author knightliao 13 | * @email knightliao@gmail.com 14 | * @date 2021/10/28 23:48 15 | */ 16 | @Slf4j 17 | public class JvmMetricService { 18 | 19 | public static final String INIT_HEAP = "0.init. heap"; 20 | public static final String MAX_HEAP = "0.max.heap"; 21 | public static final String USED_HEAP = "0.used.heap"; 22 | public static final String COMMITTED_HEAP = "0.committed.heap"; 23 | public static final String INIT_HEAP_NO = "1.init.heap.no"; 24 | public static final String MAX_HEAP_NO = "1.max.heap.no"; 25 | public static final String USED_HEAP_NO = "1.used.heap.no"; 26 | public static final String COMMITTED_HEAP_NO = "1.committed.heap.no"; 27 | public static final String TOTAL_MEM = "2.total.mem"; 28 | public static final String FREE_MEM = "2.free.mem"; 29 | public static final String MAX_MEM = "2.max.mem"; 30 | 31 | public static Map getMetrics() { 32 | 33 | Map map = new HashMap<>(); 34 | try { 35 | MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); 36 | MemoryUsage heap = memoryMXBean.getHeapMemoryUsage(); 37 | map.put(INIT_HEAP, heap.getInit() / 1024 / 1024 + "Mb"); 38 | map.put(MAX_HEAP, heap.getMax() / 1024 / 1024 + "Mb"); 39 | map.put(USED_HEAP, heap.getUsed() / 1024 / 1024 + "Mb"); 40 | map.put(COMMITTED_HEAP, heap.getCommitted() / 1024 / 1024 + "Mb"); 41 | 42 | MemoryUsage noHeap = memoryMXBean.getNonHeapMemoryUsage(); 43 | map.put(INIT_HEAP_NO, noHeap.getInit() / 1024 / 1024 + "Mb"); 44 | map.put(MAX_HEAP_NO, noHeap.getMax() / 1024 / 1024 + "Mb"); 45 | map.put(USED_HEAP_NO, noHeap.getUsed() / 1024 / 1024 + "Mb"); 46 | map.put(COMMITTED_HEAP_NO, noHeap.getCommitted() / 1024 / 1024 + "Mb"); 47 | 48 | map.put(TOTAL_MEM, Runtime.getRuntime().totalMemory() / 1024 / 1024 + "Mb"); 49 | map.put(FREE_MEM, Runtime.getRuntime().freeMemory() / 1024 / 1024 + "Mb"); 50 | map.put(MAX_MEM, Runtime.getRuntime().maxMemory() / 1024 / 1024 + "Mb"); 51 | 52 | } catch (Exception ex) { 53 | 54 | log.error(ex.toString(), ex); 55 | } 56 | 57 | return map; 58 | } 59 | } -------------------------------------------------------------------------------- /pack-metrics/src/main/java/com/github/knightliao/middle/metrics/tomcat/TomcatMetricSchedule.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.metrics.tomcat; 2 | 3 | import java.util.concurrent.ScheduledExecutorService; 4 | import java.util.concurrent.ScheduledThreadPoolExecutor; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import com.github.knightliao.middle.metrics.jvm.JvmMetricService; 8 | import com.github.knightliao.middle.utils.net.IpUtils; 9 | import com.github.knightliao.middle.utils.thread.NamedThreadFactory; 10 | 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | /** 14 | * @author knightliao 15 | * @email knightliao@gmail.com 16 | * @date 2021/10/29 00:08 17 | */ 18 | @Slf4j 19 | public class TomcatMetricSchedule { 20 | 21 | private String ip; 22 | 23 | private TomcatMetricService tomcatMetricService; 24 | 25 | public TomcatMetricSchedule(TomcatMetricService tomcatMetricService) { 26 | this.tomcatMetricService = tomcatMetricService; 27 | 28 | init(); 29 | } 30 | 31 | private void init() { 32 | 33 | this.ip = IpUtils.getLocalIp(); 34 | printLog(); 35 | 36 | ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(1, 37 | new NamedThreadFactory(TomcatMetricSchedule.class.getName(), true)); 38 | pool.scheduleAtFixedRate(this::printLog, 1, 1, TimeUnit.MINUTES); 39 | } 40 | 41 | private void printLog() { 42 | 43 | tomcatLog(); 44 | 45 | jvmLog(); 46 | } 47 | 48 | private void tomcatLog() { 49 | log.info(" " + ip + " " + tomcatMetricService.getMetrics().toString() 50 | .replace("{", "").replace("}", "")); 51 | } 52 | 53 | private void jvmLog() { 54 | log.info(" " + ip + " " + JvmMetricService.getMetrics().toString() 55 | .replace("{", "").replace("}", "")); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /pack-metrics/src/main/java/com/github/knightliao/middle/metrics/tomcat/TomcatMetricService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.metrics.tomcat; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | import javax.annotation.Resource; 8 | import javax.management.MBeanServer; 9 | import javax.management.ObjectName; 10 | 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | /** 14 | * @author knightliao 15 | * @email knightliao@gmail.com 16 | * @date 2021/10/29 00:00 17 | */ 18 | @Slf4j 19 | public class TomcatMetricService { 20 | 21 | public static final String TOMCAT_THREAD_BUSY = "0.tomcat.thread.busy"; 22 | public static final String TOMCAT_THREAD_MAX = "1.tomcat.thread.max"; 23 | public static final String TOMCAT_THREAD_CURRENT = "2.tomcat.thread.current"; 24 | public static final String TOMCAT_THREAD_CONNECTION_COUNT = "3.tomcat.thread.connectionCount"; 25 | public static final String TOMCAT_THREAD_MAX_CONNECTIONS = "4.tomcat.thread.maxConnections"; 26 | 27 | @Resource 28 | private MBeanServer mBeanServer; 29 | 30 | public Map getMetrics() { 31 | 32 | Map map = new HashMap<>(); 33 | 34 | try { 35 | 36 | Set objectNameSet = mBeanServer.queryNames(new ObjectName("Tomcat:type=ThreadPool,*"), null); 37 | 38 | if (objectNameSet != null && objectNameSet.size() == 1) { 39 | 40 | for (ObjectName objectName : objectNameSet) { 41 | 42 | long maxThreads = (Integer) mBeanServer.getAttribute(objectName, "maxThreads"); 43 | long currentThreadCount = (Integer) mBeanServer.getAttribute(objectName, "currentThreadCount"); 44 | long currentThreadsBusy = (Integer) mBeanServer.getAttribute(objectName, "currentThreadsBusy"); 45 | long connectionCount = (Integer) mBeanServer.getAttribute(objectName, "connectionCount"); 46 | long maxConnections = (Integer) mBeanServer.getAttribute(objectName, "maxConnections"); 47 | 48 | map.put(TOMCAT_THREAD_BUSY, currentThreadsBusy); 49 | map.put(TOMCAT_THREAD_MAX, maxThreads); 50 | map.put(TOMCAT_THREAD_CURRENT, currentThreadCount); 51 | map.put(TOMCAT_THREAD_MAX, connectionCount); 52 | map.put(TOMCAT_THREAD_MAX_CONNECTIONS, maxConnections); 53 | 54 | } 55 | } 56 | } catch (Exception ex) { 57 | 58 | log.error(ex.toString(), ex); 59 | } 60 | 61 | return map; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /pack-metrics/src/test/java/com/github/knightliao/middle/metrics/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.metrics.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.metrics"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-msg/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-msg 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.commons 20 | commons-lang3 21 | 22 | 23 | 24 | com.fasterxml.jackson.core 25 | jackson-core 26 | 27 | 28 | 29 | com.fasterxml.jackson.core 30 | jackson-databind 31 | 32 | 33 | 34 | com.google.guava 35 | guava 36 | 37 | 38 | 39 | com.github.knightliao.middle.bom 40 | test-bom 41 | 0.1.16-SNAPSHOT 42 | test 43 | pom 44 | 45 | 46 | 47 | commons-io 48 | commons-io 49 | 50 | 51 | 52 | org.apache.kafka 53 | kafka-clients 54 | 2.2.2 55 | 56 | 57 | 58 | com.github.knightliao.middle 59 | pack-utils 60 | 0.1.16-SNAPSHOT 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/IMessageListener.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/10/6 16:55 9 | */ 10 | public interface IMessageListener { 11 | 12 | void onMessage(String topic, List rawInstances, List extMsgs); 13 | } 14 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/dispatcher/IMessageDispatcher.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer.dispatcher; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.apache.kafka.clients.consumer.ConsumerRecord; 7 | import org.apache.kafka.common.TopicPartition; 8 | 9 | import com.github.knightliao.middle.msg.domain.domain.KafkaOffset; 10 | 11 | /** 12 | * @author knightliao 13 | * @email knightliao@gmail.com 14 | * @date 2021/10/6 17:01 15 | */ 16 | public interface IMessageDispatcher { 17 | 18 | Map dispatch(String topic, List> records); 19 | } 20 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/dispatcher/MessageDispatcherImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer.dispatcher; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.stream.Collectors; 8 | 9 | import org.apache.kafka.clients.consumer.ConsumerRecord; 10 | import org.apache.kafka.common.TopicPartition; 11 | 12 | import com.github.knightliao.middle.msg.domain.consumer.IMessageListener; 13 | import com.github.knightliao.middle.msg.domain.domain.KafkaOffset; 14 | import com.google.common.base.Preconditions; 15 | 16 | /** 17 | * @author knightliao 18 | * @email knightliao@gmail.com 19 | * @date 2021/10/28 23:06 20 | */ 21 | public class MessageDispatcherImpl implements IMessageDispatcher { 22 | 23 | private final IMessageListener messageListener; 24 | 25 | public MessageDispatcherImpl(IMessageListener messageListener) { 26 | this.messageListener = messageListener; 27 | } 28 | 29 | @Override 30 | public Map dispatch(String topic, List> records) { 31 | 32 | List list = records.stream().map(ConsumerRecord::value).collect(Collectors.toList()); 33 | 34 | List extMsgs = new ArrayList<>(); 35 | extMsgs.addAll(records); 36 | 37 | messageListener.onMessage(topic, list, extMsgs); 38 | 39 | // 40 | Map result = new HashMap<>(records.size()); 41 | for (ConsumerRecord consumerRecord : records) { 42 | 43 | // 44 | TopicPartition partition = new TopicPartition(consumerRecord.topic(), consumerRecord.partition()); 45 | long timestamp = consumerRecord.timestamp(); 46 | 47 | // 48 | long offset = consumerRecord.offset() + 1; 49 | 50 | // 51 | KafkaOffset kafkaOffset = 52 | KafkaOffset.builder().partition(partition).offset(offset).timestamp(timestamp).build(); 53 | KafkaOffset oldOffset = result.put(partition, kafkaOffset); 54 | 55 | if (oldOffset != null) { 56 | Preconditions.checkState(oldOffset.getOffset() <= offset); 57 | } 58 | } 59 | 60 | return result; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/handler/IMessageFilter.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer.handler; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/6 17:34 7 | */ 8 | public interface IMessageFilter { 9 | 10 | boolean isShouldDo(String topic, Object rawData); 11 | } 12 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/handler/IMessageHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer.handler; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/10/6 17:34 9 | */ 10 | public interface IMessageHandler { 11 | 12 | void register(String topic, IMessageProcessService processService); 13 | 14 | void process(String topic, List rawDataList, List extMsgs); 15 | } 16 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/handler/IMessageProcessService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer.handler; 2 | 3 | import com.github.knightliao.middle.msg.support.exceptions.MessageProcessorException; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/10/6 17:35 9 | */ 10 | public interface IMessageProcessService { 11 | 12 | boolean process(String topic, Object object, Object extMap) throws MessageProcessorException; 13 | 14 | Class getClassType(); 15 | } 16 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/consumer/subscribe/ISourceSubscribe.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.consumer.subscribe; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/6 16:56 7 | */ 8 | public interface ISourceSubscribe { 9 | 10 | void start(); 11 | 12 | void shutdown(); 13 | } 14 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/domain/domain/KafkaOffset.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.domain.domain; 2 | 3 | import org.apache.kafka.common.TopicPartition; 4 | 5 | import lombok.Builder; 6 | import lombok.Data; 7 | import lombok.experimental.Tolerate; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/10/6 17:02 13 | */ 14 | @Data 15 | @Builder 16 | public class KafkaOffset { 17 | 18 | private TopicPartition partition; 19 | private long offset; 20 | private long timestamp; 21 | 22 | @Tolerate 23 | public KafkaOffset() { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/impl/kafka/consumer/config/KafkaConsumeConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.impl.kafka.consumer.config; 2 | 3 | import java.util.Properties; 4 | 5 | import org.apache.kafka.clients.consumer.ConsumerConfig; 6 | import org.apache.kafka.clients.consumer.KafkaConsumer; 7 | import org.apache.kafka.clients.consumer.OffsetResetStrategy; 8 | import org.apache.kafka.common.serialization.StringDeserializer; 9 | 10 | /** 11 | * @author knightliao 12 | * @email knightliao@gmail.com 13 | * @date 2021/10/6 17:09 14 | */ 15 | public class KafkaConsumeConfig { 16 | 17 | public static KafkaConsumer build(String broker, String consumeGroup) { 18 | 19 | Properties properties = new Properties(); 20 | properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, broker); 21 | properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false"); 22 | properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 500); 23 | properties.put(ConsumerConfig.GROUP_ID_CONFIG, consumeGroup); 24 | properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, OffsetResetStrategy.LATEST.name().toLowerCase()); 25 | 26 | properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "30000"); 27 | 28 | properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); 29 | properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); 30 | 31 | return new KafkaConsumer<>(properties); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/impl/kafka/producer/KafkaProducerUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.impl.kafka.producer; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.apache.kafka.clients.consumer.ConsumerConfig; 7 | import org.apache.kafka.clients.producer.KafkaProducer; 8 | 9 | import com.github.knightliao.middle.msg.support.helper.KafkaDataSerializer; 10 | import com.github.knightliao.middle.msg.support.helper.KafkaRouteSerializer; 11 | 12 | /** 13 | * @author knightliao 14 | * @email knightliao@gmail.com 15 | * @date 2021/10/6 18:00 16 | */ 17 | public class KafkaProducerUtils { 18 | 19 | public static KafkaProducer buildKafkaProducer(String broker) { 20 | 21 | Map configs = new HashMap<>(); 22 | 23 | configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, broker); 24 | configs.put("acks", "1"); 25 | configs.put("retries", "0"); 26 | configs.put("match.size", "2097152"); 27 | configs.put("buffer.memory", "33554432"); 28 | 29 | configs.put("key.serializer", KafkaRouteSerializer.class.getName()); 30 | configs.put("value.serializer", KafkaDataSerializer.class.getName()); 31 | 32 | return new KafkaProducer(configs); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/support/constants/LoggerNames.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.support.constants; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/6 16:58 7 | */ 8 | public class LoggerNames { 9 | 10 | public static final String LOGGER_NAME = "MIDDLE_Q_LOG"; 11 | 12 | public static final String LOGGER_PROCESSOR_LOG = "MIDDLE_Q_PROCESSOR_LOG"; 13 | } 14 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/support/enums/MessageErrorEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.support.enums; 2 | 3 | import java.util.Objects; 4 | 5 | import lombok.Getter; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/10/6 17:49 11 | */ 12 | @Getter 13 | public enum MessageErrorEnum { 14 | 15 | NO_RETRY("NO_RETRY", 0), 16 | TYPE_ERROR("TYPE_ERROR", 1), 17 | RETRY("RETRY", 2); 18 | 19 | private final String key; 20 | private final int value; 21 | 22 | MessageErrorEnum(String key, int value) { 23 | this.key = key; 24 | this.value = value; 25 | } 26 | 27 | public static MessageErrorEnum getByValue(String key) { 28 | for (MessageErrorEnum value : MessageErrorEnum.values()) { 29 | if (Objects.equals(value.getKey(), key)) { 30 | return value; 31 | } 32 | } 33 | 34 | return null; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/support/enums/MessageProcessStatusEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.support.enums; 2 | 3 | import java.util.Objects; 4 | 5 | import lombok.Getter; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/10/6 17:41 11 | */ 12 | @Getter 13 | public enum MessageProcessStatusEnum { 14 | 15 | NORMAL("NORMAL", 0), 16 | RETRY("RETRY", 1), 17 | IGNORE("IGNORE", 2), 18 | ERROR("ERROR", 3), 19 | DECODE_ERROR("DECODE_ERROR", 4); 20 | 21 | private final String key; 22 | private final int value; 23 | 24 | MessageProcessStatusEnum(String key, int value) { 25 | this.key = key; 26 | this.value = value; 27 | } 28 | 29 | public static MessageProcessStatusEnum getByValue(String key) { 30 | for (MessageProcessStatusEnum value : MessageProcessStatusEnum.values()) { 31 | if (Objects.equals(value.getKey(), key)) { 32 | return value; 33 | } 34 | } 35 | 36 | return null; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/support/exceptions/MessageProcessorException.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.support.exceptions; 2 | 3 | import com.github.knightliao.middle.msg.support.enums.MessageErrorEnum; 4 | 5 | import lombok.Getter; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/10/6 17:46 11 | */ 12 | public class MessageProcessorException extends RuntimeException { 13 | 14 | @Getter 15 | private int code = 400; 16 | 17 | public MessageProcessorException() { 18 | super(); 19 | } 20 | 21 | public MessageProcessorException(String message, Throwable cause) { 22 | super(message, cause); 23 | } 24 | 25 | public MessageProcessorException(String message) { 26 | super(message); 27 | } 28 | 29 | public MessageProcessorException(String message, int code, Exception ex) { 30 | super(message, ex); 31 | this.code = code; 32 | } 33 | 34 | public MessageProcessorException(Throwable throwable) { 35 | super(throwable); 36 | } 37 | 38 | public static MessageProcessorException getMessageContentTypeError() { 39 | 40 | return new MessageProcessorException(MessageErrorEnum.TYPE_ERROR.getKey(), 41 | MessageErrorEnum.TYPE_ERROR.getValue(), null); 42 | } 43 | 44 | public static MessageProcessorException getMessageRetryError() { 45 | 46 | return new MessageProcessorException(MessageErrorEnum.RETRY.getKey(), 47 | MessageErrorEnum.RETRY.getValue(), null); 48 | } 49 | 50 | public static MessageProcessorException getMessageNoRetryError() { 51 | 52 | return new MessageProcessorException(MessageErrorEnum.NO_RETRY.getKey(), 53 | MessageErrorEnum.NO_RETRY.getValue(), null); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/support/helper/KafkaDataSerializer.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.support.helper; 2 | 3 | import java.nio.charset.StandardCharsets; 4 | import java.util.Map; 5 | 6 | import org.apache.kafka.common.serialization.Serializer; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/10/6 18:02 12 | */ 13 | public class KafkaDataSerializer implements Serializer { 14 | 15 | @Override 16 | public void configure(Map map, boolean b) { 17 | 18 | } 19 | 20 | @Override 21 | public byte[] serialize(String s, String s2) { 22 | 23 | return s2.getBytes(StandardCharsets.UTF_8); 24 | } 25 | 26 | @Override 27 | public void close() { 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /pack-msg/src/main/java/com/github/knightliao/middle/msg/support/helper/KafkaRouteSerializer.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.msg.support.helper; 2 | 3 | import java.nio.charset.StandardCharsets; 4 | import java.util.Map; 5 | 6 | import org.apache.kafka.common.serialization.Serializer; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/10/6 18:03 12 | */ 13 | public class KafkaRouteSerializer implements Serializer { 14 | @Override 15 | public void configure(Map map, boolean b) { 16 | 17 | } 18 | 19 | @Override 20 | public byte[] serialize(String s, String s2) { 21 | return s2.getBytes(StandardCharsets.UTF_8); 22 | } 23 | 24 | @Override 25 | public void close() { 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /pack-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-redis 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.commons 20 | commons-lang3 21 | 22 | 23 | 24 | org.aspectj 25 | aspectjweaver 26 | 27 | 28 | 29 | redis.clients 30 | jedis 31 | 32 | 33 | 34 | com.github.knightliao.middle 35 | pack-utils 36 | 0.1.16-SNAPSHOT 37 | 38 | 39 | 40 | com.github.knightliao.middle 41 | pack-log 42 | 0.1.16-SNAPSHOT 43 | 44 | 45 | 46 | com.github.knightliao.middle 47 | pack-metrics 48 | 0.1.16-SNAPSHOT 49 | 50 | 51 | 52 | com.github.knightliao.middle.bom 53 | test-bom 54 | 0.1.16-SNAPSHOT 55 | test 56 | pom 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /pack-redis/src/main/java/com/github/knightliao/middle/redis/IMyRedisBatchService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.redis; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/8/12 09:33 10 | */ 11 | public interface IMyRedisBatchService { 12 | 13 | Map batchGetData(List keys); 14 | 15 | Map batchGetData(List keys, Class myclass); 16 | 17 | Map batchGetDataWithLong(List keys, Class myclass); 18 | } 19 | -------------------------------------------------------------------------------- /pack-redis/src/main/java/com/github/knightliao/middle/redis/IMyRedisService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.redis; 2 | 3 | import java.util.Map; 4 | 5 | import redis.clients.jedis.JedisCluster; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/11 16:50 11 | */ 12 | public interface IMyRedisService { 13 | 14 | Boolean exist(String key); 15 | 16 | void expire(String key, int expireTimeSeconds); 17 | 18 | void set(String key, Integer expireSeconds, Object data); 19 | 20 | void setRaw(String key, Integer expireSeconds, String data); 21 | 22 | T get(String key, Class myclass); 23 | 24 | String get(String key); 25 | 26 | boolean del(String key); 27 | 28 | void hset(String key, String field, Object data, Integer expireSeconds); 29 | 30 | Map hmgetAll(String key, Class myclass); 31 | 32 | void hdel(String key, String... field); 33 | 34 | JedisCluster getJedisCluster(); 35 | 36 | Long incr(String key, int step, Integer expireSeconds); 37 | } 38 | -------------------------------------------------------------------------------- /pack-redis/src/main/java/com/github/knightliao/middle/redis/RedisServerFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.redis; 2 | 3 | import com.github.knightliao.middle.redis.impl.MyRedisServiceImpl; 4 | 5 | import redis.clients.jedis.JedisCluster; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/11 17:26 11 | */ 12 | public class RedisServerFactory { 13 | 14 | public static IMyRedisService getMyRedisService(JedisCluster jedisCluster) { 15 | return new MyRedisServiceImpl(jedisCluster); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /pack-redis/src/main/java/com/github/knightliao/middle/redis/schedule/RedisMetricSchedule.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.redis.schedule; 2 | 3 | import java.util.concurrent.ScheduledExecutorService; 4 | import java.util.concurrent.ScheduledThreadPoolExecutor; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import javax.annotation.PostConstruct; 8 | 9 | import com.github.knightliao.middle.redis.schedule.helper.RedisMetricService; 10 | import com.github.knightliao.middle.utils.thread.NamedThreadFactory; 11 | 12 | import lombok.extern.slf4j.Slf4j; 13 | import redis.clients.jedis.JedisCluster; 14 | 15 | /** 16 | * @author knightliao 17 | * @email knightliao@gmail.com 18 | * @date 2021/8/19 18:33 19 | */ 20 | @Slf4j 21 | public class RedisMetricSchedule { 22 | 23 | private RedisMetricService redisMetricService; 24 | 25 | public RedisMetricSchedule(String keyType, JedisCluster jedisCluster) { 26 | this.redisMetricService = new RedisMetricService(keyType, jedisCluster); 27 | } 28 | 29 | @PostConstruct 30 | private void init() { 31 | 32 | printLog(); 33 | 34 | ScheduledExecutorService pool = 35 | new ScheduledThreadPoolExecutor(1, 36 | new NamedThreadFactory("redis_metric_print", true)); 37 | pool.scheduleAtFixedRate(this::printLog, 1, 1, TimeUnit.MINUTES); 38 | } 39 | 40 | private void printLog() { 41 | 42 | redisMetricService.printMetric(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /pack-redis/src/main/java/com/github/knightliao/middle/redis/schedule/helper/RedisMetricService.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.redis.schedule.helper; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import com.github.knightliao.middle.log.LoggerUtil; 10 | import com.github.knightliao.middle.utils.net.IpUtils; 11 | 12 | import lombok.extern.slf4j.Slf4j; 13 | import redis.clients.jedis.JedisCluster; 14 | import redis.clients.jedis.JedisPool; 15 | 16 | /** 17 | * @author knightliao 18 | * @email knightliao@gmail.com 19 | * @date 2021/8/19 18:28 20 | */ 21 | @Slf4j 22 | public class RedisMetricService { 23 | 24 | private static final Logger logger = LoggerFactory.getLogger("RedisMetric"); 25 | 26 | private JedisCluster jedisCluster; 27 | private String keyType; 28 | private String ip; 29 | 30 | public RedisMetricService(String keyType, JedisCluster jedisCluster) { 31 | this.keyType = keyType; 32 | this.jedisCluster = jedisCluster; 33 | this.ip = IpUtils.getLocalIp(); 34 | } 35 | 36 | public void printMetric() { 37 | 38 | Map map = new HashMap<>(); 39 | 40 | try { 41 | 42 | if (jedisCluster == null) { 43 | return; 44 | } 45 | 46 | Map jedisPoolMap = jedisCluster.getClusterNodes(); 47 | for (String key : jedisPoolMap.keySet()) { 48 | 49 | JedisPool jedisPool = jedisPoolMap.get(key); 50 | 51 | LoggerUtil.info(logger, 52 | "{0} {1} key={2} active={3} waiters={4} idle={5} maxWaitmill={6} meanWaitmil={7}", 53 | ip, keyType, key, 54 | jedisPool.getNumActive(), jedisPool.getNumWaiters(), jedisPool.getNumIdle(), 55 | jedisPool.getMaxBorrowWaitTimeMillis(), jedisPool.getMeanBorrowWaitTimeMillis()); 56 | } 57 | } catch (Exception e) { 58 | 59 | log.error(e.toString(), e); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /pack-redis/src/test/java/com/github/knightliao/middle/redis/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.redis.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.redis"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-ruleengine/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-ruleengine 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.alibaba 20 | QLExpress 21 | 3.2.0 22 | 23 | 24 | 25 | com.github.knightliao.middle 26 | pack-lang 27 | 0.1.16-SNAPSHOT 28 | 29 | 30 | 31 | commons-collections 32 | commons-collections 33 | 3.2.2 34 | 35 | 36 | 37 | com.github.knightliao.middle.bom 38 | test-bom 39 | 0.1.16-SNAPSHOT 40 | test 41 | pom 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /pack-ruleengine/src/main/java/com/github/knightliao/middle/ruleengine/IExpressExecutor.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.ruleengine; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/9/11 11:19 10 | */ 11 | public interface IExpressExecutor { 12 | 13 | Object execute(String expressStr, Map params); 14 | 15 | Object execute(String expressStr, Map params, List errorList); 16 | } 17 | -------------------------------------------------------------------------------- /pack-ruleengine/src/main/java/com/github/knightliao/middle/ruleengine/factory/ExpressExecutorFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.ruleengine.factory; 2 | 3 | import com.github.knightliao.middle.ruleengine.IExpressExecutor; 4 | import com.github.knightliao.middle.ruleengine.impl.ql.ExpressExecutorQlImpl; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/9/11 13:29 10 | */ 11 | public class ExpressExecutorFactory { 12 | 13 | public static IExpressExecutor getExpressExecutorQl() { 14 | return new ExpressExecutorQlImpl(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /pack-ruleengine/src/main/java/com/github/knightliao/middle/ruleengine/impl/ql/support/helper/MyQlExpressContext.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.ruleengine.impl.ql.support.helper; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.ql.util.express.IExpressContext; 8 | 9 | public class MyQlExpressContext extends HashMap implements 10 | IExpressContext { 11 | 12 | private static final String PRE_TAG = "$"; 13 | 14 | public MyQlExpressContext(Map map) { 15 | // 需要经过 put处理才能生效 16 | for (String key : map.keySet()) { 17 | this.put(key, map.get(key)); 18 | } 19 | } 20 | 21 | /** 22 | * 抽象方法:根据名称从属性列表中提取属性值 23 | */ 24 | @Override 25 | public Object get(Object name) { 26 | 27 | return super.get(name); 28 | } 29 | 30 | @Override 31 | public Object put(String name, Object object) { 32 | return super.put(PRE_TAG + name, object); 33 | } 34 | 35 | public boolean containAllKey(List requireKeys) { 36 | 37 | return keySet().containsAll(requireKeys); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /pack-ruleengine/src/main/java/com/github/knightliao/middle/ruleengine/support/constants/RuleEngineConstants.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.ruleengine.support.constants; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/9/11 11:26 7 | */ 8 | public class RuleEngineConstants { 9 | 10 | public static final String OPERATOR_CONTAIN = "contain"; 11 | public static final String OPERATOR_NOT_CONTAIN = "notContain"; 12 | public static final String OPERATOR_NOT_IN = "notIn"; 13 | public static final String OPERATOR_INTERSET = "intersect"; 14 | public static final String OPERATOR_DISJOINT = "disjoint"; 15 | public static final String OPERATOR_BETWEEN = "between"; 16 | public static final String OPERATOR_IS = "is"; 17 | 18 | public static final String UDF_CASE_WHEN = "caseWhen"; 19 | } 20 | -------------------------------------------------------------------------------- /pack-ruleengine/src/test/java/com/github/knightliao/middle/ruleengine/test/ExpressExecutorTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.ruleengine.test; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import com.github.knightliao.middle.ruleengine.IExpressExecutor; 10 | import com.github.knightliao.middle.ruleengine.factory.ExpressExecutorFactory; 11 | import com.github.knightliao.middle.ruleengine.test.dto.UserInfoTest; 12 | 13 | /** 14 | * @author knightliao 15 | * @email knightliao@gmail.com 16 | * @date 2021/9/11 13:27 17 | */ 18 | public class ExpressExecutorTestCase { 19 | 20 | private IExpressExecutor expressExecutor = ExpressExecutorFactory.getExpressExecutorQl(); 21 | 22 | @Test 23 | public void test() { 24 | 25 | Map map = new HashMap<>(); 26 | map.put("a", 1); 27 | map.put("b", 2); 28 | map.put("c", 3); 29 | 30 | map.put("user1", new UserInfoTest(1, "1", 3)); 31 | map.put("user2", null); 32 | 33 | Object value2 = expressExecutor.execute( 34 | "if($user2!=null){ $a + $b + $user2.getId() } else { $a + $b + $user1.getId() }", map); 35 | Assert.assertEquals(value2, 4L); 36 | 37 | Object value3 = expressExecutor.execute("($a > $b) && (true) ", map); 38 | Assert.assertFalse((Boolean) value3); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /pack-ruleengine/src/test/java/com/github/knightliao/middle/ruleengine/test/dto/UserInfoTest.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.ruleengine.test.dto; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/9/11 02:33 9 | */ 10 | @Data 11 | public class UserInfoTest { 12 | 13 | private long id; 14 | private long tag; 15 | private String name; 16 | 17 | public UserInfoTest(long aId, String aName, long aUserTag) { 18 | this.id = aId; 19 | this.tag = aUserTag; 20 | this.name = aName; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /pack-springboot-ext/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-springboot-ext 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 2.3.12.RELEASE 22 | provided 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot 28 | 1.5.17.RELEASE 29 | provided 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /pack-springboot-ext/src/main/java/com/github/knightliao/middle/springboot/ext/bean/TomcatConfigBean.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.springboot.ext.bean; 2 | 3 | import org.apache.coyote.http11.Http11NioProtocol; 4 | import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; 5 | import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; 6 | import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; 7 | 8 | import lombok.Builder; 9 | import lombok.Setter; 10 | import lombok.extern.slf4j.Slf4j; 11 | 12 | /** 13 | * @author knightliao 14 | * @email knightliao@gmail.com 15 | * @date 2021/10/29 00:20 16 | */ 17 | @Slf4j 18 | @Builder 19 | public class TomcatConfigBean { 20 | 21 | @Setter 22 | private Integer asyncTimeout; 23 | 24 | @Setter 25 | private Integer connectCount; 26 | 27 | @Setter 28 | private Integer keepAliveCount; 29 | 30 | private EmbeddedServletContainerFactory servletContainer() { 31 | 32 | TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); 33 | factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { 34 | @Override 35 | public void customize(org.apache.catalina.connector.Connector connector) { 36 | 37 | connector.setAsyncTimeout(asyncTimeout); 38 | 39 | Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); 40 | 41 | protocol.setMaxConnections(connectCount); 42 | protocol.setMaxKeepAliveRequests(keepAliveCount); 43 | 44 | log.info("keepAliveCount={} connectCount={} asyncTimeout={}", keepAliveCount, connectCount, 45 | asyncTimeout); 46 | } 47 | 48 | }); 49 | 50 | return factory; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pack-threadcontext/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-threadcontext 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | com.github.knightliao.middle.bom 20 | test-bom 21 | 0.1.16-SNAPSHOT 22 | test 23 | pom 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /pack-threadcontext/src/main/java/com/github/knightliao/middle/thread/MyThreadContext.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.thread; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/11 16:19 12 | */ 13 | @Slf4j 14 | public class MyThreadContext { 15 | 16 | private static final ThreadLocal> CTX = new ThreadLocal<>(); 17 | 18 | private static final String PRINT_LOG_KEY = "PRINT_LOG_KEY"; 19 | 20 | private static final String ERROR_LOG_INFO = "调用ThreadContext时,必须要先进行ThreadContext的init,线程退出前再进行clean, " 21 | + "避免被其他线程使用到本线程的数据,发生线程安全问题"; 22 | 23 | public MyThreadContext() { 24 | } 25 | 26 | public static void ensureInited() { 27 | if (CTX.get() == null) { 28 | init(); 29 | } 30 | } 31 | 32 | public static void clean() { 33 | CTX.remove(); 34 | } 35 | 36 | public static final boolean init() { 37 | if (CTX.get() != null) { 38 | return false; 39 | } else { 40 | 41 | Map currentThreadCtx = new HashMap<>(16); 42 | CTX.set(currentThreadCtx); 43 | return true; 44 | } 45 | } 46 | 47 | public static final void put(String key, V value) { 48 | try { 49 | ((Map) CTX.get()).put(key, value); 50 | } catch (NullPointerException var3) { 51 | log.error(ERROR_LOG_INFO); 52 | throw var3; 53 | } 54 | } 55 | 56 | public static final V get(String key) { 57 | Map map = CTX.get(); 58 | return map == null ? null : (V) map.get(key); 59 | } 60 | 61 | public static void putPrintLogKey(boolean isPrintLog) { 62 | put(PRINT_LOG_KEY, isPrintLog); 63 | } 64 | 65 | public static void removePrintLogKey() { 66 | try { 67 | ((Map) CTX.get()).remove(PRINT_LOG_KEY); 68 | } catch (NullPointerException var3) { 69 | log.error(ERROR_LOG_INFO); 70 | throw var3; 71 | } 72 | } 73 | 74 | public static boolean isPrintLog() { 75 | Boolean ret = get(PRINT_LOG_KEY); 76 | 77 | if (ret != null && ret) { 78 | return true; 79 | } 80 | 81 | return false; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /pack-threadcontext/src/test/java/com/github/knightliao/middle/thread/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.thread.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.test.support.utils.TestUtils; 6 | 7 | /** 8 | * @author knightliao 9 | * @date 2016/12/10 14:47 10 | */ 11 | public class TestCommonTestCase { 12 | 13 | @Test 14 | public void test() { 15 | 16 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.thread"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pack-trace/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-trace 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | javax.servlet 20 | javax.servlet-api 21 | 3.1.0 22 | 23 | 24 | 25 | org.apache.commons 26 | commons-lang3 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /pack-trace/src/main/java/com/github/knightliao/middle/trace/MyTraceUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.trace; 2 | 3 | import java.util.UUID; 4 | 5 | import javax.servlet.ServletRequest; 6 | import javax.servlet.ServletResponse; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.apache.commons.lang3.StringUtils; 11 | import org.slf4j.MDC; 12 | 13 | import lombok.extern.slf4j.Slf4j; 14 | 15 | /** 16 | * @author knightliao 17 | * @email knightliao@gmail.com 18 | * @date 2021/8/28 17:58 19 | */ 20 | @Slf4j 21 | public class MyTraceUtils { 22 | 23 | public static final String TRACE_ID = "traceId"; 24 | public static final String REQ_ID = "reqId"; 25 | 26 | // 从web request继承 27 | public static void startTraceFromHttpRequest(ServletRequest servletRequest) { 28 | 29 | try { 30 | 31 | HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; 32 | String traceId = httpServletRequest.getHeader(TRACE_ID); 33 | 34 | if (StringUtils.isEmpty(traceId)) { 35 | traceId = newTrace(); 36 | } else { 37 | // put to mdc 38 | MDC.put(TRACE_ID, traceId); 39 | } 40 | 41 | // 42 | } catch (Exception ex) { 43 | log.error(ex.toString(), ex); 44 | } 45 | } 46 | 47 | // 新开一个trace 48 | public static String newTrace() { 49 | 50 | try { 51 | 52 | String traceId = UUID.randomUUID().toString().replace("-", ""); 53 | 54 | // put to mdc 55 | MDC.put(TRACE_ID, traceId); 56 | 57 | // 58 | 59 | return traceId; 60 | 61 | } catch (Exception ex) { 62 | log.error(ex.toString(), ex); 63 | 64 | return ""; 65 | } 66 | } 67 | 68 | // 新开一个 reqId 69 | public static String newReqId() { 70 | 71 | try { 72 | 73 | String reqId = UUID.randomUUID().toString().replace("-", ""); 74 | 75 | // put to mdc 76 | MDC.put(REQ_ID, reqId); 77 | 78 | // 79 | 80 | return reqId; 81 | 82 | } catch (Exception ex) { 83 | log.error(ex.toString(), ex); 84 | 85 | return ""; 86 | } 87 | } 88 | 89 | public static void addTraceToResponse(ServletResponse servletResponse) { 90 | 91 | HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; 92 | httpServletResponse.addHeader(TRACE_ID, getTraceId()); 93 | } 94 | 95 | public static String getTraceId() { 96 | 97 | try { 98 | 99 | return MDC.get(TRACE_ID); 100 | 101 | } catch (Exception ex) { 102 | log.error(ex.toString(), ex); 103 | 104 | return ""; 105 | } 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /pack-utils/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-utils 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.commons 20 | commons-lang3 21 | 22 | 23 | 24 | com.fasterxml.jackson.core 25 | jackson-core 26 | 27 | 28 | 29 | com.fasterxml.jackson.core 30 | jackson-databind 31 | 32 | 33 | 34 | com.google.guava 35 | guava 36 | 37 | 38 | 39 | com.github.knightliao.middle.bom 40 | test-bom 41 | 0.1.16-SNAPSHOT 42 | test 43 | pom 44 | 45 | 46 | 47 | commons-io 48 | commons-io 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/devlopment/IdeaDetectUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.devlopment; 2 | 3 | import java.net.URL; 4 | import java.net.URLClassLoader; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/9/20 11:27 10 | */ 11 | public class IdeaDetectUtil { 12 | 13 | private static final boolean IS_IDEA_ENV; 14 | 15 | public static boolean isInIDea() { 16 | return IS_IDEA_ENV; 17 | } 18 | 19 | static { 20 | IS_IDEA_ENV = detectIDeaEnv(); 21 | } 22 | 23 | private static boolean detectIDeaEnv() { 24 | 25 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 26 | if (classLoader instanceof URLClassLoader) { 27 | 28 | final URL[] ursl = ((URLClassLoader) classLoader).getURLs(); 29 | 30 | for (int i = 0; i < ursl.length; ++i) { 31 | URL url = ursl[i]; 32 | if (url.toString().endsWith("idea_rt.jar")) { 33 | return true; 34 | } 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/io/MyFileUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.io; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.OutputStream; 7 | import java.io.Reader; 8 | import java.io.Writer; 9 | 10 | import org.apache.commons.io.FileUtils; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | /** 15 | * 文件操作的方法集 16 | * 17 | * @author liaoqiqi 18 | * @version 2014-8-20 19 | */ 20 | public final class MyFileUtils extends org.apache.commons.io.FileUtils { 21 | 22 | private static final Logger logger = LoggerFactory.getLogger(MyFileUtils.class); 23 | 24 | private MyFileUtils() { 25 | 26 | } 27 | 28 | /** 29 | * 关闭文件流 30 | * 31 | * @param w 上午10:27:27 created by Darwin(Tianxin) 32 | */ 33 | public final static void closeWriter(Writer w) { 34 | if (w != null) { 35 | try { 36 | w.close(); 37 | } catch (Exception e) { 38 | } 39 | } 40 | } 41 | 42 | /** 43 | * 关闭文件流 44 | * 45 | * @param r 上午10:27:27 created by Darwin(Tianxin) 46 | */ 47 | public final static void closeReader(Reader r) { 48 | if (r != null) { 49 | try { 50 | r.close(); 51 | } catch (Exception e) { 52 | } 53 | } 54 | } 55 | 56 | /** 57 | * 关闭文件流 58 | * 59 | * @param os 上午10:27:27 created by Darwin(Tianxin) 60 | */ 61 | public final static void closeOutputStream(OutputStream os) { 62 | if (os != null) { 63 | try { 64 | os.close(); 65 | } catch (Exception e) { 66 | } 67 | } 68 | } 69 | 70 | /** 71 | * 关闭文件流 72 | * 73 | * @param is 上午10:27:27 created by Darwin(Tianxin) 74 | */ 75 | public final static void closeInputStream(InputStream is) { 76 | if (is != null) { 77 | try { 78 | is.close(); 79 | } catch (Exception e) { 80 | } 81 | } 82 | } 83 | 84 | /** 85 | * 使用jar包:commons-codec-1.4.jar的md5比较方法
86 | * http://blog.csdn.net/very365_1208/article/details/8824033 87 | * 88 | * @param oldName 89 | * @param newName 90 | * @return 91 | */ 92 | public static boolean isFileUpdate(String oldName, String newName) { 93 | 94 | return isFileEqual(new File(oldName), new File(newName)); 95 | } 96 | 97 | /** 98 | * http://blog.csdn.net/very365_1208/article/details/8824033 99 | * http://www.avajava.com/tutorials/lessons/whats-a-quick-way 100 | * -to-tell-if-the-contents-of-two-files-are-identical-or-not.html 101 | * 102 | * @param oldName 103 | * @param newName 104 | * @return 105 | */ 106 | public static boolean isFileEqual(File oldFile, File newFile) { 107 | 108 | try { 109 | 110 | return FileUtils.contentEquals(oldFile, newFile); 111 | 112 | } catch (IOException e) { 113 | 114 | logger.warn(e.toString()); 115 | return false; 116 | } 117 | 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/io/PropertiesReader.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.io; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Map; 6 | import java.util.MissingResourceException; 7 | import java.util.Properties; 8 | import java.util.WeakHashMap; 9 | 10 | /** 11 | * 读取properties文件的辅助类 12 | * 13 | * @author liaoqiqi 14 | * @version 2014-8-20 15 | */ 16 | public final class PropertiesReader { 17 | 18 | private static Map filePropMapping = new WeakHashMap(); 19 | 20 | private PropertiesReader() { 21 | 22 | } 23 | 24 | /** 25 | * 取得指定properties文件的指定key的value 26 | * 27 | * @param fileName 28 | * @param key 29 | * @return 30 | * @throws MissingResourceException 31 | */ 32 | public static String getValue(String fileName, String key) 33 | throws MissingResourceException { 34 | final Properties properties = fillProperties(fileName); 35 | String value = properties.getProperty(key); 36 | return value.trim(); 37 | } 38 | 39 | /** 40 | * 将文件中配置信息填充到properties对象中(用earth的ClassLoader) 41 | * 42 | * @param fileName 43 | * @return Properties对象 44 | * @author liuzeyin 45 | */ 46 | public static Properties fillProperties(String fileName) { 47 | return fillProperties(fileName, PropertiesReader.class.getClassLoader()); 48 | } 49 | 50 | /** 51 | * 将文件中配置信息填充到properties对象中(用指定的ClassLoader) 52 | * 53 | * @param fileName 54 | * @param cl 55 | * @return Properties对象 56 | * @author liuzeyin 57 | */ 58 | public static Properties fillProperties(String fileName, ClassLoader cl) { 59 | 60 | if (!fileName.endsWith(".properties")) { 61 | fileName = fileName + ".properties"; 62 | } 63 | 64 | Properties properties = new Properties(); 65 | 66 | if (filePropMapping.containsKey(fileName)) { 67 | properties = filePropMapping.get(fileName); 68 | } else { 69 | InputStream is = cl.getResourceAsStream(fileName); 70 | try { 71 | properties.load(is); 72 | filePropMapping.put(fileName, properties); 73 | } catch (Exception e) { 74 | throw new RuntimeException("load properties file error " 75 | + fileName, e); 76 | } finally { 77 | try { 78 | is.close(); 79 | } catch (IOException e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | } 84 | 85 | return properties; 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/ClassLoaderUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | import java.net.URLDecoder; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | /** 9 | * 10 | */ 11 | public final class ClassLoaderUtil { 12 | 13 | protected static final Logger LOGGER = LoggerFactory 14 | .getLogger(ClassLoaderUtil.class); 15 | 16 | // class path 17 | private static String classPath = ""; 18 | 19 | // loader 20 | private static ClassLoader loader = Thread.currentThread().getContextClassLoader(); 21 | 22 | /* 23 | 24 | */ 25 | private ClassLoaderUtil() { 26 | 27 | } 28 | 29 | // 30 | // get class path 31 | // 32 | static { 33 | 34 | if (loader == null) { 35 | LOGGER.info("using system class loader!"); 36 | loader = ClassLoader.getSystemClassLoader(); 37 | } 38 | 39 | try { 40 | 41 | java.net.URL url = loader.getResource(""); 42 | // get class path 43 | classPath = url.getPath(); 44 | classPath = URLDecoder.decode(classPath, "utf-8"); 45 | 46 | // 如果是jar包内的,则返回当前路径 47 | if (classPath.contains(".jar!")) { 48 | LOGGER.warn("using config file inline jar!"); 49 | classPath = System.getProperty("user.dir"); 50 | } 51 | 52 | } catch (Exception e) { 53 | LOGGER.warn("cannot get classpath using getResource(), now using user.dir"); 54 | classPath = System.getProperty("user.dir"); 55 | } 56 | 57 | LOGGER.info("classpath: {}", classPath); 58 | } 59 | 60 | public static String getClassPath() { 61 | return classPath; 62 | } 63 | 64 | public static ClassLoader getLoader() { 65 | return loader; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/ExceptionUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/5 18:04 7 | */ 8 | public class ExceptionUtils { 9 | 10 | public static boolean exceptionMatch(Throwable throwable, Class clazz) { 11 | 12 | if (clazz.isInstance(throwable)) { 13 | return true; 14 | } 15 | 16 | while ((throwable = throwable.getCause()) != null) { 17 | 18 | if (clazz.isInstance(throwable)) { 19 | return true; 20 | } 21 | } 22 | 23 | return false; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/MapsUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | import java.util.Map; 4 | 5 | import com.google.common.collect.Maps; 6 | 7 | import lombok.val; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/9/20 18:06 13 | */ 14 | public class MapsUtils { 15 | 16 | public static void putIn(Map map, Object value, String... paths) { 17 | 18 | Map last = map; 19 | for (int i = 0; i < paths.length - 1; ++i) { 20 | String key = paths[i]; 21 | Object v = last.get(key); 22 | 23 | if (v == null) { 24 | val newMap = Maps.newHashMap(); 25 | last.put(key, newMap); 26 | last = newMap; 27 | } else { 28 | if (v instanceof Map) { 29 | last = (Map) v; 30 | } else { 31 | 32 | } 33 | } 34 | } 35 | 36 | last.put(paths[paths.length - 1], value); 37 | } 38 | 39 | public static V takeIn(Map map, String... keys) { 40 | Object last = map; 41 | 42 | for (String key : keys) { 43 | if (last != null) { 44 | last = ((Map) last).get(key); 45 | } else { 46 | return null; 47 | } 48 | } 49 | 50 | return (V) last; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/MyStringUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | import org.apache.commons.lang3.StringUtils; 8 | 9 | import com.google.common.base.Splitter; 10 | 11 | /** 12 | * @author knightliao 13 | * @email knightliao@gmail.com 14 | * @date 2021/8/19 18:58 15 | */ 16 | public class MyStringUtils { 17 | 18 | public static List split(String value, String splitter) { 19 | 20 | return StringUtils.isBlank(value) ? 21 | new ArrayList<>() : 22 | Splitter.on(splitter).trimResults().omitEmptyStrings().splitToList(value); 23 | } 24 | 25 | public static List splitToLong(String value, String splitter) { 26 | 27 | if (StringUtils.isEmpty(value)) { 28 | return new ArrayList<>(); 29 | } 30 | List list = split(value, splitter); 31 | return list.stream().map(Long::valueOf).collect(Collectors.toList()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/RandomString.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * 随机的数据生成 器 7 | * 8 | * @author knightliao 9 | * 10 | */ 11 | public class RandomString { 12 | 13 | private static final char[] symbols; 14 | 15 | static { 16 | StringBuilder tmp = new StringBuilder(); 17 | for (char ch = '0'; ch <= '9'; ++ch) { 18 | tmp.append(ch); 19 | } 20 | for (char ch = 'a'; ch <= 'z'; ++ch) { 21 | tmp.append(ch); 22 | } 23 | symbols = tmp.toString().toCharArray(); 24 | } 25 | 26 | private final Random random = new Random(); 27 | 28 | private final char[] buf; 29 | 30 | public RandomString(int length) { 31 | if (length < 1) { 32 | throw new IllegalArgumentException("length < 1: " + length); 33 | } 34 | buf = new char[length]; 35 | } 36 | 37 | public String nextString() { 38 | for (int idx = 0; idx < buf.length; ++idx) { 39 | buf[idx] = symbols[random.nextInt(symbols.length)]; 40 | } 41 | return new String(buf); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/RandomUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | import java.util.Random; 7 | 8 | /** 9 | * @author liaoqiqi 10 | * @version 2014-1-15 11 | */ 12 | public final class RandomUtil { 13 | 14 | private RandomUtil() { 15 | 16 | } 17 | 18 | /** 19 | * 获取一堆数据 20 | * 21 | * @param limit 22 | * @return 23 | */ 24 | public static List randomSerial(int limit) { 25 | List list = new ArrayList(limit); 26 | 27 | for (int ix = 0; ix < limit; ++ix) { 28 | list.add(ix); 29 | } 30 | 31 | Collections.shuffle(list, new Random()); 32 | return list; 33 | } 34 | 35 | public static void main(String[] args) { 36 | System.out.println((int) 1 / 20 + "," + random(0, 0) + "," 37 | + random(3, 2) + "," + random(-2, 4)); 38 | List a = RandomUtil.randomSerial(10); 39 | for (int i : a) { 40 | System.out.println(i); 41 | } 42 | // for(int i = 0 ;i < 500 ; i++){ 43 | // System.out.println(random(0, 20)); 44 | // } 45 | } 46 | 47 | /** 48 | * [min,max] 49 | * 50 | * @param min 51 | * @param max 52 | * @return 53 | */ 54 | public static int random(int min, int max) { 55 | if (min < 0 || max < 0) { 56 | throw new RuntimeException( 57 | "illegal argment, min and max must great then zero."); 58 | } 59 | if (min > max) { 60 | int t = max; 61 | max = min; 62 | min = t; 63 | } else if (min == max) { 64 | return min; 65 | } 66 | 67 | Random random = new Random(); 68 | return random.nextInt(max) % (max - min + 1) + min; 69 | 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/lang/TokenUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.lang; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author liaoqiqi 7 | * @version 2014-8-20 8 | */ 9 | public final class TokenUtil { 10 | 11 | private TokenUtil() { 12 | 13 | } 14 | 15 | public static final String getTokenId(int length) { 16 | if (0 == length) { 17 | return ""; 18 | } 19 | 20 | Date tt = new Date(); 21 | String mTimeStr = "" + tt.getTime(); 22 | int realLength = mTimeStr.length(); 23 | if (realLength >= length) { 24 | return mTimeStr.substring(0, length); 25 | } 26 | 27 | String tempString = ""; 28 | String randomString = mTimeStr; 29 | 30 | while (realLength < length) { 31 | tempString = "" + Math.random(); 32 | randomString += tempString.substring(2); 33 | realLength = randomString.length(); 34 | } 35 | 36 | return randomString.substring(0, length); 37 | } 38 | 39 | /** 40 | * 获取UUID 41 | * 42 | * @return 43 | */ 44 | public static final String generateToken() { 45 | return java.util.UUID.randomUUID().toString(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/log/LogControllerTemplate.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.log; 2 | 3 | import com.google.common.util.concurrent.RateLimiter; 4 | 5 | import lombok.extern.slf4j.Slf4j; 6 | 7 | /** 8 | * @author knightliao 9 | * @email knightliao@gmail.com 10 | * @date 2021/8/19 21:10 11 | */ 12 | @Slf4j 13 | public class LogControllerTemplate { 14 | 15 | private static RateLimiter rateLimiter = RateLimiter.create(100); 16 | 17 | private static boolean printLog(int times) { 18 | 19 | rateLimiter.setRate(times); 20 | return rateLimiter.tryAcquire(); 21 | } 22 | 23 | public static void doFewLog(int times, Runnable callback) { 24 | try { 25 | if (printLog(times)) { 26 | callback.run(); 27 | } 28 | } catch (Throwable throwable) { 29 | log.error("处理回调失败", throwable); 30 | } 31 | } 32 | 33 | public static void doFewLog(Runnable callback) { 34 | doFewLog(200, callback); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/net/NetUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.net; 2 | 3 | import java.io.IOException; 4 | import java.net.HttpURLConnection; 5 | import java.net.MalformedURLException; 6 | import java.net.URL; 7 | 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | /** 12 | * 网络工具 13 | * 14 | * @author liaoqiqi 15 | * @version 2014-6-17 16 | */ 17 | public final class NetUtils { 18 | 19 | protected static final Logger LOGGER = LoggerFactory 20 | .getLogger(NetUtils.class); 21 | 22 | private NetUtils() { 23 | 24 | } 25 | 26 | /** 27 | * PING指定URL是否可用 28 | * 29 | * @param address 30 | * @return 31 | */ 32 | public static boolean pingUrl(final String address) { 33 | 34 | try { 35 | 36 | final URL url = new URL("http://" + address); 37 | 38 | final HttpURLConnection urlConn = (HttpURLConnection) url 39 | .openConnection(); 40 | 41 | urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds 42 | 43 | final long startTime = System.currentTimeMillis(); 44 | 45 | urlConn.connect(); 46 | 47 | final long endTime = System.currentTimeMillis(); 48 | 49 | if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 50 | LOGGER.info("Time (ms) : " + (endTime - startTime)); 51 | LOGGER.info("Ping to " + address + " was success"); 52 | return true; 53 | } 54 | 55 | } catch (final MalformedURLException e1) { 56 | e1.printStackTrace(); 57 | 58 | } catch (final IOException e) { 59 | e.printStackTrace(); 60 | } 61 | return false; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/thread/NamedThreadFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.thread; 2 | 3 | import java.util.concurrent.ThreadFactory; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | /** 9 | * @author knightliao 10 | * @email knightliao@gmail.com 11 | * @date 2021/8/19 18:38 12 | */ 13 | @Slf4j 14 | public class NamedThreadFactory implements ThreadFactory { 15 | 16 | private static final AtomicInteger POOL_SEQUENCE = new AtomicInteger(1); 17 | private final AtomicInteger threadNum = new AtomicInteger(1); 18 | private final String prefix; 19 | private final boolean daemon; 20 | private final ThreadGroup group; 21 | private final Thread.UncaughtExceptionHandler exceptionHandler; 22 | 23 | public NamedThreadFactory(String poolPrefix) { 24 | this(poolPrefix, false); 25 | } 26 | 27 | public NamedThreadFactory(String poolPrefix, boolean daemon) { 28 | this(poolPrefix, daemon, new LogUncaughtExceptionHandler()); 29 | } 30 | 31 | public NamedThreadFactory(String poolPrefix, boolean daemon, Thread.UncaughtExceptionHandler exceptionHandler) { 32 | 33 | this.prefix = poolPrefix + "-" + POOL_SEQUENCE.getAndIncrement(); 34 | this.daemon = daemon; 35 | 36 | SecurityManager securityManager = System.getSecurityManager(); 37 | this.group = securityManager == null ? Thread.currentThread().getThreadGroup() : 38 | securityManager.getThreadGroup(); 39 | this.exceptionHandler = exceptionHandler; 40 | } 41 | 42 | @Override 43 | public Thread newThread(Runnable r) { 44 | 45 | String name = prefix + "-" + this.threadNum.getAndIncrement(); 46 | Thread thread = new Thread(group, r, name); 47 | thread.setDaemon(daemon); 48 | thread.setUncaughtExceptionHandler(exceptionHandler); 49 | return thread; 50 | } 51 | 52 | private static class LogUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { 53 | 54 | @Override 55 | public void uncaughtException(Thread t, Throwable e) { 56 | log.error("thread {} caught exception", t, e); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/thread/ThreadUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.thread; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | /** 6 | * @author knightliao 7 | * @email knightliao@gmail.com 8 | * @date 2021/10/6 17:17 9 | */ 10 | @Slf4j 11 | public class ThreadUtil { 12 | 13 | public static void sleep(int mills) { 14 | 15 | try { 16 | 17 | Thread.sleep(mills); 18 | } catch (InterruptedException ex) { 19 | 20 | log.warn("sleep interrupted", ex); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/trans/JsonUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.trans; 2 | 3 | import java.util.Map; 4 | 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | import com.fasterxml.jackson.annotation.JsonInclude; 8 | import com.fasterxml.jackson.core.type.TypeReference; 9 | import com.fasterxml.jackson.databind.DeserializationFeature; 10 | import com.fasterxml.jackson.databind.ObjectMapper; 11 | 12 | import lombok.extern.slf4j.Slf4j; 13 | 14 | /** 15 | * @author knightliao 16 | * @email knightliao@gmail.com 17 | * @date 2021/8/11 17:13 18 | */ 19 | @Slf4j 20 | public class JsonUtils { 21 | 22 | private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper(); 23 | 24 | static { 25 | OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 26 | OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); 27 | } 28 | 29 | public static String toJson(Map map) { 30 | 31 | if (null == map || map.size() == 0) { 32 | return ""; 33 | } 34 | 35 | try { 36 | return OBJECT_MAPPER.writeValueAsString(map); 37 | } catch (Exception ex) { 38 | log.error("to json cause exception", ex); 39 | throw new RuntimeException(ex); 40 | } 41 | } 42 | 43 | /** 44 | * JSON串转换为Java泛型对象,可以是各种类型,此方法最为强大。用法看测试用例。 45 | * 46 | * @param 47 | * @param jsonString JSON字符串 48 | * @param tr TypeReference,例如: new TypeReference< List >(){} 49 | * @return List对象列表 50 | */ 51 | @SuppressWarnings("unchecked") 52 | public static T json2GenericObject(String jsonString, 53 | TypeReference tr) { 54 | 55 | if (jsonString == null || "".equals(jsonString)) { 56 | return null; 57 | } else { 58 | try { 59 | return (T) OBJECT_MAPPER.readValue(jsonString, tr); 60 | } catch (Exception e) { 61 | log.warn("json error:" + e.getMessage()); 62 | } 63 | } 64 | return null; 65 | } 66 | 67 | public static String toJson(Object obj) { 68 | 69 | if (null == obj) { 70 | return ""; 71 | } 72 | 73 | try { 74 | return OBJECT_MAPPER.writeValueAsString(obj); 75 | } catch (Exception ex) { 76 | log.error("to json cause exception", ex); 77 | throw new RuntimeException(ex); 78 | } 79 | } 80 | 81 | public static T fromJson(String json, Class clz) { 82 | 83 | if (StringUtils.isBlank(json)) { 84 | return null; 85 | } 86 | 87 | try { 88 | return OBJECT_MAPPER.readValue(json, clz); 89 | } catch (Exception ex) { 90 | throw new RuntimeException(ex); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/trans/NetBytesSwitchUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.trans; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.nio.ByteBuffer; 5 | import java.nio.ByteOrder; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | /** 11 | * @author liaoqiqi 12 | * @version 2014-8-21 13 | */ 14 | public class NetBytesSwitchUtil { 15 | 16 | private static final Logger logger = LoggerFactory 17 | .getLogger(NetBytesSwitchUtil.class); 18 | 19 | private static String STR_ENCODE = "GBK"; 20 | 21 | public static byte[] IntToBytes(int x) { 22 | byte[] b = new byte[4]; 23 | for (int i = 0; i < b.length; i++) { 24 | b[i] = (byte) (x >> i * 8 & 0xFF); 25 | } 26 | 27 | return b; 28 | } 29 | 30 | public static byte[] LongToBytes(long x) { 31 | byte[] b = new byte[4]; 32 | for (int i = 0; i < b.length; i++) { 33 | b[i] = (byte) (int) (x >> i * 8 & 0xFF); 34 | } 35 | 36 | return b; 37 | } 38 | 39 | public static byte[] LongToLongBytes(long x) { 40 | byte[] b = new byte[8]; 41 | for (int i = 0; i < b.length; i++) { 42 | b[i] = (byte) (int) (x >> i * 8 & 0xFF); 43 | } 44 | 45 | return b; 46 | } 47 | 48 | public static int BytesToInt(byte[] x) { 49 | int iOutcome = 0; 50 | 51 | for (int i = 0; i < 4; i++) { 52 | byte bLoop = x[i]; 53 | iOutcome += ((bLoop & 0xFF) << 8 * i); 54 | } 55 | 56 | return iOutcome; 57 | } 58 | 59 | public static long BytesToLong(byte[] x) { 60 | long iOutcome = 0L; 61 | 62 | for (int i = 0; i < 4; i++) { 63 | byte bLoop = x[i]; 64 | iOutcome += ((bLoop & 0xFFL) << 8 * i); 65 | } 66 | 67 | return iOutcome; 68 | } 69 | 70 | public static byte[] StringToBytes(String input) { 71 | byte[] result = (byte[]) null; 72 | if (input != null) { 73 | try { 74 | result = input.getBytes(STR_ENCODE); 75 | } catch (UnsupportedEncodingException e) { 76 | logger.debug("StringToBytes", e); 77 | } 78 | } 79 | return result; 80 | } 81 | 82 | public static String bytesToString(byte[] input) { 83 | String result = null; 84 | if (input != null) { 85 | try { 86 | result = new String(input, STR_ENCODE).trim(); 87 | } catch (Exception e) { 88 | throw new RuntimeException("This jdk does not support GBK"); 89 | } 90 | } 91 | return result; 92 | } 93 | 94 | public static byte[] StringToBytes(String input, int length) { 95 | byte[] tmp = StringToBytes(input); 96 | byte[] bytes = new byte[length]; 97 | System.arraycopy(tmp, 0, bytes, 0, tmp.length); 98 | return bytes; 99 | } 100 | 101 | public static long BytesToLong2(byte[] x) throws Exception { 102 | try { 103 | ByteBuffer buffer = ByteBuffer.allocate(x.length); 104 | buffer.order(ByteOrder.LITTLE_ENDIAN); 105 | buffer.put(x); 106 | return buffer.getLong(0); 107 | } catch (Exception e) { 108 | } 109 | throw new Exception("exception in transfer byte to long..."); 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /pack-utils/src/main/java/com/github/knightliao/middle/utils/trans/UnsignedSwitchUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.trans; 2 | 3 | /** 4 | * @author liaoqiqi 5 | * @version 2014-8-21 6 | */ 7 | public class UnsignedSwitchUtil { 8 | 9 | public static long UintToLong(int x) { 10 | return x & 0xFFFFFFFF; 11 | } 12 | 13 | public static int UShortToInt(short x) { 14 | return x & 0xFFFF; 15 | } 16 | 17 | public static int LongToUint(long x) { 18 | return (int) (x & 0xFFFFFFFF); 19 | } 20 | 21 | public static short IntToUshort(int x) { 22 | return (short) (x & 0xFFFF); 23 | } 24 | } -------------------------------------------------------------------------------- /pack-utils/src/test/java/com/github/knightliao/middle/utils/test/common/TestCommonTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.test.common; 2 | 3 | import org.junit.Test; 4 | 5 | import com.github.knightliao.middle.utils.devlopment.IdeaDetectUtil; 6 | import com.github.knightliao.test.support.utils.TestUtils; 7 | 8 | /** 9 | * @author knightliao 10 | * @date 2016/12/10 14:47 11 | */ 12 | public class TestCommonTestCase { 13 | 14 | @Test 15 | public void test() { 16 | 17 | TestUtils.testAllClassUnderPackage("com.github.knightliao.middle.utils"); 18 | } 19 | 20 | @Test 21 | public void testIDea() { 22 | 23 | System.out.println(IdeaDetectUtil.isInIDea()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /pack-utils/src/test/java/com/github/knightliao/middle/utils/test/lang/ClassUtilsTestCase.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.utils.test.lang; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import com.github.knightliao.middle.utils.lang.ClassUtils; 7 | 8 | /** 9 | * @author liaoqiqi 10 | * @version 2014-9-1 11 | */ 12 | public class ClassUtilsTestCase { 13 | 14 | /** 15 | * 16 | */ 17 | @Test 18 | public void getValeByTypeTest() { 19 | 20 | try { 21 | 22 | Integer integer = (Integer) ClassUtils.getValeByType(Integer.class, 23 | "3000 "); 24 | System.out.println(integer); 25 | Assert.assertEquals(new Integer(3000), integer); 26 | 27 | } catch (Exception e) { 28 | 29 | Assert.assertTrue(false); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /pack-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-web 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | javax.servlet 20 | javax.servlet-api 21 | 3.1.0 22 | 23 | 24 | 25 | org.aspectj 26 | aspectjweaver 27 | 28 | 29 | 30 | org.apache.commons 31 | commons-lang3 32 | 33 | 34 | 35 | com.github.knightliao.middle 36 | pack-utils 37 | 0.1.16-SNAPSHOT 38 | 39 | 40 | 41 | com.github.knightliao.middle 42 | pack-log 43 | 0.1.16-SNAPSHOT 44 | 45 | 46 | 47 | com.github.knightliao.middle 48 | pack-api 49 | 0.1.16-SNAPSHOT 50 | 51 | 52 | 53 | com.github.knightliao.middle 54 | pack-metrics 55 | 0.1.16-SNAPSHOT 56 | 57 | 58 | 59 | com.github.knightliao.middle 60 | pack-trace 61 | 0.1.16-SNAPSHOT 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /pack-web/src/main/java/com/github/knightliao/middle/web/aop/QpsAnnotation.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.web.aop; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/8/20 01:19 13 | */ 14 | @Target(ElementType.METHOD) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | public @interface QpsAnnotation { 18 | 19 | // 打印请求参数 20 | boolean printParam() default false; 21 | 22 | // 超过一定时间才进行打印,小于0则没有这个限制 23 | int logIfLongTime() default -1; 24 | 25 | // 只有需求时才打日志 26 | boolean logIfNeed() default false; 27 | } 28 | -------------------------------------------------------------------------------- /pack-web/src/main/java/com/github/knightliao/middle/web/filter/HttpServletRequestFilter.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.web.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | 12 | import com.github.knightliao.middle.trace.MyTraceUtils; 13 | import com.github.knightliao.middle.thread.MyThreadContext; 14 | 15 | import lombok.extern.slf4j.Slf4j; 16 | 17 | /** 18 | * @author knightliao 19 | * @email knightliao@gmail.com 20 | * @date 2021/8/28 17:47 21 | */ 22 | @Slf4j 23 | public class HttpServletRequestFilter implements Filter { 24 | 25 | private HttpServletRequestFilterCallback httpServletRequestFilterCallback; 26 | 27 | public HttpServletRequestFilter(HttpServletRequestFilterCallback httpServletRequestFilterCallback) { 28 | this.httpServletRequestFilterCallback = httpServletRequestFilterCallback; 29 | } 30 | 31 | public HttpServletRequestFilter() { 32 | 33 | } 34 | 35 | @Override 36 | public void init(FilterConfig filterConfig) throws ServletException { 37 | 38 | } 39 | 40 | @Override 41 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 42 | throws IOException, ServletException { 43 | 44 | try { 45 | 46 | MyThreadContext.init(); 47 | 48 | // 49 | traceProcess(request, response); 50 | 51 | // 52 | if (httpServletRequestFilterCallback != null) { 53 | httpServletRequestFilterCallback.before(request, response); 54 | } 55 | 56 | // 57 | chain.doFilter(request, response); 58 | 59 | // 60 | if (httpServletRequestFilterCallback != null) { 61 | httpServletRequestFilterCallback.after(request, response); 62 | } 63 | 64 | } finally { 65 | 66 | MyThreadContext.clean(); 67 | } 68 | 69 | } 70 | 71 | private void traceProcess(ServletRequest serverRequest, ServletResponse response) { 72 | 73 | MyTraceUtils.startTraceFromHttpRequest(serverRequest); 74 | 75 | MyTraceUtils.addTraceToResponse(response); 76 | } 77 | 78 | @Override 79 | public void destroy() { 80 | 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /pack-web/src/main/java/com/github/knightliao/middle/web/filter/HttpServletRequestFilterCallback.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.web.filter; 2 | 3 | import javax.servlet.ServletRequest; 4 | import javax.servlet.ServletResponse; 5 | 6 | /** 7 | * @author knightliao 8 | * @email knightliao@gmail.com 9 | * @date 2021/8/28 17:49 10 | */ 11 | public interface HttpServletRequestFilterCallback { 12 | 13 | void before(ServletRequest request, ServletResponse servletResponse); 14 | 15 | void after(ServletRequest request, ServletResponse servletResponse); 16 | } 17 | -------------------------------------------------------------------------------- /pack-web/src/main/java/com/github/knightliao/middle/web/utils/CookieUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.web.utils; 2 | 3 | import javax.servlet.http.Cookie; 4 | import javax.servlet.http.HttpServletRequest; 5 | import javax.servlet.http.HttpServletResponse; 6 | 7 | /** 8 | * @author liaoqiqi 9 | * @version 2014-2-7 10 | */ 11 | public class CookieUtils { 12 | 13 | /** 14 | * https://code.google.com/p/util-java/source/browse/trunk/src/utils/ 15 | * CookieUtils.java?r=6 16 | * 17 | * @param response 18 | * @param name 19 | * @param value 20 | * @param domain 21 | * @param maxAge 22 | */ 23 | public static void setCookie(HttpServletResponse response, String name, 24 | String value, String domain, int maxAge) { 25 | if (value == null) { 26 | value = ""; 27 | } 28 | Cookie cookie = new Cookie(name, value); 29 | cookie.setMaxAge(maxAge); 30 | if (domain != null && !"".equals(domain)) { 31 | cookie.setDomain(domain); 32 | } 33 | cookie.setPath("/"); 34 | response.addCookie(cookie); 35 | } 36 | 37 | public static String getCookieValue(HttpServletRequest request, 38 | String cookieName) { 39 | if (cookieName == null || request == null) { 40 | return null; 41 | } 42 | Cookie[] cks = request.getCookies(); 43 | if (cks == null) { 44 | return null; 45 | } 46 | for (Cookie cookie : cks) { 47 | if (cookieName.equals(cookie.getName())) { 48 | return cookie.getValue(); 49 | } 50 | } 51 | return null; 52 | } 53 | 54 | /** 55 | * @param request 56 | * @param cookieName 57 | * @return 58 | */ 59 | public static Cookie getCookie(HttpServletRequest request, String cookieName) { 60 | if (cookieName == null || request == null) { 61 | return null; 62 | } 63 | Cookie[] cks = request.getCookies(); 64 | if (cks == null) { 65 | return null; 66 | } 67 | for (Cookie cookie : cks) { 68 | if (cookieName.equals(cookie.getName())) { 69 | return cookie; 70 | } 71 | } 72 | return null; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /pack-web/src/main/java/com/github/knightliao/middle/web/utils/WebIpUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.web.utils; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | /** 6 | * @author liaoqiqi 7 | * @version 2014-4-23 8 | */ 9 | public class WebIpUtils { 10 | 11 | public static String getIp(HttpServletRequest request) { 12 | if (null == request) { 13 | return null; 14 | } 15 | String ip = request.getHeader("x-forwarded-for"); 16 | if (!validateIp(ip)) { 17 | ip = request.getHeader("Proxy-Client-IP"); 18 | if (!validateIp(ip)) { 19 | ip = request.getHeader("WL-Proxy-Client-IP"); 20 | if (!validateIp(ip)) { 21 | ip = request.getRemoteAddr(); 22 | } 23 | } 24 | } 25 | return ip; 26 | } 27 | 28 | private static boolean validateIp(String ip) { 29 | return ip != null && ip.length() != 0 && !ip.equalsIgnoreCase("unknown"); 30 | } 31 | 32 | /** 33 | * 获取连接WEB服务器的真实IP,前端有UTR的拦截请求 34 | */ 35 | public static String getIpAddr(HttpServletRequest request) { 36 | 37 | String ip = request.getHeader("X-Forwarded-For"); 38 | 39 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 40 | ip = request.getHeader("Proxy-Client-IP"); 41 | 42 | } else { 43 | String[] ipArray = ip.split("\\,"); 44 | ip = ipArray[0];//  获取第一个IP 45 | } 46 | 47 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 48 | ip = request.getHeader("WL-Proxy-Client-IP"); 49 | } 50 | 51 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 52 | ip = request.getHeader("clientip"); 53 | } 54 | 55 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 56 | ip = request.getRemoteAddr(); 57 | } 58 | 59 | return ip; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /pack-zk/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.knightliao.middle 7 | middle-libs 8 | 0.1.16-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | pack-zk 13 | jar 14 | 0.1.16-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.curator 20 | curator-framework 21 | 5.2.0 22 | 23 | 24 | log4j 25 | log4j 26 | 27 | 28 | 29 | 30 | 31 | org.apache.curator 32 | curator-recipes 33 | 5.2.0 34 | 35 | 36 | 37 | org.apache.commons 38 | commons-lang3 39 | 40 | 41 | 42 | com.github.knightliao.middle 43 | pack-utils 44 | 0.1.16-SNAPSHOT 45 | 46 | 47 | 48 | com.github.knightliao.middle 49 | pack-lang 50 | 0.1.16-SNAPSHOT 51 | 52 | 53 | 54 | com.github.knightliao.middle.bom 55 | all-bom 56 | 0.1.16-SNAPSHOT 57 | test 58 | pom 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /pack-zk/src/main/java/com/github/knightliao/middle/zk/domain/WatcherWrapper.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.zk.domain; 2 | 3 | import org.apache.zookeeper.Watcher; 4 | 5 | import com.google.common.base.Preconditions; 6 | 7 | import lombok.Getter; 8 | 9 | /** 10 | * @author knightliao 11 | * @email knightliao@gmail.com 12 | * @date 2021/10/5 17:38 13 | */ 14 | @Getter 15 | public abstract class WatcherWrapper implements Watcher { 16 | 17 | protected Watcher rawWatcher; 18 | 19 | public WatcherWrapper(Watcher watcher) { 20 | Preconditions.checkNotNull(watcher); 21 | this.rawWatcher = watcher; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pack-zk/src/main/java/com/github/knightliao/middle/zk/domain/select/ISelectState.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.zk.domain.select; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/5 22:02 7 | */ 8 | public interface ISelectState { 9 | 10 | boolean isLeading(); 11 | } 12 | -------------------------------------------------------------------------------- /pack-zk/src/main/java/com/github/knightliao/middle/zk/domain/zk/IZkClient.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.zk.domain.zk; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.Executor; 5 | 6 | import org.apache.curator.framework.state.ConnectionStateListener; 7 | import org.apache.zookeeper.Watcher; 8 | 9 | import com.github.knightliao.middle.zk.domain.select.ISelectState; 10 | 11 | /** 12 | * @author knightliao 13 | * @email knightliao@gmail.com 14 | * @date 2021/10/5 17:33 15 | */ 16 | public interface IZkClient { 17 | 18 | void start(); 19 | 20 | void stop(); 21 | 22 | void listenConnectionState(ConnectionStateListener listener, Executor executor); 23 | 24 | boolean isAvailable(); 25 | 26 | void create(String path, boolean ephemeral, byte[] data); 27 | 28 | void create(List paths, boolean ephemeral); 29 | 30 | void setData(String path, byte[] data); 31 | 32 | String getDataAsString(String path); 33 | 34 | List getChildren(String path); 35 | 36 | byte[] getData(String path); 37 | 38 | void delete(String path); 39 | 40 | void delete(List paths); 41 | 42 | Watcher watchData(final String path, final Watcher watcher); 43 | 44 | Watcher watchChildren(final String path, final Watcher watcher); 45 | 46 | void unWatch(Watcher watcher); 47 | 48 | ISelectState joinSelect(); 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /pack-zk/src/main/java/com/github/knightliao/middle/zk/impl/LeaderLatchServer.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.zk.impl; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | 6 | import org.apache.curator.framework.CuratorFramework; 7 | import org.apache.curator.framework.recipes.leader.LeaderLatch; 8 | import org.apache.curator.framework.recipes.leader.LeaderLatchListener; 9 | 10 | import lombok.Getter; 11 | import lombok.Setter; 12 | import lombok.extern.slf4j.Slf4j; 13 | 14 | /** 15 | * @author knightliao 16 | * @email knightliao@gmail.com 17 | * @date 2021/10/5 21:32 18 | */ 19 | @Slf4j 20 | public class LeaderLatchServer implements LeaderLatchListener, Closeable { 21 | /** 22 | * server name 23 | **/ 24 | @Getter 25 | private final String serverName; 26 | 27 | @Getter 28 | private LeaderLatch leaderLatch; 29 | 30 | @Setter 31 | private Runnable onTakeLead; 32 | 33 | @Setter 34 | private Runnable onLostLead; 35 | 36 | public LeaderLatchServer(CuratorFramework client, String path, String serverName) { 37 | 38 | this.serverName = serverName; 39 | /** 传入客户端、监听路径、监听器 */ 40 | leaderLatch = new LeaderLatch(client, path); 41 | leaderLatch.addListener(this); 42 | } 43 | 44 | public void start() throws Exception { 45 | leaderLatch.start(); 46 | log.info(serverName + " started done"); 47 | } 48 | 49 | @Override 50 | public void close() throws IOException { 51 | leaderLatch.close(); 52 | } 53 | 54 | @Override 55 | public void isLeader() { 56 | 57 | synchronized(this) { 58 | 59 | doCallback(); 60 | 61 | } 62 | } 63 | 64 | @Override 65 | public void notLeader() { 66 | synchronized(this) { 67 | 68 | doCallback(); 69 | } 70 | } 71 | 72 | private void doCallback() { 73 | if (leaderLatch.hasLeadership()) { 74 | if (onTakeLead != null) { 75 | onTakeLead.run(); 76 | } 77 | log.info(serverName + " has gained leadership"); 78 | } else { 79 | if (onLostLead != null) { 80 | onLostLead.run(); 81 | } 82 | log.info(serverName + " has lost leadership"); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /pack-zk/src/main/java/com/github/knightliao/middle/zk/tmp.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.zk; 2 | 3 | /** 4 | * @author knightliao 5 | * @email knightliao@gmail.com 6 | * @date 2021/10/5 17:32 7 | */ 8 | public class tmp { 9 | } 10 | -------------------------------------------------------------------------------- /pack-zk/src/test/java/com/github/knightliao/middle/zk/MasterTestMain.java: -------------------------------------------------------------------------------- 1 | package com.github.knightliao.middle.zk; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.apache.curator.RetryPolicy; 8 | import org.apache.curator.framework.CuratorFramework; 9 | import org.apache.curator.framework.CuratorFrameworkFactory; 10 | import org.apache.curator.retry.RetryNTimes; 11 | import org.apache.curator.utils.CloseableUtils; 12 | 13 | import com.github.knightliao.middle.zk.impl.LeaderLatchServer; 14 | 15 | /** 16 | * @author knightliao 17 | * @email knightliao@gmail.com 18 | * @date 2021/10/5 22:37 19 | */ 20 | public class MasterTestMain { 21 | 22 | /** 23 | * 重试策略:重试间隔时间为1000ms; 最多重试3次; 24 | */ 25 | private static RetryPolicy retryPolicy = new RetryNTimes(3, 1000); 26 | 27 | public static void main(String[] args) throws Exception { 28 | 29 | List clients = new ArrayList<>(16); 30 | List workServers = new ArrayList<>(16); 31 | 32 | String name = "server-"; 33 | 34 | try { 35 | // 模拟是个server,进行争抢master 36 | for (int i = 0; i < 4; i++) { 37 | CuratorFramework client = CuratorFrameworkFactory.builder(). 38 | connectString("127.0.0.1:8584"). 39 | sessionTimeoutMs(5000). 40 | connectionTimeoutMs(5000). 41 | retryPolicy(retryPolicy). 42 | build(); 43 | clients.add(client); 44 | 45 | LeaderLatchServer server = new LeaderLatchServer(client, "/zk-master", name + i); 46 | workServers.add(server); 47 | client.start(); 48 | server.start(); 49 | } 50 | 51 | } finally { 52 | 53 | TimeUnit.SECONDS.sleep(3); 54 | 55 | System.out.println("开始关闭!"); 56 | LeaderLatchServer server; 57 | 58 | for (int i = 0; i < workServers.size(); i++) { 59 | 60 | server = workServers.get(i); 61 | 62 | System.out.println(server.getServerName() + " " + server.getLeaderLatch().hasLeadership()); 63 | } 64 | 65 | // 关闭server会进行master切换 66 | for (int i = 0; i < workServers.size(); i++) { 67 | 68 | server = workServers.get(i); 69 | 70 | CloseableUtils.closeQuietly(server); 71 | 72 | // 为方便观察,这里阻塞几秒 73 | TimeUnit.SECONDS.sleep(3); 74 | } 75 | for (CuratorFramework client : clients) { 76 | CloseableUtils.closeQuietly(client); 77 | } 78 | } 79 | } 80 | } 81 | --------------------------------------------------------------------------------