1. 程式人生 > 其它 >競賽期中考試總結及題解

競賽期中考試總結及題解

一.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

spring:
datasource:
# 資料庫訪問配置, 使用druid資料來源(預設資料來源是HikariDataSource)
type: com.alibaba.druid.pool.DruidDataSource
#連結池配置
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: xsge

# 連線池配置:大小,最小,最大
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
max-open-prepared-statements: 20
max-pool-prepared-statement-per-connection-size: 20

# 配置監控統計攔截的filters, 去掉後監控介面sql無法統計, 'wall'用於防火牆防禦sql注入,stat監控統計,logback日誌
filters: stat,wall
# Spring監控AOP切入點,如x.y.z.service.*,配置多個英文逗號分隔
#aop-patterns: com.springboot.servie.*
# lowSqlMillis用來配置SQL慢的標準,執行時間超過slowSqlMillis的就是慢
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

# WebStatFilter監控配置
web-stat-filter:
# 新增過濾規則:那些訪問攔截統計
# 忽略過濾的格式:哪些不攔截,不統計
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

# StatViewServlet配置(Druid監控後臺的Servlet對映配置,因為SpringBoot專案沒有web.xml所在在這裡使用配置檔案設定)
stat-view-servlet:
enabled: true
# 配置Servlet的訪問路徑:訪問路徑為/druid/**時,跳轉到StatViewServlet,會自動轉到Druid監控後臺
url-pattern: /druid/*
# 是否能夠重置資料
reset-enable: false
# 設定監控後臺的訪問賬戶及密碼
login-username: xsge
login-password: xsge
# IP白名單:允許哪些主機訪問,預設為“”任何主機
# allow: 127.0.0.1
# IP黑名單:禁止IP訪問,(共同存在時,deny優先於allow)
# deny: 192.168.1.218

# 配置StatFilter
filter:
stat:
log-slow-sql: true

上述配置不但配置了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;
}