SpringBoot學習筆記(7):Druid使用心得

SpringBoot學習筆記(7):Druid使用心得

快速開始

添加依賴

	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>${druid-version}</version>
	</dependency>

配置屬性

JDBC配置

spring.datasource.druid.url= # 或spring.datasource.url= 
spring.datasource.druid.username= # 或spring.datasource.username=
spring.datasource.druid.password= # 或spring.datasource.password=
spring.datasource.druid.driver-class-name= #或 spring.datasource.driver-class-name=

鏈接池配置

spring.datasource.druid.initial-size=
spring.datasource.druid.max-active=
spring.datasource.druid.min-idle=
spring.datasource.druid.max-wait=
spring.datasource.druid.pool-prepared-statements=
spring.datasource.druid.max-pool-prepared-statement-per-connection-size= 
spring.datasource.druid.max-open-prepared-statements= #和上面的等價
spring.datasource.druid.validation-query=
spring.datasource.druid.validation-query-timeout=
spring.datasource.druid.test-on-borrow=
spring.datasource.druid.test-on-return=
spring.datasource.druid.test-while-idle=
spring.datasource.druid.time-between-eviction-runs-millis=
spring.datasource.druid.min-evictable-idle-time-millis=
spring.datasource.druid.max-evictable-idle-time-millis=
spring.datasource.druid.filters= #配置多個英文逗號分隔

監控配置

# WebStatFilter配置,說明請參考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled= #是否啓用StatFilter默認值true
spring.datasource.druid.web-stat-filter.url-pattern=
spring.datasource.druid.web-stat-filter.exclusions=
spring.datasource.druid.web-stat-filter.session-stat-enable=
spring.datasource.druid.web-stat-filter.session-stat-max-count=
spring.datasource.druid.web-stat-filter.principal-session-name=
spring.datasource.druid.web-stat-filter.principal-cookie-name=
spring.datasource.druid.web-stat-filter.profile-enable=

# StatViewServlet配置,說明請參考Druid Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled= #是否啓用StatViewServlet默認值true
spring.datasource.druid.stat-view-servlet.url-pattern=
spring.datasource.druid.stat-view-servlet.reset-enable=
spring.datasource.druid.stat-view-servlet.login-username=
spring.datasource.druid.stat-view-servlet.login-password=
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=

# Spring監控配置,說明請參考Druid Github Wiki,配置_Druid和Spring關聯監控配置
spring.datasource.druid.aop-patterns= # Spring監控AOP切入點,如x.y.z.service.*,配置多個英文逗號分隔

簡單配置示例

# 配置數據源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/gongxiao?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 鏈接池配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=2335
spring.datasource.druid.max-wait=100
spring.datasource.druid.test-on-borrow=true

如何在Spring Boot中集成Druid鏈接池和監控?

SpringBoot添加Filter或者Servlet

  在SpringBoot中,有兩種方式能夠添加Servlet/Filter/Listener到應用中。html

方法1:經過使用Spring Bean添加

  要使用Spring bean添加Servlet,Filter或Servlet * Listener,必須爲其提供一個@Bean定義java

  對於Filters和Servlet,您還能夠添加映射和初始化參數,方法是添加FilterRegistrationBean或ServletRegistrationBean,而不是或添加到基礎組件。mysql

@Bean
public FilterRegistrationBean registration(MyFilter filter) {
	FilterRegistrationBean registration = new FilterRegistrationBean(filter);
	registration.setEnabled(false);
	return registration;
}

說明web

  registration.setEnabled(false);能夠將配置設置爲禁用狀態。spring

方法2:經過使用類路徑掃描添加

@WebServlet,@WebFilter和@WebListener帶註釋的類能夠經過使用@ServletComponentScan註釋@Configuration類並指定包含要註冊的組件的包來自動向嵌入式servlet容器註冊。 默認狀況下,@ServletComponentScan從註釋類的包中進行掃描。sql

配置監控頁面

    @Bean
    public ServletRegistrationBean druidServlet()
    {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new StatViewServlet());
        servletRegistrationBean.addUrlMappings("/druid/*");
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("loginUsername", "admin");// 用戶名
        initParameters.put("loginPassword", "123456");// 密碼
        initParameters.put("resetEnable", "false");// 禁用HTML頁面上的「Reset All」功能
        initParameters.put("allow", ""); // IP白名單 (沒有配置或者爲空,則容許全部訪問)
        //initParameters.put("deny", "192.168.20.38");// IP黑名單 (存在共同時,deny優先於allow)
        servletRegistrationBean.setInitParameters(initParameters);
        return servletRegistrationBean;
    }

訪問監控頁面

配置數據庫密碼加密

 

防護SQL注入攻擊

 

輸入SQL執行信息

相關文章
相關標籤/搜索