数据库连接池类别
目前市面上常用的就2个,其他的不必看
- HikariCP (Spring默认内置)
- druid
HikariCP是Spring中默认的数据库连接池方案,国外用的多,国内一般用的是 druid。
druid 和 druid-spring-boot-starter区别
- druid-spring-boot-starter 是在 druid 基础上进行了一次封装,专门用来整合 spring-boot 项目;
- druid 需要自己手动编写配置类并注入到 bean 中
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
结论:使用 druid-spring-boot-starter
使用 druid-spring-boot-starter
一共有3步
- pom中导入坐标,3个坐标
- application.yml文件中写入配置信息,注意格式
- 编写一个测试类验证
注意不用再编写配置类
- pom中导入坐标,3个坐标
<!-- 数据库连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
- application.yml文件中写入配置信息
注意配置url、username、password、driver以外的其他配置必须在druid下写,这样会自动包装到DruidDataSourceWrapper里面
server:
port: 9110
spring:
application:
name: reggie
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/firstdemo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
druid:
initial-size: 10
min-idle: 5
max-active: 20
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 60000
test-while-idle: true
test-on-borrow: false
test-on-return: false
filters: stat,wall
stat-view-servlet:
enabled: true
login-username: daniel
login-password: 1234
url-pattern: /druid/*
reset-enable: false
allow: 127.0.0.1,192.168.2.29,192.168.1.1
deny: 22.1.2.3
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
session-stat-enable: true
session-stat-max-count: 1
principal-session-name: userName
principal-cookie-name: userName
profile-enable: true
filter:
stat:
log-slow-sql: false
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true
- 编写一个测试类验证
package com.cool.reggie;
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class DruidStarterTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Resource
private DataSource dataSource;
@Test
public void testDruidConnector() throws SQLException, InstantiationException, IllegalAccessException {
DruidDataSource druidDataSource = (DruidDataSource) dataSource;
System.out.println(druidDataSource.getUsername());
System.out.println(druidDataSource.getPassword());
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from student");
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
}
druid监控
druid 不光是数据库连接池,还可以查看数据源,sql监控、sql防火墙、web应用、session监控、URI监控、spring监控、json api等信息。
可以实时观察数据库连接池和 SQL 的运行情况,帮助我们及时排查出系统中存在的问题。
访问地址:http://localhost:9110/druid/index.html
如果你复制了上面的配置内容,那么stat-view-servlet.allow 白名单需要改为你自己的本地Ip地址,或者不设置此项。 否则你会看到页面Sorry, you are not permitted to view this page. tips: 查看自己本机的ip地址,命令行中输入 ipconfig ,
参考资料
Maven仓库 - druid 配置StatFilter druid-start自动装配原理
|