随着并发量的不断增加,显然单个数据库已经承受不了高并发带来的压力。一个项目使用多个数据库(无论是主从复制- - 读写分离还是分布式数据库结构)的重要性变得越来越明显。传统项目中(个人对传统项目的理解就是所有的业务模块都在一个tomcat中完成,多个相同的tomcat集群也可认为是传统项目)整合多数据源有两种方法:分包和AOP。这篇文章对分包进行讲解。
1、pom.xml引入maven依赖
<dependencies>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- sqlserver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
</dependencies>
2、在application.properties中配置多个数据源以及Druid配置
#数据库1
spring.datasource.jinggong.name=jinggong
spring.datasource.jinggong.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.jinggong.url=jdbc:sqlserver://ip:port;DatabaseName=db1
spring.datasource.jinggong.username=sa
spring.datasource.jinggong.password=1234
#数据库2
spring.datasource.huating.name=huating
spring.datasource.huating.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.huating.url=jdbc:sqlserver://ip:port;DatabaseName=db2
spring.datasource.huating.username=sa
spring.datasource.huating.password=1234
#数据库3
spring.datasource.huiguan.name=huiguan
spring.datasource.huiguan.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.huiguan.url=jdbc:sqlserver://ip:port;DatabaseName=db3
spring.datasource.huiguan.username=sa
spring.datasource.huiguan.password=1234
#mybatis配置
mybatis.config-location=classpath:/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
#region druid配置
#druid监控页面配置
#允许访问的ip
#druid.allow=176.1.7.55
#不允许访问的ip,优先级高于allow
#druid.deny=192.168.0.200
#用户名和密码
druid.loginUsername=hikvision
druid.loginPassword=hikvision
#是否允许重置监控内容
druid.resetEnable=true
druid.stat.mergeSql=true
druid.stat.slowSqlMillis=1000
#数据源通用配置
#为了启用各种插件
#stat:监控统计用 com.alibaba.druid.filter.stat.StatFilter
#log4j:日志用 com.alibaba.druid.filter.logging.Log4jFilter
#wa

最低0.47元/天 解锁文章
4万+

被折叠的 条评论
为什么被折叠?



