From 3d16e4e68bad8f97014e0314c7adfcdbcba8c449 Mon Sep 17 00:00:00 2001 From: ljh01459796 Date: Fri, 23 Jun 2023 18:25:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20spring-boot-dataso?= =?UTF-8?q?urce=20starter.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- orion-ops-dependencies/pom.xml | 28 ++++++++- .../banner/core/BannerApplicationRunner.java | 3 +- .../pom.xml | 57 +++++++++++++++++++ .../OrionDatasourceAutoConfiguration.java | 54 ++++++++++++++++++ .../core/filter/DruidAdRemoveFilter.java | 40 +++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../web/config/OrionWebAutoConfiguration.java | 12 ++-- .../core/handler/GlobalExceptionHandler.java | 3 +- orion-ops-framework/pom.xml | 1 + orion-ops-server/pom.xml | 4 ++ .../src/main/resources/application-dev.yaml | 10 ++++ .../src/main/resources/application-prod.yaml | 28 +++++++++ .../src/main/resources/application.yaml | 35 +++++++++++- 13 files changed, 265 insertions(+), 11 deletions(-) create mode 100644 orion-ops-framework/orion-ops-spring-boot-starter-datasource/pom.xml create mode 100644 orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/config/OrionDatasourceAutoConfiguration.java create mode 100644 orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/core/filter/DruidAdRemoveFilter.java create mode 100644 orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/orion-ops-dependencies/pom.xml b/orion-ops-dependencies/pom.xml index cf796e86..8e7baf9c 100644 --- a/orion-ops-dependencies/pom.xml +++ b/orion-ops-dependencies/pom.xml @@ -21,7 +21,9 @@ 1.6.15 4.1.0 1.5.5.Final - + 3.5.3.1 + 3.5.3.1 + 1.2.16 @@ -68,6 +70,11 @@ orion-ops-spring-boot-starter-swagger ${revision} + + com.orion.ops + orion-ops-spring-boot-starter-datasource + ${revision} + @@ -109,6 +116,25 @@ ${knife4j.version} + + + com.alibaba + druid-spring-boot-starter + ${druid.version} + + + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-generator + ${mybatis-plus-generator.version} + + diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-banner/src/main/java/com/orion/ops/framework/banner/core/BannerApplicationRunner.java b/orion-ops-framework/orion-ops-spring-boot-starter-banner/src/main/java/com/orion/ops/framework/banner/core/BannerApplicationRunner.java index 401748be..8f39d8a9 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-banner/src/main/java/com/orion/ops/framework/banner/core/BannerApplicationRunner.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-banner/src/main/java/com/orion/ops/framework/banner/core/BannerApplicationRunner.java @@ -32,8 +32,9 @@ public class BannerApplicationRunner implements ApplicationRunner { public void run(ApplicationArguments args) { String line = AnsiCode.GLOSS_GREEN.stain(":: orion-ops-server v" + version + " 服务已启动(" + env + ") ::\n") + AnsiCode.GLOSS_GREEN.stain(":: swagger 文档 ") + - // TODO swagger 地址 AnsiCode.GLOSS_BLUE.stain("http://127.0.0.1:" + port + "/doc.html\n") + + AnsiCode.GLOSS_GREEN.stain(":: druid console ") + + AnsiCode.GLOSS_BLUE.stain("http://127.0.0.1:" + port + "/druid/index.html\n") + AnsiCode.GLOSS_GREEN.stain(":: server 心跳检测 ") + AnsiCode.GLOSS_BLUE + "curl -X GET --location \"http://127.0.0.1:" + port + apiPrefix + "/server/bootstrap/health\"" + diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-datasource/pom.xml b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/pom.xml new file mode 100644 index 00000000..32ad82af --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/pom.xml @@ -0,0 +1,57 @@ + + + + com.orion.ops + orion-ops-framework + ${revision} + + + 4.0.0 + orion-ops-spring-boot-starter-datasource + ${project.artifactId} + jar + + 项目数据源配置包 + https://github.com/lijiahangmax/orion-ops-pro + + + + com.orion.ops + orion-ops-common + + + + org.springframework.boot + spring-boot-starter + + + + + com.mysql + mysql-connector-j + + + + + com.alibaba + druid-spring-boot-starter + + + + + com.baomidou + mybatis-plus-boot-starter + provided + + + + + org.springframework.boot + spring-boot-starter-web + provided + + + + \ No newline at end of file diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/config/OrionDatasourceAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/config/OrionDatasourceAutoConfiguration.java new file mode 100644 index 00000000..8a1d3cc5 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/config/OrionDatasourceAutoConfiguration.java @@ -0,0 +1,54 @@ +package com.orion.ops.framework.datasource.config; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; +import com.orion.ops.framework.datasource.core.filter.DruidAdRemoveFilter; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; + +/** + * 数据源配置类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 17:22 + */ +@AutoConfiguration +@EnableTransactionManagement(proxyTargetClass = true) +@EnableConfigurationProperties(DruidStatProperties.class) +public class OrionDatasourceAutoConfiguration { + + /** + * @return druid 数据源 + */ + @Bean + public DataSource druidDataSource() { + return new DruidDataSource(); + } + + /** + * @param properties 配置 + * @return druid 广告过滤器 + */ + @Bean + @ConditionalOnProperty(name = "spring.datasource.druid.web-stat-filter.enabled", havingValue = "true") + public FilterRegistrationBean druidAdRemoveFilterFilter(DruidStatProperties properties) { + // 获取 druid web 监控页面的参数 + DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); + // 提取 common.js 的配置路径 + String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"; + String commonJsPattern = pattern.replaceAll("\\*", "js/common.js"); + // 创建 DruidAdRemoveFilter Bean + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + registrationBean.setFilter(new DruidAdRemoveFilter()); + registrationBean.addUrlPatterns(commonJsPattern); + return registrationBean; + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/core/filter/DruidAdRemoveFilter.java b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/core/filter/DruidAdRemoveFilter.java new file mode 100644 index 00000000..530129ac --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/java/com/orion/ops/framework/datasource/core/filter/DruidAdRemoveFilter.java @@ -0,0 +1,40 @@ +package com.orion.ops.framework.datasource.core.filter; + +import com.alibaba.druid.util.Utils; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * druid 广告过滤器 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 17:23 + */ +public class DruidAdRemoveFilter extends OncePerRequestFilter { + + /** + * common.js 的路径 + */ + private static final String COMMON_JS_ILE_PATH = "support/http/resources/js/common.js"; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + chain.doFilter(request, response); + // 重置缓冲区 响应头不会被重置 + response.resetBuffer(); + // 获取 common.js + String text = Utils.readFromResource(COMMON_JS_ILE_PATH); + // 正则替换 banner 除去底部的广告信息 + text = text.replaceAll("
", ""); + text = text.replaceAll("powered.*?shrek.wang", ""); + response.getWriter().write(text); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..7611e1ca --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-datasource/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.orion.ops.framework.datasource.config.OrionDatasourceAutoConfiguration \ No newline at end of file diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java index ff9ce2c6..54ed66c2 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java @@ -69,7 +69,7 @@ public class OrionWebAutoConfiguration implements WebMvcConfigurer { } /** - * @return http json 转换器 + * @return http message 转换器 */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { @@ -97,15 +97,15 @@ public class OrionWebAutoConfiguration implements WebMvcConfigurer { jsonConverter.setFastJsonConfig(config); // 先获取默认转换器 List> defaultConverters = new HttpMessageConverters().getConverters(); - List> newConverters = new ArrayList<>(); + List> converters = new ArrayList<>(); // 将 byte converter 添加至首位 - fix swagger api 返回base64报错 - newConverters.add(new ByteArrayHttpMessageConverter()); + converters.add(new ByteArrayHttpMessageConverter()); // 添加自定义 converter - using WrapperResultHandler - newConverters.add(jsonConverter); + converters.add(jsonConverter); // 添加默认 converter - newConverters.addAll(defaultConverters); + converters.addAll(defaultConverters); // 设置不添加默认 converter - return new HttpMessageConverters(false, newConverters); + return new HttpMessageConverters(false, converters); } /** diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java index 48d02438..c0b5399b 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java @@ -47,8 +47,7 @@ public class GlobalExceptionHandler { return HttpWrapper.error(ex.getMessage()); } - // TODO datasource starter - // @ExceptionHandler(value = DataAccessResourceFailureException.class) + @ExceptionHandler(value = DataAccessResourceFailureException.class) public HttpWrapper dataAccessResourceFailureExceptionHandler(HttpServletRequest request, Exception ex) { log.error("dataAccessResourceFailureExceptionHandler url: {}, 抛出异常: {}, message: {}", request.getRequestURI(), ex.getClass(), ex.getMessage(), ex); return HttpWrapper.error(ExceptionMessageConst.NETWORK_FLUCTUATION); diff --git a/orion-ops-framework/pom.xml b/orion-ops-framework/pom.xml index a6bf599f..80c2fb62 100644 --- a/orion-ops-framework/pom.xml +++ b/orion-ops-framework/pom.xml @@ -20,6 +20,7 @@ orion-ops-spring-boot-starter-web orion-ops-spring-boot-starter-banner orion-ops-spring-boot-starter-swagger + orion-ops-spring-boot-starter-datasource \ No newline at end of file diff --git a/orion-ops-server/pom.xml b/orion-ops-server/pom.xml index c22314a0..1c3ae837 100644 --- a/orion-ops-server/pom.xml +++ b/orion-ops-server/pom.xml @@ -36,6 +36,10 @@ com.orion.ops orion-ops-spring-boot-starter-swagger + + com.orion.ops + orion-ops-spring-boot-starter-datasource + org.springframework.boot diff --git a/orion-ops-server/src/main/resources/application-dev.yaml b/orion-ops-server/src/main/resources/application-dev.yaml index e69de29b..13f001f3 100644 --- a/orion-ops-server/src/main/resources/application-dev.yaml +++ b/orion-ops-server/src/main/resources/application-dev.yaml @@ -0,0 +1,10 @@ +spring: + datasource: + druid: + url: + username: + password: + initial-size: 0 + min-idle: 1 + max-active: 5 + diff --git a/orion-ops-server/src/main/resources/application-prod.yaml b/orion-ops-server/src/main/resources/application-prod.yaml index e69de29b..4cfba9cc 100644 --- a/orion-ops-server/src/main/resources/application-prod.yaml +++ b/orion-ops-server/src/main/resources/application-prod.yaml @@ -0,0 +1,28 @@ +spring: + datasource: + druid: + url: + username: + password: + # 初始连接数 + initial-size: 5 + # 最小连接池数量 + min-idle: 5 + # 最大连接池数量 + max-active: 20 + web-stat-filter: + enabled: true + stat-view-servlet: + enabled: true + filter: + stat: + enabled: true + +springdoc: + api-docs: + enabled: false + swagger-ui: + enabled: false + +knife4j: + enable: false diff --git a/orion-ops-server/src/main/resources/application.yaml b/orion-ops-server/src/main/resources/application.yaml index 3288fad9..853cf5e3 100644 --- a/orion-ops-server/src/main/resources/application.yaml +++ b/orion-ops-server/src/main/resources/application.yaml @@ -9,7 +9,6 @@ spring: main: # 允许循环依赖 allow-circular-references: true - # Servlet 配置 servlet: # 文件上传相关配置项 multipart: @@ -20,6 +19,40 @@ spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER + datasource: + druid: + driver-class-name: com.mysql.cj.jdbc.Driver + # 初始连接数 + initial-size: 5 + # 最小连接池数量 + min-idle: 5 + # 最大连接池数量 + max-active: 20 + # 配置获取连接等待超时的时间 + max-wait: 600000 + # 检测间隔 + time-between-eviction-runs-millis: 60000 + # 最小生存的时间 + min-evictable-idle-time-millis: 300000 + # 最大生存的时间 + max-evictable-idle-time-millis: 900000 + validation-query: SELECT 1 + web-stat-filter: + enabled: true + stat-view-servlet: + enabled: true + url-pattern: /druid/* + login-username: + login-password: + filter: + stat: + enabled: true + log-slow-sql: true + slow-sql-millis: 800 + merge-sql: true + wall: + config: + multi-statement-allow: true springdoc: api-docs: