一.SpringBoot整合Druid
Druid是阿里巴巴的一個開源項目,是一個數據庫連接池的實現,結合了C3P0、DBCP、PROXOOL等DB池的優點,整合配置參考地址。Druid不但提供連接池的功能,還提供監控功能,可以實時查看數據庫連接池和SQL查詢的工作情況(最牛X的地方就在與提供的日志監控功能)。在上一章中(SpringBoot整合JDBC,JPA)講述到,Spring Boot底層都是采用Spring Data的方式進行統一處理,Spring Data中內置連接池數據源HikariDataSource。SpringBoot整合Druid實現數據源更換,則需要手動配置。
1.新建SpringBoot工程,引入依賴
基礎依賴:新建SpringBoot工程,建議引入以下基礎依賴
spring-boot-starter-parent(SpringBoot父工程)——必選
spring-boot-starter(SpringBoot啟動器)——必選
spring-boot-devtools(SpringBoot熱部署)——可選
spring-boot-starter-web(SpringBoot整合WEB)——必選
mysql-connector-java(MySQL驅動)——必選
druid-spring-boot-starter(SpringBoot整合Druid)——必選
spring-boot-starter-jdbc(SpringBoot整合JDBC)——必選
spring-boot-starter-test(SpringBoot整合測試Junit)——默認必選
spring-boot-maven-plugin(SpringBoot打包插件)——必選
lombok(整合Lombok簡化POJO開發)——可選
spring-boot-configuration-processor(SpringBoot整合配置文件注入POJO)——可選
1 <!-- MySQL驅動 --> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 <scope>runtime</scope> 6 </dependency> 7 8 <!-- SpringBoot整合Druid --> 9 <dependency> 10 <groupId>com.alibaba</groupId> 11 <artifactId>druid-spring-boot-starter</artifactId> 12 <version>1.1.22</version> 13 </dependency>
常見問題:整合如果沒有引入spring-boot-starter-jdbc,會報錯:.....ClassNotFoundException: org....jdbc....embedded.EmbeddedDatabaseType。
問題原因:Spring Boot底層都是采用Spring Data的方式進行統一處理。EmbeddedDatabaseType類在spring-boot-starter-jdbc依賴中。
依賴說明:SpringBoot屬於Spring“全家桶”組件,Druid屬於阿里巴巴旗下開發產品,所以SpringBoot整合Druid並不是由Spring組件提供依賴,而是由阿里巴巴提供。
2.修改yml配置文件,整合配置Druid
# 數據庫訪問配置, 使用druid數據源(默認數據源是HikariDataSource)
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initial-size: 5
min-idle: 5
max-active: 20
# 連接等待超時時間
max-wait: 30000
# 配置檢測可以關閉的空閑連接,間隔時間
time-between-eviction-runs-millis: 60000
# 配置連接在池中的最小生存時間
min-evictable-idle-time-millis: 300000
# 檢測連接是否有,有效得select語句
validation-query: select '1' from dual
# 申請連接的時候檢測,如果空閑時間大於time-between-eviction-runs-millis,執行validationQuery檢測連接是否有效,建議配置為true,不影響性能,並且保證安全性。
test-while-idle: true
# 申請連接時執行validationQuery檢測連接是否有效,建議設置為false,不然會會降低性能
test-on-borrow: false
# 歸還連接時執行validationQuery檢測連接是否有效,建議設置為false,不然會會降低性能
test-on-return: false
# 是否緩存preparedStatement,也就是PSCache 官方建議MySQL下建議關閉 個人建議如果想用SQL防火牆 建議打開
# 打開PSCache,並且指定每個連接上PSCache的大小
pool-prepared-statements: true
# 配置監控統計攔截的filters, 去掉后監控界面sql無法統計, 'wall'用於防火牆防御sql注入,stat監控統計,logback日志
filters: stat,wall
# Spring監控AOP切入點,如x.y.z.service.*,配置多個英文逗號分隔
#aop-patterns: com.springboot.servie.*
# lowSqlMillis用來配置SQL慢的標准,執行時間超過slowSqlMillis的就是慢
useGlobalDataSourceStat: true
max-pool-prepared-statement-per-connection-size: 20
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
上述配置不但配置了Druid作為連接池,而且還開啟了Druid的監控功能。
druid強大的地方在於它可以自定義配置
config包——————新建一個類DruidConfig——————DataSource druidDataSource(){return new DruidDataSource();}——————@Bean 注入到spring容器中——————@
ConfigurationProperties(prefix = "spring.datasource")
//后台監控 : 相當於web.xml ServletRegistrationBean
//springboot內置了servlet容器,所有沒有web.xml 替代方法:將ServletRegistrationBean注冊進去
public ServletRegistrationBean StatViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(), "/druid/**");
//后台有人要登錄,賬號密碼配置
HashMap<String, String> initParameters = new HashMap<>();
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123456"); //登錄的key是固定的 必須是loginUsername loginPassword
//允許誰能訪問
initParameters.put("allow","");
//也可以禁止誰能訪問
initParameters.put("dragon","ip地址");
bean.setInitParameters(initParameters);//設置初始化參數
return bean;
}
//過濾器
@Bean
public FilterRegistrationBean webStaFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
HashMap<String, String> initParameters = new HashMap<>();
initParameters.put("exclusions","*.js,*.css");
bean.setInitParameters(initParameters);
return bean;
}
