├── .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 | [](https://www.apache.org/licenses/LICENSE-2.0.txt)
6 | [](https://travis-ci.com/knightliao/middle-libs)
7 | [](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