feat: 增加sqlserver数据源

This commit is contained in:
Kelvin 2024-11-27 17:07:23 +08:00
parent bc00c4380c
commit d9992854e6
8 changed files with 42 additions and 54 deletions

View File

@ -103,32 +103,6 @@
</dependency> </dependency>
<!-- 达梦数据库驱动依赖 --> <!-- 达梦数据库驱动依赖 -->
<!-- <dependency>-->
<!-- <groupId>com.dameng</groupId>-->
<!-- <artifactId>DmJdbcDriver18</artifactId>-->
<!-- <version>8.1.2.79</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dameng</groupId>-->
<!-- <artifactId>DmDialect-for-hibernate5.4</artifactId>-->
<!-- <version>8.1.3.140</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dm</groupId>-->
<!-- <artifactId>DmJdbcDriver</artifactId>-->
<!-- <version>1.8</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${basedir}/lib/DmJdbcDriver18.jar</systemPath>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dm.dialect</groupId>-->
<!-- <artifactId>hibernate5</artifactId>-->
<!-- <version>5.4</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${basedir}/lib/DmDialect-for-hibernate5.4.jar</systemPath>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>com.dm</groupId> <groupId>com.dm</groupId>
<artifactId>DmJdbcDriver</artifactId> <artifactId>DmJdbcDriver</artifactId>
@ -139,7 +113,12 @@
<artifactId>hibernate5</artifactId> <artifactId>hibernate5</artifactId>
<version>5.4</version> <version>5.4</version>
</dependency> </dependency>
<!--sqlServer-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre8</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -2,22 +2,23 @@
spring: spring:
datasource: datasource:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
driverClassName: dm.jdbc.driver.DmDriver
druid: druid:
# 主库数据源 # 主库数据源
master: dm:
driverClassName: dm.jdbc.driver.DmDriver
# url: jdbc:dm://30.30.2.24:5237/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # url: jdbc:dm://30.30.2.24:5237/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
# url: jdbc:dm://localhost:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # url: jdbc:dm://localhost:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
url: jdbc:dm://192.168.8.60:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 url: jdbc:dm://192.168.8.60:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: SYSDBA username: SYSDBA
password: SYSDBA password: SYSDBA
# 从库数据源 # 从库数据源
slave: sqlserver:
# 从数据源开关/默认关闭 # 从数据源开关/默认关闭
enabled: false enabled: true
url: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: url: jdbc:sqlserver://192.168.8.60:5236:1433;DatabaseName=DPG_RH_YQ_20240711
password: username: sa
password: rh123456
# 初始连接数 # 初始连接数
initialSize: 5 initialSize: 5
# 最小连接池数量 # 最小连接池数量

View File

@ -63,15 +63,13 @@ spring:
# redis 配置 # redis 配置
redis: redis:
# 地址 # 地址
# host: 192.168.8.60 host: 192.168.8.60
host: 127.0.0.1
# 端口默认为6379 # 端口默认为6379
port: 6379 port: 6379
# 数据库索引 # 数据库索引
database: 4 database: 4
# 密码 # 密码
# password: 123456 password: 123456
password:
# 连接超时时间 # 连接超时时间
timeout: 10s timeout: 10s
lettuce: lettuce:

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<!-- 日志存放路径 --> <!-- 日志存放路径 -->
<property name="log.path" value="/home/ktg/logs" /> <property name="log.path" value="./logs" />
<!-- 日志输出格式 --> <!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" /> <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />

View File

@ -24,5 +24,5 @@ public @interface DataSource
/** /**
* 切换数据源名称 * 切换数据源名称
*/ */
public DataSourceType value() default DataSourceType.MASTER; public DataSourceType value() default DataSourceType.DM;
} }

View File

@ -10,10 +10,10 @@ public enum DataSourceType
/** /**
* 主库 * 主库
*/ */
MASTER, DM,
/** /**
* 从库 * 从库
*/ */
SLAVE SQLSERVER,
} }

View File

@ -9,6 +9,8 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
@ -33,17 +35,17 @@ import com.ktg.framework.datasource.DynamicDataSource;
public class DruidConfig public class DruidConfig
{ {
@Bean @Bean
@ConfigurationProperties("spring.datasource.druid.master") @ConfigurationProperties("spring.datasource.druid.dm")
public DataSource masterDataSource(DruidProperties druidProperties) public DataSource dmDataSource(DruidProperties druidProperties)
{ {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource); return druidProperties.dataSource(dataSource);
} }
@Bean @Bean
@ConfigurationProperties("spring.datasource.druid.slave") @ConfigurationProperties("spring.datasource.druid.sqlserver")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") @ConditionalOnProperty(prefix = "spring.datasource.druid.sqlserver", name = "enabled", havingValue = "true")
public DataSource slaveDataSource(DruidProperties druidProperties) public DataSource sqlServerDataSource(DruidProperties druidProperties)
{ {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource); return druidProperties.dataSource(dataSource);
@ -51,12 +53,20 @@ public class DruidConfig
@Bean(name = "dynamicDataSource") @Bean(name = "dynamicDataSource")
@Primary @Primary
public DynamicDataSource dataSource(DataSource masterDataSource) public DynamicDataSource dataSource(@Qualifier("dmDataSource") DataSource dmDataSource) {
{
Map<Object, Object> targetDataSources = new HashMap<>(); Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); targetDataSources.put(DataSourceType.DM.name(), dmDataSource);
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources); // 尝试获取 slaveDataSource如果存在则添加到数据源中
try {
DataSource sqlServerDataSource = SpringUtils.getBean("sqlServerDataSource");
targetDataSources.put(DataSourceType.SQLSERVER.name(), sqlServerDataSource);
} catch (Exception e) {
// 这里可以记录日志表示未启用 SQLServer 数据源
System.out.println("SQLServer DataSource is not enabled or configured.");
}
return new DynamicDataSource(dmDataSource, targetDataSources);
} }
/** /**

View File

@ -47,7 +47,7 @@ public class SysConfigServiceImpl implements ISysConfigService
* @return 参数配置信息 * @return 参数配置信息
*/ */
@Override @Override
@DataSource(DataSourceType.MASTER) @DataSource(DataSourceType.DM)
public SysConfig selectConfigById(Long configId) public SysConfig selectConfigById(Long configId)
{ {
SysConfig config = new SysConfig(); SysConfig config = new SysConfig();