整合多数据源(待完善)
This commit is contained in:
+34
-9
@@ -33,7 +33,17 @@
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<!-- 数据库 -->
|
||||
<dependency>
|
||||
<dependency><!-- druid -->
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>${druid-version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- mysql driver -->
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency><!-- mysql driver -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
<exclusions>
|
||||
@@ -43,15 +53,15 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency> <!-- druid连接池 -->
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>${druid-version}</version>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
<dependency> <!-- mysql driver -->
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<dependency> <!-- 分页插件 -->
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
<!-- lombok:实体类自动生成getting和setting方法,以及equals,hashCode,toString等 -->
|
||||
<dependency>
|
||||
@@ -129,4 +139,19 @@
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- mybatis generator 自动生成代码插件 -->
|
||||
<plugin>
|
||||
<groupId>org.mybatis.generator</groupId>
|
||||
<artifactId>mybatis-generator-maven-plugin</artifactId>
|
||||
<version>1.3.2</version>
|
||||
<configuration>
|
||||
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
|
||||
<overwrite>true</overwrite>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.xwbing.datasource;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2018/5/31 21:38
|
||||
* 作者: xiangwb
|
||||
* 说明:
|
||||
*/
|
||||
@Configuration
|
||||
@PropertySource("classpath:druid.properties")
|
||||
public class JpaDataSourceConfig {
|
||||
@Bean(name="")
|
||||
@Primary
|
||||
@ConfigurationProperties("db1")
|
||||
public DataSource primaryDataSource() {
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.xwbing.datasource;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 说明: 德鲁伊数据源配置。监控地址:datasource/index.html
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/12/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan(basePackages = "", sqlSessionFactoryRef = "mybatisSqlSessionFactory")
|
||||
@PropertySource("classpath:druid.properties")
|
||||
public class MybatisDataSourceConfig {
|
||||
private final Logger logger = LoggerFactory.getLogger(MybatisDataSourceConfig.class);
|
||||
|
||||
@Bean(name = "mybatisDatasource")
|
||||
@ConfigurationProperties("db2")
|
||||
public DataSource dataSource() {
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Bean(name = "mybatisSqlSessionFactory")
|
||||
public SqlSessionFactory sqlSessionFactory() throws Exception {
|
||||
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
|
||||
sqlSessionFactoryBean.setDataSource(dataSource());
|
||||
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
|
||||
return sqlSessionFactoryBean.getObject();
|
||||
}
|
||||
|
||||
@Bean(name = "mybatisTransactionManager")
|
||||
public DataSourceTransactionManager dataSourceTransactionManager() {
|
||||
return new DataSourceTransactionManager(dataSource());
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.xwbing.druid;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 说明: 德鲁伊数据源配置。监控地址:druid/index.html
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/12/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(DruidDataSourceModel.class)
|
||||
public class DruidDataSourceConfig {
|
||||
@Resource
|
||||
private DruidDataSourceModel druidDataSourceModel;
|
||||
private final Logger logger = LoggerFactory.getLogger(DruidDataSourceConfig.class);
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public DruidDataSource dataSource() {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl(druidDataSourceModel.getUrl());
|
||||
dataSource.setDriverClassName(druidDataSourceModel.getDriver());
|
||||
dataSource.setUsername(druidDataSourceModel.getUsername());
|
||||
dataSource.setPassword(druidDataSourceModel.getPassword());
|
||||
dataSource.setInitialSize(druidDataSourceModel.getInitSize());
|
||||
dataSource.setMinIdle(druidDataSourceModel.getMinIdle());
|
||||
dataSource.setMaxActive(druidDataSourceModel.getMaxActive());
|
||||
dataSource.setMaxWait(druidDataSourceModel.getMaxWait());
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(druidDataSourceModel.getTimeBetweenEvictionRunsMillis());
|
||||
dataSource.setMinEvictableIdleTimeMillis(druidDataSourceModel.getMinEvictableIdleTimeMillis());
|
||||
dataSource.setValidationQuery(druidDataSourceModel.getValidationQuery());
|
||||
dataSource.setTestWhileIdle(druidDataSourceModel.isTestWhileIdle());
|
||||
dataSource.setTestOnBorrow(druidDataSourceModel.isTestOnBorrow());
|
||||
dataSource.setTestOnReturn(druidDataSourceModel.isTestOnReturn());
|
||||
dataSource.setPoolPreparedStatements(druidDataSourceModel.isPoolPreparedStatements());
|
||||
dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidDataSourceModel.getMaxPoolPreparedStatementPerConnectionSize());
|
||||
dataSource.setConnectionProperties(druidDataSourceModel.getConnectionProperties());
|
||||
dataSource.setUseGlobalDataSourceStat(druidDataSourceModel.isUseGlobalDataSourceStat());
|
||||
try {
|
||||
dataSource.setFilters(druidDataSourceModel.getFilters());
|
||||
} catch (SQLException e) {
|
||||
logger.error("配置druid过滤器异常");
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean statViewServlet() {
|
||||
logger.info("注册druid监控信息显示statViewServlet ======================= ");
|
||||
ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet());
|
||||
registration.addUrlMappings("/druid/*");
|
||||
//添加初始化参数:initParams
|
||||
// registration.addInitParameter("allow", "127.0.0.1");//IP白名单(没有配置或者为空,则允许所有访问)
|
||||
// registration.addInitParameter("deny", "172.17.21.232");//IP黑名单(存在共同时,deny优先于allow)
|
||||
registration.addInitParameter("loginUsername", "admin");//用户名
|
||||
registration.addInitParameter("loginPassword", "123456");//密码
|
||||
registration.addInitParameter("resetEnable", "true");//禁用HTML页面上的"Reset All"功能
|
||||
return registration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean webStatFilter() {
|
||||
logger.info("注册druid监控信息采集webStatFilter ======================= ");
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
|
||||
filterRegistrationBean.addUrlPatterns("/*");
|
||||
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,*.jsp,/druid/*");
|
||||
filterRegistrationBean.addInitParameter("profileEnable", "true");//监控单个url调用的sql列表
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package com.xwbing.druid;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 说明: 德鲁伊数据源参数模型
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@PropertySource("classpath:druid.properties")
|
||||
@ConfigurationProperties(prefix = "jdbc")
|
||||
public class DruidDataSourceModel {
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
private String driver;
|
||||
/**
|
||||
* 初始连接数
|
||||
*/
|
||||
private int initSize;
|
||||
/**
|
||||
* 最小空闲
|
||||
*/
|
||||
private int minIdle;
|
||||
/**
|
||||
* 最大连接数
|
||||
*/
|
||||
private int maxActive;
|
||||
/**
|
||||
* 连接等待超时时间
|
||||
*/
|
||||
private int maxWait;
|
||||
/**
|
||||
* 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
|
||||
*/
|
||||
private int timeBetweenEvictionRunsMillis;
|
||||
/**
|
||||
* 配置一个连接在池中最小生存的时间,单位是毫秒
|
||||
*/
|
||||
private int minEvictableIdleTimeMillis;
|
||||
/**
|
||||
* 用来检测连接是否有效的sql,要求是一个查询语句
|
||||
*/
|
||||
private String validationQuery;
|
||||
/**
|
||||
* 申请连接的时候检测
|
||||
*/
|
||||
private boolean testWhileIdle;
|
||||
/**
|
||||
* 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能
|
||||
*/
|
||||
private boolean testOnBorrow;
|
||||
/**
|
||||
* 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能
|
||||
*/
|
||||
private boolean testOnReturn;
|
||||
/**
|
||||
* 打开PSCache,mysql5.5以下的版本中没有PSCache功能,建议关闭掉
|
||||
*/
|
||||
private boolean poolPreparedStatements;
|
||||
/**
|
||||
* 指定每个连接上PSCache的大小
|
||||
*/
|
||||
private int maxPoolPreparedStatementPerConnectionSize;
|
||||
/**
|
||||
* 配置监控统计拦截的filters
|
||||
*/
|
||||
private String filters;
|
||||
/**
|
||||
* 慢SQL记录
|
||||
*/
|
||||
private String connectionProperties;
|
||||
/**
|
||||
* 合并多个DruidDataSource的监控数据
|
||||
*/
|
||||
private boolean useGlobalDataSourceStat;
|
||||
}
|
||||
@@ -1,29 +1,83 @@
|
||||
# 数据库基本信息配置
|
||||
jdbc.driver=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://127.0.0.1:3306/xwbing?useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
jdbc.username=root
|
||||
jdbc.password=xwbing
|
||||
##jdbc配置#####################################
|
||||
db1.driver-class-name=com.mysql.jdbc.Driver
|
||||
db1.url=jdbc:mysql://127.0.0.1:3306/xwbing?useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
db1.username=root
|
||||
db1.password=xwbing
|
||||
##连接池配置###############################
|
||||
# 初始化大小,最小,最大
|
||||
jdbc.initSize=1
|
||||
jdbc.minIdle=3
|
||||
jdbc.maxActive=20
|
||||
db1.initial-size=1
|
||||
db1.min-idle=3
|
||||
db1.max-active=20
|
||||
# 获取连接最大等待时间
|
||||
jdbc.maxWait=60000
|
||||
db1.max-wait=60000
|
||||
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
|
||||
jdbc.timeBetweenEvictionRunsMillis=60000
|
||||
db1.time-between-eviction-runs-millis=60000
|
||||
# 配置一个连接在池中最小生存的时间,单位是毫秒
|
||||
jdbc.minEvictableIdleTimeMillis=30000
|
||||
db1.min-evictable-idle-time-millis=30000
|
||||
# 连接性能检测(用来检测连接是否有效的sql,为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用)
|
||||
jdbc.validationQuery=SELECT 'x'
|
||||
jdbc.testWhileIdle=true
|
||||
jdbc.testOnBorrow=false
|
||||
jdbc.testOnReturn=false
|
||||
db1.validation-query=SELECT 'x'
|
||||
db1.test-while-idle=true
|
||||
db1.test-on-borrow=false
|
||||
db1.test-on-return=false
|
||||
# 打开PSCache,并且指定每个连接上PSCache的大小 ,mysql5.5以下的版本中没有PSCache功能,建议关闭掉
|
||||
jdbc.poolPreparedStatements=true
|
||||
jdbc.maxPoolPreparedStatementPerConnectionSize=20
|
||||
db1.pool-prepared-statements=true
|
||||
db1.max-pool-prepared-statement-per-connection-size=20
|
||||
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,防止sql注入
|
||||
jdbc.filters=stat,wall,slf4j
|
||||
db1.filters=stat,wall,slf4j
|
||||
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
|
||||
jdbc.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
||||
db1.connect-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
||||
# 合并多个DruidDataSource的监控数据
|
||||
#jdbc.useGlobalDataSourceStat=true
|
||||
#datasource.useGlobalDataSourceStat=true
|
||||
##WebStatFilter配置###################################
|
||||
db1.web-stat-filter.enabled=false
|
||||
db1.web-stat-filter.url-pattern=/*
|
||||
db1.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
|
||||
#sesison统计
|
||||
db1.web-stat-filter.session-stat-enable=true
|
||||
db1.web-stat-filter.session-stat-max-count=1000
|
||||
db1.web-stat-filter.principal-session-name=
|
||||
db1.web-stat-filter.principal-cookie-name=
|
||||
#监控单个url调用的sql列表
|
||||
db1.web-stat-filter.profile-enable=true
|
||||
##StatViewServlet配置###############################
|
||||
db1.stat-view-servlet.enabled=true
|
||||
db1.stat-view-servlet.url-pattern=/druid/*
|
||||
#禁用HTML页面上的"Reset All"功能
|
||||
db1.stat-view-servlet.reset-enable=true
|
||||
db1.stat-view-servlet.login-username=admin
|
||||
db1.stat-view-servlet.login-password=123456
|
||||
#IP白名单(没有配置或者为空,则允许所有访问)
|
||||
db1.stat-view-servlet.allow=
|
||||
#IP黑名单(存在共同时,deny优先于allow)
|
||||
db1.stat-view-servlet.deny=
|
||||
###########################################################################################################
|
||||
##jdbc配置#####################################
|
||||
db2.driver-class-name=com.mysql.jdbc.Driver
|
||||
db2.url=jdbc:mysql://127.0.0.1:3306/xwbing?useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
db2.username=root
|
||||
db2.password=xwbing
|
||||
##连接池配置###############################
|
||||
# 初始化大小,最小,最大
|
||||
db2.initial-size=1
|
||||
db2.min-idle=3
|
||||
db2.max-active=20
|
||||
# 获取连接最大等待时间
|
||||
db2.max-wait=60000
|
||||
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
|
||||
db2.time-between-eviction-runs-millis=60000
|
||||
# 配置一个连接在池中最小生存的时间,单位是毫秒
|
||||
db2.min-evictable-idle-time-millis=30000
|
||||
# 连接性能检测(用来检测连接是否有效的sql,为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用)
|
||||
db2.validation-query=SELECT 'x'
|
||||
db2.test-while-idle=true
|
||||
druid.db2.test-on-borrow=false
|
||||
db2.test-on-return=false
|
||||
# 打开PSCache,并且指定每个连接上PSCache的大小 ,mysql5.5以下的版本中没有PSCache功能,建议关闭掉
|
||||
db2.pool-prepared-statements=true
|
||||
db2.max-pool-prepared-statement-per-connection-size=20
|
||||
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,防止sql注入
|
||||
db2.filters=stat,wall,slf4j
|
||||
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
|
||||
db2.connect-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
||||
# 合并多个DruidDataSource的监控数据
|
||||
#datasource.useGlobalDataSourceStat=true
|
||||
@@ -2,6 +2,7 @@ package com.xwbing;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
@@ -11,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
*/
|
||||
//@EnableRabbit
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
//@Configuration 定义为配置类,相当于xml文件中的<beans></beans>
|
||||
//@ComponentScan 组件扫描
|
||||
//@EnableAutoConfiguration 借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器
|
||||
|
||||
@@ -59,6 +59,10 @@ spring:
|
||||
cache:
|
||||
channel:
|
||||
size: 1
|
||||
#mybatis
|
||||
mybatis:
|
||||
mapper-locations:
|
||||
config-location:
|
||||
#log
|
||||
logging:
|
||||
config: classpath:log4j2.xml
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
<rsa.version>1.45</rsa.version>
|
||||
<commons-io.version>2.2</commons-io.version>
|
||||
<alipay-version>1.0</alipay-version>
|
||||
<druid-version>1.1.5</druid-version>
|
||||
<druid-version>1.1.10</druid-version>
|
||||
<poi-verison>3.14</poi-verison>
|
||||
<httpclient-version>4.5.1</httpclient-version>
|
||||
<!-- 打包跳过单元测试 -->
|
||||
|
||||
Reference in New Issue
Block a user