feat: 添加 spring-boot-datasource starter.

This commit is contained in:
ljh01459796
2023-06-23 18:25:49 +08:00
parent 2cf36da82d
commit 3d16e4e68b
13 changed files with 265 additions and 11 deletions

View File

@@ -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\"" +

View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.orion.ops</groupId>
<artifactId>orion-ops-framework</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>orion-ops-spring-boot-starter-datasource</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<description>项目数据源配置包</description>
<url>https://github.com/lijiahangmax/orion-ops-pro</url>
<dependencies>
<dependency>
<groupId>com.orion.ops</groupId>
<artifactId>orion-ops-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -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<DruidAdRemoveFilter> 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<DruidAdRemoveFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new DruidAdRemoveFilter());
registrationBean.addUrlPatterns(commonJsPattern);
return registrationBean;
}
}

View File

@@ -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("<a.*?banner\"></a><br/>", "");
text = text.replaceAll("powered.*?shrek.wang</a>", "");
response.getWriter().write(text);
}
}

View File

@@ -0,0 +1 @@
com.orion.ops.framework.datasource.config.OrionDatasourceAutoConfiguration

View File

@@ -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<HttpMessageConverter<?>> defaultConverters = new HttpMessageConverters().getConverters();
List<HttpMessageConverter<?>> newConverters = new ArrayList<>();
List<HttpMessageConverter<?>> 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);
}
/**

View File

@@ -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);

View File

@@ -20,6 +20,7 @@
<module>orion-ops-spring-boot-starter-web</module>
<module>orion-ops-spring-boot-starter-banner</module>
<module>orion-ops-spring-boot-starter-swagger</module>
<module>orion-ops-spring-boot-starter-datasource</module>
</modules>
</project>