From 67c917562eae1a2ccb096e0b9888d0917a5aa861 Mon Sep 17 00:00:00 2001 From: zhaoyong_sh Date: Thu, 8 Apr 2021 11:38:10 +0800 Subject: [PATCH] init project --- archetype-simple/.gitignore | 33 +++ archetype-simple/README.md | 15 ++ archetype-simple/pom.xml | 21 ++ .../META-INF/maven/archetype-metadata.xml | 76 +++++++ .../__rootArtifactId__-api-impl/pom.xml | 96 +++++++++ .../ServiceTest.java | 9 + .../__rootArtifactId__-api/pom.xml | 11 + .../ServiceTest.java | 9 + .../__rootArtifactId__-client/pom.xml | 42 ++++ .../src.main.java.client/ClientTest.java | 9 + .../src/main/resources/bootstrap.yml | 0 .../src/main/resources/logback-spring.xml | 201 ++++++++++++++++++ .../__rootArtifactId__-test/pom.xml | 23 ++ .../resources/archetype-resources/pom.xml | 128 +++++++++++ archetype-standard/.gitignore | 33 +++ archetype-standard/README.md | 34 +++ archetype-standard/pom.xml | 20 ++ .../META-INF/maven/archetype-metadata.xml | 114 ++++++++++ .../__rootArtifactId__-client/pom.xml | 42 ++++ .../src.main.java.client/ClientTest.java | 9 + .../src/main/resources/bootstrap.yml | 0 .../src/main/resources/logback-spring.xml | 201 ++++++++++++++++++ .../__rootArtifactId__-common/pom.xml | 63 ++++++ .../src/main/java/common/CommonTest.java | 9 + .../__rootArtifactId__-dal/pom.xml | 45 ++++ .../src/main/java/dal/DalTest.java | 9 + .../__rootArtifactId__-integration/pom.xml | 23 ++ .../IntegrationTest.java | 9 + .../__rootArtifactId__-manager-api/pom.xml | 23 ++ .../ManagerTest.java | 9 + .../__rootArtifactId__-manager/pom.xml | 28 +++ .../main/java/manager/impl/ManagerTest.java | 9 + .../__rootArtifactId__-service-api/pom.xml | 24 +++ .../ServiceTest.java | 9 + .../__rootArtifactId__-service/pom.xml | 24 +++ .../ServiceTest.java | 9 + .../__rootArtifactId__-test/pom.xml | 23 ++ .../resources/archetype-resources/pom.xml | 128 +++++++++++ common-common/.gitignore | 33 +++ common-common/pom.xml | 33 +++ ...tractSimpleRegisterableServiceFactory.java | 93 ++++++++ .../spring/SimpleRegisterableService.java | 18 ++ pom.xml | 70 ++++++ smart-datasource/pom.xml | 43 ++++ .../azxo/framework/datasource/DataSource.java | 23 ++ .../datasource/DataSourcePointcut.java | 26 +++ .../datasource/DatasourceContextHolder.java | 26 +++ .../datasource/EnableDataSource.java | 24 +++ .../ProxyShardingConfiguration.java | 41 ++++ .../ShardingConfigurationSelector.java | 20 ++ .../datasource/ShardingInterceptor.java | 129 +++++++++++ .../framework/datasource/SmartDatasource.java | 25 +++ 52 files changed, 2174 insertions(+) create mode 100644 archetype-simple/.gitignore create mode 100644 archetype-simple/README.md create mode 100644 archetype-simple/pom.xml create mode 100644 archetype-simple/src/main/resources/META-INF/maven/archetype-metadata.xml create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/pom.xml create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/src.main.java.service.api/ServiceTest.java create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/pom.xml create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/src.main.java.service.impl/ServiceTest.java create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/bootstrap.yml create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml create mode 100644 archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml create mode 100644 archetype-simple/src/main/resources/archetype-resources/pom.xml create mode 100644 archetype-standard/.gitignore create mode 100644 archetype-standard/README.md create mode 100644 archetype-standard/pom.xml create mode 100644 archetype-standard/src/main/resources/META-INF/maven/archetype-metadata.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/bootstrap.yml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/src/main/java/common/CommonTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/src/main/java/dal/DalTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/src.main.java.integration/IntegrationTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/src.main.java.manager.api/ManagerTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/src/main/java/manager/impl/ManagerTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/src.main.java.service.api/ServiceTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/src.main.java.service.impl/ServiceTest.java create mode 100644 archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml create mode 100644 archetype-standard/src/main/resources/archetype-resources/pom.xml create mode 100644 common-common/.gitignore create mode 100644 common-common/pom.xml create mode 100644 common-common/src/main/java/cn/azxo/framework/common/spring/AbstractSimpleRegisterableServiceFactory.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/spring/SimpleRegisterableService.java create mode 100644 pom.xml create mode 100644 smart-datasource/pom.xml create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSource.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSourcePointcut.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/DatasourceContextHolder.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/EnableDataSource.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/ProxyShardingConfiguration.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingConfigurationSelector.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingInterceptor.java create mode 100644 smart-datasource/src/main/java/cn/azxo/framework/datasource/SmartDatasource.java diff --git a/archetype-simple/.gitignore b/archetype-simple/.gitignore new file mode 100644 index 0000000..33dedbb --- /dev/null +++ b/archetype-simple/.gitignore @@ -0,0 +1,33 @@ +# maven ignore +target/ +*.jar +*.war +*.zip +*.tar +*.tar.gz + +# eclipse ignore +.settings/ +.project +.classpath + +# idea ignore +.idea/ +*.ipr +*.iml +*.iws + +# temp ignore +logs/ +*.doc +*.log +*.cache +*.diff +*.patch +*.tmp + +# system ignore +.DS_Store +Thumbs.db + +*.class \ No newline at end of file diff --git a/archetype-simple/README.md b/archetype-simple/README.md new file mode 100644 index 0000000..0e2fb87 --- /dev/null +++ b/archetype-simple/README.md @@ -0,0 +1,15 @@ +# archetype-simple + +公司项目简单骨架 + +## xxx-api +业务逻辑模块,包前缀标准为:``cn.axzo.xxx.service.api.`` 里面定义的接口直接给 xxx-client 中的 controller 调用,所有的业务逻辑都会被封装到它的实现类当中。业务 Controller 只会调用这个 Service 并不关注具体的业务实现细节。可以定义 request 包用于 service 接口的请求参数,可以定义 response 用于 service 接口的响应参数。 + +## xxx-api-impl +业务逻辑实现模块,是 xxx-service-api 模块定义的接口的实现类接口,包前缀标准为:``cn.axzo.xxx.service.impl.``.它是聚合 xxx-manager 这个通用处理层的,它主要是集成数据库访问以及第三方接口的调用。它的作用是业务逻辑的编排。 + +## xxx-client +请求处理模块,包前缀标准为:``cn.axzo.xxx.client.`` 里面可以定义 controller 包用于定义响应前端请求的控制器,config 包用于定义整个项目的配置(Redis 配置,Xxl Job 配置,Spring Web 统一异常配置,数据库配置等),interceptor 包用来定义 spring mvc 中的拦截器等。 + +## xxx-test: +单元测试模块:所有模块的单元测试都可以放在这个模块里。 diff --git a/archetype-simple/pom.xml b/archetype-simple/pom.xml new file mode 100644 index 0000000..9e717c3 --- /dev/null +++ b/archetype-simple/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + + cn.axzo.archetype + archetype-simple + 1.0.0 + archetype-simple + jar + + + \ No newline at end of file diff --git a/archetype-simple/src/main/resources/META-INF/maven/archetype-metadata.xml b/archetype-simple/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 0000000..fab586e --- /dev/null +++ b/archetype-simple/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,76 @@ + + + + + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.properties + + + + + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.properties + + + + + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.properties + **/*.yml + **/*.xml + + + + + + + + src/test/java + + **/*.java + + + + src/test/resources + + **/*.xml + + + + + + diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/pom.xml b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/pom.xml new file mode 100644 index 0000000..d5567d6 --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-api-impl + + + + ${groupId} + ${rootArtifactId}-api + ${project.version} + + + net.logstash.logback + logstash-logback-encoder + + + org.springframework.boot + spring-boot-starter-aop + + + javax.validation + validation-api + + + org.hibernate + hibernate-validator + + + org.redisson + redisson + + + com.alibaba + fastjson + + + joda-time + joda-time + + + org.apache.commons + commons-lang3 + + + org.apache.commons + commons-collections4 + + + cn.hutool + hutool-all + + + com.google.guava + guava + + + org.projectlombok + lombok + + + mysql + mysql-connector-java + + + com.zaxxer + HikariCP + + + org.springframework.boot + spring-boot-starter-jdbc + + + com.baomidou + mybatis-plus-boot-starter + + + org.apache.velocity + velocity-engine-core + compile + + + com.baomidou + mybatis-plus-generator + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/src.main.java.service.api/ServiceTest.java b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/src.main.java.service.api/ServiceTest.java new file mode 100644 index 0000000..021f3c4 --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api-impl/src.main.java.service.api/ServiceTest.java @@ -0,0 +1,9 @@ +package ${package}.service.api; + +/** + * @author zhaoyong + * + */ +public class ServiceTest { + +} diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/pom.xml b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/pom.xml new file mode 100644 index 0000000..804e9c0 --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/pom.xml @@ -0,0 +1,11 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-api + + diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/src.main.java.service.impl/ServiceTest.java b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/src.main.java.service.impl/ServiceTest.java new file mode 100644 index 0000000..ae867db --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-api/src.main.java.service.impl/ServiceTest.java @@ -0,0 +1,9 @@ +package ${package}.service.impl; + +/** + * @author zhaoyong + * + */ +public class ServiceTest { + +} diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml new file mode 100644 index 0000000..2e33c3d --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-client + + + + ${groupId} + ${rootArtifactId}-api + ${project.version} + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + guava + com.google.guava + + + + + org.apache.httpcomponents + httpclient + + + com.xuxueli + xxl-job-core + + + + diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java new file mode 100644 index 0000000..e23c35e --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java @@ -0,0 +1,9 @@ +package ${package}.client; + +/** + * @author zhaoyong + * + */ +public class ClientTest { + +} diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/bootstrap.yml b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..e69de29 diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..7cff95c --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml @@ -0,0 +1,201 @@ + + + + + + + + + + + + + ${PROJECT_NAME} + + + + ${LOG_PATH}/error.log + + ${LOG_PATH}/error/error-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + error + ACCEPT + DENY + + + + + + ${LOG_PATH}/warn.log + + ${LOG_PATH}/warn/warn-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + warn + ACCEPT + DENY + + + + + + ${LOG_PATH}/info.log + + ${LOG_PATH}/info/info-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + + info + ACCEPT + DENY + + + + + + + ${LOG_PATH}/debug.log + + ${LOG_PATH}/debug/debug-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + + debug + ACCEPT + DENY + + + + + + + ${LOG_PATTERN} + utf-8 + + + + info + + + + + + + + + @timestamp + yyyy-MM-dd'T'HH:mm:ss.SSSx + GMT+8 + + + + { + + "app": "${appName}", + "level": "%level", + + "thread": "%thread", + "class": "%logger{40}", + "message":"%message", + "m":"#asJson{%message}", + "stack_trace": "%exception{10}" + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml new file mode 100644 index 0000000..aef023f --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-test + + + + ${groupId} + ${rootArtifactId}-client + ${project.version} + + + org.springframework.boot + spring-boot-starter-test + + + + diff --git a/archetype-simple/src/main/resources/archetype-resources/pom.xml b/archetype-simple/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 0000000..eec80d7 --- /dev/null +++ b/archetype-simple/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,128 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + + ${groupId} + ${rootArtifactId} + ${version} + pom + + + 4.4 + 3.5 + 5.5.7 + 6.1 + 2.2.0 + 3.13.2 + 1.2.47 + 2.10.6 + 2.0.0.Final + 6.0.16.Final + 4.5.6 + 3.3.2 + 2.0 + 2.2.1.RELEASE + Hoxton.SR6 + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + com.baomidou + mybatis-plus-generator + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + + joda-time + joda-time + ${joda-time.version} + + + net.logstash.logback + logstash-logback-encoder + ${logstash-logback-encoder.version} + + + javax.validation + validation-api + ${javax.validation.version} + + + org.hibernate + hibernate-validator + ${hibernate.validator.version} + + + org.apache.httpcomponents + httpclient + ${http.client.version} + + + cn.hutool + hutool-all + ${hutool-all.version} + + + org.apache.velocity + velocity-engine-core + ${velocity-engine-core.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + org.redisson + redisson + ${redisson.version} + + + com.xuxueli + xxl-job-core + ${xxl.job.version} + + + + + diff --git a/archetype-standard/.gitignore b/archetype-standard/.gitignore new file mode 100644 index 0000000..33dedbb --- /dev/null +++ b/archetype-standard/.gitignore @@ -0,0 +1,33 @@ +# maven ignore +target/ +*.jar +*.war +*.zip +*.tar +*.tar.gz + +# eclipse ignore +.settings/ +.project +.classpath + +# idea ignore +.idea/ +*.ipr +*.iml +*.iws + +# temp ignore +logs/ +*.doc +*.log +*.cache +*.diff +*.patch +*.tmp + +# system ignore +.DS_Store +Thumbs.db + +*.class \ No newline at end of file diff --git a/archetype-standard/README.md b/archetype-standard/README.md new file mode 100644 index 0000000..059f007 --- /dev/null +++ b/archetype-standard/README.md @@ -0,0 +1,34 @@ +# archetype-standard + +公司项目标准骨架 + +## xxx-common +通用模块,包前缀标准为:``cn.axzo.xxx.common.`` 里面可以定义 constants 包用来定义系统中使用到的常量值;context 包用来定义系统当中要使用到的上下文;enums 包用来定义系统当中使用到的枚举值;utils 包用来定义系统当中的工具类等。 + +因为这个模块都会被其它模块引用,所以这个模块的 pom.xml 依赖的 jar 包必须是公共能用的,比如:lombok,common-utils ,guava,fastjson 等。不应该引用的 jar 比如携程的配置中心 apollo(xxx-client 依赖);远程服务调用 dubbo 或者 feign (xxx-integration 依赖)。 + +## xxx-dal +数据访问模块,包前缀标准为:``cn.axzo.xxx.dal.`` 里面可以定义 mapper 包用来定义服务访问 mapper 接口;repository 包用来定义包装 mapper 接口(主要功能是可以代理增强 mapper);entity 包用来定义数据库实体访问对象。domain 包用来定义访问数据库的请求对象等。 + +因为这个模块的主要功能是访问数据库,所以这个模块的 pom.xml 依赖的 jar 包应该是和数据访问相关的。比如:jdbc 驱动;mybatis 等 + +## xxx-integration +外部接口集成模块,包前缀标准为:``cn.axzo.xxx.integration.`` 里面可以定义 service 包用来定义 feign 接口,定义 client 接口用来包装 service 包中的接口(代理增强 service:打印日志,异常处理);可以定义 dto 包用来定义远程请求与响应对象。 + +## xxx-client +请求处理模块,包前缀标准为:``cn.axzo.xxx.client.`` 里面可以定义 controller 包用于定义响应前端请求的控制器,config 包用于定义整个项目的配置(Redis 配置,Xxl Job 配置,Spring Web 统一异常配置,数据库配置等),interceptor 包用来定义 spring mvc 中的拦截器等。 + +## xxx-service-api +业务逻辑模块,包前缀标准为:``cn.axzo.xxx.service.api.`` 里面定义的接口直接给 xxx-client 中的 controller 调用,所有的业务逻辑都会被封装到它的实现类当中。业务 Controller 只会调用这个 Service 并不关注具体的业务实现细节。可以定义 request 包用于 service 接口的请求参数,可以定义 response 用于 service 接口的响应参数。 + +## xxx-service +业务逻辑实现模块,是 xxx-service-api 模块定义的接口的实现类接口,包前缀标准为:``cn.axzo.xxx.service.impl.``.它是聚合 xxx-manager 这个通用处理层的,它主要是集成数据库访问以及第三方接口的调用。它的作用是业务逻辑的编排。 + +## xxx-manager-api +通用逻辑模块,包前缀标准为:``cn.axzo.xxx.manager.api.``,可以是访问数据库的一些逻辑的编排,也可以是定义一些公共的逻辑处理,比如:时间处理 Manager,分布式锁 Manager 等 + +## xxx-manager +能用逻辑实现模块:包前缀标准为:``cn.axzo.xxx.manager.impl.``。它对访问数据库的一些逻辑的编排达到操作的原子性,同时也实现一些公共逻辑处理的一些实现。 + +## xxx-test: +单元测试模块:所有模块的单元测试都可以放在这个模块里。 \ No newline at end of file diff --git a/archetype-standard/pom.xml b/archetype-standard/pom.xml new file mode 100644 index 0000000..6b95f89 --- /dev/null +++ b/archetype-standard/pom.xml @@ -0,0 +1,20 @@ + + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + + cn.axzo.archetype + archetype-standard + 1.0.0 + archetype-standard + jar + + diff --git a/archetype-standard/src/main/resources/META-INF/maven/archetype-metadata.xml b/archetype-standard/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 0000000..f551f9f --- /dev/null +++ b/archetype-standard/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,114 @@ + + + + + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.properties + **/*.yml + **/*.xml + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/main/java + + **/*.java + + + + + + + + src/test/java + + **/*.java + + + + src/test/resources + + **/*.xml + + + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml new file mode 100644 index 0000000..5fe34ce --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-client + + + + ${groupId} + ${rootArtifactId}-service + ${project.version} + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + guava + com.google.guava + + + + + org.apache.httpcomponents + httpclient + + + com.xuxueli + xxl-job-core + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java new file mode 100644 index 0000000..e23c35e --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src.main.java.client/ClientTest.java @@ -0,0 +1,9 @@ +package ${package}.client; + +/** + * @author zhaoyong + * + */ +public class ClientTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/bootstrap.yml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..e69de29 diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..7cff95c --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-client/src/main/resources/logback-spring.xml @@ -0,0 +1,201 @@ + + + + + + + + + + + + + ${PROJECT_NAME} + + + + ${LOG_PATH}/error.log + + ${LOG_PATH}/error/error-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + error + ACCEPT + DENY + + + + + + ${LOG_PATH}/warn.log + + ${LOG_PATH}/warn/warn-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + warn + ACCEPT + DENY + + + + + + ${LOG_PATH}/info.log + + ${LOG_PATH}/info/info-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + + info + ACCEPT + DENY + + + + + + + ${LOG_PATH}/debug.log + + ${LOG_PATH}/debug/debug-%d{yyyy-MM-dd}.%i.log + 10MB + ${MAX_HISTORY} + + + true + + + ${LOG_PATTERN} + utf-8 + + + + + debug + ACCEPT + DENY + + + + + + + ${LOG_PATTERN} + utf-8 + + + + info + + + + + + + + + @timestamp + yyyy-MM-dd'T'HH:mm:ss.SSSx + GMT+8 + + + + { + + "app": "${appName}", + "level": "%level", + + "thread": "%thread", + "class": "%logger{40}", + "message":"%message", + "m":"#asJson{%message}", + "stack_trace": "%exception{10}" + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/pom.xml new file mode 100644 index 0000000..8d686a7 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-common + + + + net.logstash.logback + logstash-logback-encoder + + + org.springframework.boot + spring-boot-starter-aop + + + javax.validation + validation-api + + + org.hibernate + hibernate-validator + + + org.redisson + redisson + + + com.alibaba + fastjson + + + joda-time + joda-time + + + org.apache.commons + commons-lang3 + + + org.apache.commons + commons-collections4 + + + cn.hutool + hutool-all + + + com.google.guava + guava + + + org.projectlombok + lombok + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/src/main/java/common/CommonTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/src/main/java/common/CommonTest.java new file mode 100644 index 0000000..fe76c62 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-common/src/main/java/common/CommonTest.java @@ -0,0 +1,9 @@ +package ${package}.common; + +/** + * @author zhaoyong + * + */ +public class CommonTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/pom.xml new file mode 100644 index 0000000..b6bc135 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-dal + + + + + ${groupId} + ${rootArtifactId}-common + ${project.version} + + + mysql + mysql-connector-java + + + com.zaxxer + HikariCP + + + org.springframework.boot + spring-boot-starter-jdbc + + + com.baomidou + mybatis-plus-boot-starter + + + org.apache.velocity + velocity-engine-core + compile + + + com.baomidou + mybatis-plus-generator + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/src/main/java/dal/DalTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/src/main/java/dal/DalTest.java new file mode 100644 index 0000000..ca00d86 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-dal/src/main/java/dal/DalTest.java @@ -0,0 +1,9 @@ +package ${package}.dal; + +/** + * @author zhaoyong + * + */ +public class DalTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/pom.xml new file mode 100644 index 0000000..4a7ac44 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-integration + + + + ${groupId} + ${rootArtifactId}-common + ${project.version} + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/src.main.java.integration/IntegrationTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/src.main.java.integration/IntegrationTest.java new file mode 100644 index 0000000..4db8c18 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-integration/src.main.java.integration/IntegrationTest.java @@ -0,0 +1,9 @@ +package ${package}.integration; + +/** + * @author zhaoyong + * + */ +public class IntegrationTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/pom.xml new file mode 100644 index 0000000..d0836dd --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-manager-api + + + + ${groupId} + ${rootArtifactId}-common + ${project.version} + + + ${groupId} + ${rootArtifactId}-dal + ${project.version} + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/src.main.java.manager.api/ManagerTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/src.main.java.manager.api/ManagerTest.java new file mode 100644 index 0000000..8d2c389 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager-api/src.main.java.manager.api/ManagerTest.java @@ -0,0 +1,9 @@ +package ${package}.manager.api; + +/** + * @author zhaoyong + * + */ +public class ManagerTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/pom.xml new file mode 100644 index 0000000..e67435a --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-manager + + + + ${groupId} + ${rootArtifactId}-dal + ${project.version} + + + ${groupId} + ${rootArtifactId}-manager-api + ${project.version} + + + ${groupId} + ${rootArtifactId}-integration + ${project.version} + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/src/main/java/manager/impl/ManagerTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/src/main/java/manager/impl/ManagerTest.java new file mode 100644 index 0000000..1faeb08 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-manager/src/main/java/manager/impl/ManagerTest.java @@ -0,0 +1,9 @@ +package ${package}.manager.impl; + +/** + * @author zhaoyong + * + */ +public class ManagerTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/pom.xml new file mode 100644 index 0000000..217ffb3 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-service-api + + + + ${groupId} + ${rootArtifactId}-common + ${project.version} + + + ${groupId} + ${rootArtifactId}-manager-api + ${project.version} + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/src.main.java.service.api/ServiceTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/src.main.java.service.api/ServiceTest.java new file mode 100644 index 0000000..021f3c4 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service-api/src.main.java.service.api/ServiceTest.java @@ -0,0 +1,9 @@ +package ${package}.service.api; + +/** + * @author zhaoyong + * + */ +public class ServiceTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/pom.xml new file mode 100644 index 0000000..2944ecf --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-service + + + + ${groupId} + ${rootArtifactId}-service-api + ${project.version} + + + ${groupId} + ${rootArtifactId}-manager + ${project.version} + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/src.main.java.service.impl/ServiceTest.java b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/src.main.java.service.impl/ServiceTest.java new file mode 100644 index 0000000..ae867db --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-service/src.main.java.service.impl/ServiceTest.java @@ -0,0 +1,9 @@ +package ${package}.service.impl; + +/** + * @author zhaoyong + * + */ +public class ServiceTest { + +} diff --git a/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml new file mode 100644 index 0000000..aef023f --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/__rootArtifactId__-test/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + ${groupId} + ${rootArtifactId} + ${version} + + ${rootArtifactId}-test + + + + ${groupId} + ${rootArtifactId}-client + ${project.version} + + + org.springframework.boot + spring-boot-starter-test + + + + diff --git a/archetype-standard/src/main/resources/archetype-resources/pom.xml b/archetype-standard/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 0000000..eec80d7 --- /dev/null +++ b/archetype-standard/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,128 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + + ${groupId} + ${rootArtifactId} + ${version} + pom + + + 4.4 + 3.5 + 5.5.7 + 6.1 + 2.2.0 + 3.13.2 + 1.2.47 + 2.10.6 + 2.0.0.Final + 6.0.16.Final + 4.5.6 + 3.3.2 + 2.0 + 2.2.1.RELEASE + Hoxton.SR6 + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + com.baomidou + mybatis-plus-generator + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + + joda-time + joda-time + ${joda-time.version} + + + net.logstash.logback + logstash-logback-encoder + ${logstash-logback-encoder.version} + + + javax.validation + validation-api + ${javax.validation.version} + + + org.hibernate + hibernate-validator + ${hibernate.validator.version} + + + org.apache.httpcomponents + httpclient + ${http.client.version} + + + cn.hutool + hutool-all + ${hutool-all.version} + + + org.apache.velocity + velocity-engine-core + ${velocity-engine-core.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + org.redisson + redisson + ${redisson.version} + + + com.xuxueli + xxl-job-core + ${xxl.job.version} + + + + + diff --git a/common-common/.gitignore b/common-common/.gitignore new file mode 100644 index 0000000..33dedbb --- /dev/null +++ b/common-common/.gitignore @@ -0,0 +1,33 @@ +# maven ignore +target/ +*.jar +*.war +*.zip +*.tar +*.tar.gz + +# eclipse ignore +.settings/ +.project +.classpath + +# idea ignore +.idea/ +*.ipr +*.iml +*.iws + +# temp ignore +logs/ +*.doc +*.log +*.cache +*.diff +*.patch +*.tmp + +# system ignore +.DS_Store +Thumbs.db + +*.class \ No newline at end of file diff --git a/common-common/pom.xml b/common-common/pom.xml new file mode 100644 index 0000000..00cae20 --- /dev/null +++ b/common-common/pom.xml @@ -0,0 +1,33 @@ + + + + 4.0.0 + + + cn.axzo.framework + axzo-framework + ${revision} + + + common-common + + + + org.slf4j + slf4j-api + provided + + + org.springframework + spring-context + provided + + + org.projectlombok + lombok + provided + + + + diff --git a/common-common/src/main/java/cn/azxo/framework/common/spring/AbstractSimpleRegisterableServiceFactory.java b/common-common/src/main/java/cn/azxo/framework/common/spring/AbstractSimpleRegisterableServiceFactory.java new file mode 100644 index 0000000..b61c921 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/spring/AbstractSimpleRegisterableServiceFactory.java @@ -0,0 +1,93 @@ +package cn.azxo.framework.common.spring; + +import lombok.extern.slf4j.Slf4j; +import org.slf4j.MDC; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.util.CollectionUtils; + +import java.util.HashMap; +import java.util.Map; + +/** + * 抽象简单可自动注册服务工厂,自动发现服务 + * + * @author zhaoyong_sh + * @since 2020-07-07 + */ +@Slf4j +public abstract class AbstractSimpleRegisterableServiceFactory> + implements ApplicationListener { + + protected Map serviceMap = new HashMap<>(); + + /** + * 返回服务工厂名称 + */ + protected abstract String getFactoryName(); + + /** + * 返回自动发现服务的接口类型 + */ + protected abstract Class getServiceClazz(); + + /** + * 返回服务 + * @param routeKey 路由对象 + * @return 如果无匹配服务返回null + */ + public Service getService(RouteKey routeKey) { + return serviceMap.get(routeKey); + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + try { + Map services = event.getApplicationContext().getBeansOfType(getServiceClazz()); + if (CollectionUtils.isEmpty(services)) { + return; + } + beforeRegisterAll(); + for (Map.Entry entry : services.entrySet()) { + registerService(entry.getKey(), entry.getValue()); + } + afterRegisterAll(); + } finally { + MDC.clear(); + } + } + + /** + * 在所有注册之前 + */ + protected void beforeRegisterAll() { + log.info("开始初始化简单服务工厂: {}", getFactoryName()); + } + + /** + * 默认注册服务实现,Map管理服务,key是路由对象,value是服务 + * @param beanName bean名称 + * @param service 服务实例 + */ + protected void registerService(String beanName, Service service) { + RouteKey routeKey = service.getRouteKey(); + if (routeKey == null) { + throw new RuntimeException(String.format("注册服务路由对象不可为空:%s", service.getClass())); + } + if (!serviceMap.containsKey(routeKey)) { + serviceMap.put(routeKey, service); + log.info("已注册服务: {}, {}", routeKey, service.getClass()); + } else { + throw new RuntimeException(String.format("注册服务路由对象重复: %s, %s, %s", routeKey, + serviceMap.get(routeKey).getClass(), service.getClass())); + } + } + + /** + * 在所有注册之后 + */ + protected void afterRegisterAll() { + } + +} + diff --git a/common-common/src/main/java/cn/azxo/framework/common/spring/SimpleRegisterableService.java b/common-common/src/main/java/cn/azxo/framework/common/spring/SimpleRegisterableService.java new file mode 100644 index 0000000..0b3f70e --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/spring/SimpleRegisterableService.java @@ -0,0 +1,18 @@ +package cn.azxo.framework.common.spring; + +/** + * 简单可自动注册服务,根据路由对象直接路由 + * + * @author zhaoyong_sh + * @since 2020-07-07 + */ +public interface SimpleRegisterableService { + + /** + * 返回用于路由服务的对象 + * + * @return RouteKey + */ + RouteKey getRouteKey(); + +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a1012e0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,70 @@ + + + + 4.0.0 + + cn.axzo.framework + axzo-framework + ${revision} + pom + + + archetype-simple + archetype-standard + common-common + smart-datasource + + + + UTF-8 + 1.8 + 1.8 + 1.0.0 + + 1.7.3 + 1.0 + + + + + + + org.aspectj + aspectjrt + ${aspectj.version} + + + org.aspectj + aspectjweaver + ${aspectj.version} + + + aopalliance + aopalliance + ${aopalliance.version} + + + org.slf4j + slf4j-api + 1.7.5 + + + org.springframework + spring-jdbc + 4.3.19.RELEASE + + + org.springframework + spring-context + 4.3.19.RELEASE + + + org.projectlombok + lombok + 1.18.18 + + + + + \ No newline at end of file diff --git a/smart-datasource/pom.xml b/smart-datasource/pom.xml new file mode 100644 index 0000000..25caa66 --- /dev/null +++ b/smart-datasource/pom.xml @@ -0,0 +1,43 @@ + + + + 4.0.0 + + + cn.axzo.framework + axzo-framework + ${revision} + + + smart-datasource + + + + org.aspectj + aspectjrt + provided + + + org.aspectj + aspectjweaver + provided + + + aopalliance + aopalliance + provided + + + org.springframework + spring-context + provided + + + org.springframework + spring-jdbc + provided + + + + diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSource.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSource.java new file mode 100644 index 0000000..69a5a67 --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSource.java @@ -0,0 +1,23 @@ +package cn.azxo.framework.datasource; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 分片自定义数据源 + * + * @author zhaoyong_sh + * @see DataSource + * @since 2020-03-27 13:49 + */ +@Documented +@Target({ElementType.METHOD,ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface DataSource { + + String value() default ""; + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSourcePointcut.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSourcePointcut.java new file mode 100644 index 0000000..02ef22d --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/DataSourcePointcut.java @@ -0,0 +1,26 @@ +package cn.azxo.framework.datasource; + +import org.springframework.aop.support.StaticMethodMatcherPointcut; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; + +/** + * 数据源 Pointcut + * + * @author zhaoyong_sh + * @see DataSourcePointcut + * @since 2020-03-27 15:57 + */ +public class DataSourcePointcut extends StaticMethodMatcherPointcut { + + @Override + public boolean matches(Method method, Class aClass) { + return matchesInternal(method) || matchesInternal(aClass); + } + + private boolean matchesInternal(AnnotatedElement annotatedElement) { + return annotatedElement.getAnnotation(DataSource.class) != null; + } + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/DatasourceContextHolder.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/DatasourceContextHolder.java new file mode 100644 index 0000000..07c3163 --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/DatasourceContextHolder.java @@ -0,0 +1,26 @@ +package cn.azxo.framework.datasource; + +/** + * 数据源上下文 Holder + * + * @author zhaoyong_sh + * @see DatasourceContextHolder + * @since 2020-03-27 13:24 + */ +public class DatasourceContextHolder { + + public static final ThreadLocal contextHolder = new ThreadLocal<>(); + + public static void setDataSourceKey(String dataSourceKey) { + contextHolder.set(dataSourceKey); + } + + public static String getDataSourceKey() { + return contextHolder.get(); + } + + public static void clearDataSourceKey() { + contextHolder.remove(); + } + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/EnableDataSource.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/EnableDataSource.java new file mode 100644 index 0000000..d80a138 --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/EnableDataSource.java @@ -0,0 +1,24 @@ +package cn.azxo.framework.datasource; + +import org.springframework.context.annotation.Import; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 激活动态数据源 + * + * @author zhaoyong_sh + * @see EnableDataSource + * @since 2020-03-27 14:18 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(ShardingConfigurationSelector.class) +public @interface EnableDataSource { + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/ProxyShardingConfiguration.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/ProxyShardingConfiguration.java new file mode 100644 index 0000000..700ce37 --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/ProxyShardingConfiguration.java @@ -0,0 +1,41 @@ +package cn.azxo.framework.datasource; + +import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Role; + +/** + * 分片配置类 + * + * @author zhaoyong_sh + * @see ProxyShardingConfiguration + * @since 2020-03-27 14:52 + */ +@Configuration +public class ProxyShardingConfiguration { + + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + @Bean + public DefaultBeanFactoryPointcutAdvisor shardingAdvisor() { + DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor(); + advisor.setPointcut(dataSourcePointcut()); + advisor.setAdvice(shardingInterceptor()); + return advisor; + } + + @Bean + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + public DataSourcePointcut dataSourcePointcut(){ + return new DataSourcePointcut(); + } + + @Bean + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + public ShardingInterceptor shardingInterceptor() { + ShardingInterceptor interceptor = new ShardingInterceptor(); + return interceptor; + } + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingConfigurationSelector.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingConfigurationSelector.java new file mode 100644 index 0000000..77a8fc1 --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingConfigurationSelector.java @@ -0,0 +1,20 @@ +package cn.azxo.framework.datasource; + +import org.springframework.context.annotation.ImportSelector; +import org.springframework.core.type.AnnotationMetadata; + +/** + * 分片配置 Selector + * + * @author zhaoyong_sh + * @see ShardingConfigurationSelector + * @since 2020-03-27 14:22 + */ +public class ShardingConfigurationSelector implements ImportSelector { + + @Override + public String[] selectImports(AnnotationMetadata importingClassMetadata) { + return new String[] {ProxyShardingConfiguration.class.getName()}; + } + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingInterceptor.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingInterceptor.java new file mode 100644 index 0000000..6c6d09d --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/ShardingInterceptor.java @@ -0,0 +1,129 @@ +package cn.azxo.framework.datasource; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.springframework.core.BridgeMethodResolver; +import org.springframework.core.MethodClassKey; +import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 数据源切换切面 + * + * @author zhaoyong_sh + * @see ShardingInterceptor + * @since 2020-03-27 15:42 + */ +public class ShardingInterceptor implements MethodInterceptor { + + private final static String NULL_DATASOURCE_ATTRIBUTE = "null"; + + private final Map attributeCache = new ConcurrentHashMap<>(1024); + + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + Method method = invocation.getMethod(); + Class declaringClass = invocation.getMethod().getDeclaringClass(); + String dataSourceAttribute = getDataSourceAttribute(method, declaringClass); + if(StringUtils.hasText(dataSourceAttribute)){ + DatasourceContextHolder.setDataSourceKey(dataSourceAttribute); + } + Object result; + try { + result = invocation.proceed(); + } finally { + DatasourceContextHolder.clearDataSourceKey(); + } + return result; + } + + public String getDataSourceAttribute(Method method, Class targetClass) { + if (method.getDeclaringClass() == Object.class) { + return null; + } + + // First, see if we have a cached value. + Object cacheKey = getCacheKey(method, targetClass); + String cached = this.attributeCache.get(cacheKey); + if (cached != null) { + // Value will either be canonical value indicating there is no transaction attribute, + // or an actual transaction attribute. + if (cached == NULL_DATASOURCE_ATTRIBUTE) { + return null; + } + else { + return cached; + } + } else { + // We need to work it out. + String txAttr = computeDataSourceAttribute(method, targetClass); + // Put it in the cache. + if (StringUtils.hasText(txAttr)) { + this.attributeCache.put(cacheKey, txAttr); + } else { + this.attributeCache.put(cacheKey, NULL_DATASOURCE_ATTRIBUTE); + } + return txAttr; + } + } + + /** + * Determine a cache key for the given method and target class. + *

Must not produce same key for overloaded methods. + * Must produce same key for different instances of the same method. + * @param method the method (never {@code null}) + * @param targetClass the target class (may be {@code null}) + * @return the cache key (never {@code null}) + */ + protected Object getCacheKey(Method method, Class targetClass) { + return new MethodClassKey(method, targetClass); + } + + private String computeDataSourceAttribute(Method method, Class targetClass){ + // Ignore CGLIB subclasses - introspect the actual user class. + Class userClass = ClassUtils.getUserClass(targetClass); + // The method may be on an interface, but we need attributes from the target class. + // If the target class is null, the method will be unchanged. + Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass); + // If we are dealing with method with generic parameters, find the original method. + specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod); + // First try is the method in the target class. + String txAttr = findDataSourceAttribute(specificMethod); + if (txAttr != null) { + return txAttr; + } + // Second try is the transaction attribute on the target class. + txAttr = findDataSourceAttribute(specificMethod.getDeclaringClass()); + if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { + return txAttr; + } + if (specificMethod != method) { + // Fallback is to look at the original method. + txAttr = findDataSourceAttribute(method); + if (txAttr != null) { + return txAttr; + } + // Last fallback is the class of the original method. + txAttr = findDataSourceAttribute(method.getDeclaringClass()); + if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { + return txAttr; + } + } + + return null; + } + + private String findDataSourceAttribute(AnnotatedElement annotatedElement){ + DataSource dataSourceAnnotation = annotatedElement.getAnnotation(DataSource.class); + if(dataSourceAnnotation != null) { + return dataSourceAnnotation.value(); + } + return null; + } + +} diff --git a/smart-datasource/src/main/java/cn/azxo/framework/datasource/SmartDatasource.java b/smart-datasource/src/main/java/cn/azxo/framework/datasource/SmartDatasource.java new file mode 100644 index 0000000..5c58fdb --- /dev/null +++ b/smart-datasource/src/main/java/cn/azxo/framework/datasource/SmartDatasource.java @@ -0,0 +1,25 @@ +package cn.azxo.framework.datasource; + +import org.springframework.core.Ordered; +import org.springframework.core.PriorityOrdered; +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; + +/** + * 智能分片数据源 + * + * @author zhaoyong_sh + * @see SmartDatasource + * @since 2020-03-27 11:30 + */ +public class SmartDatasource extends AbstractRoutingDataSource implements PriorityOrdered { + + @Override + protected Object determineCurrentLookupKey() { + return DatasourceContextHolder.getDataSourceKey(); + } + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE; + } +}