欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot Druid整合,SpringBoot 集成Druid

程序员文章站 2022-05-10 18:18:31
...

SpringBoot Druid整合,SpringBoot 集成Druid

 

================================

©Copyright 蕃薯耀 2018年4月3日

http://fanshuyao.iteye.com/

 

一、SpringBoot引入Druid的依赖包

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>
		
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.9</version>
</dependency>

 

二、设置配置文件application.properties

spring.datasource.url=jdbc:mysql://192.168.1.166:3306/study
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.schema=

#数据源其他配置,需要增加Druid的配置类(不然不生效)
#配置见:com.lqy.springboot.datasource.druid.DruidConfig
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
spring.datasource.filters=stat,wall,log4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.useGlobalDataSourceStat=true  
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

 

三、增加Druid自定义配置类

为什么要增加呢?

因为SpringBoot本身提供的配置只是部分的数据库连接配置,很多Druid需要的属性配置是没有的,如:

spring.datasource.initialSize

 这个属性,SpringBoot是没有的,只有Druid才有,所以需要增加相应的配置类,自动注入。

 

DruidConfig 类

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * 导入druid数据源,扩展相关的配置属性
 *
 */
@Configuration
public class DruidConfig {

	/**
	 * 配置Druid的属性,和DataSource进行绑定,前缀设置为:spring.datasource
	 * 不配置的话,很多初始化的属性是没有绑定的
	 * @return
	 */
	@Bean
	@ConfigurationProperties(prefix="spring.datasource")
	public DataSource druid() {
		return new DruidDataSource();
	}
	
	/**
	 * 配置druid监控
	 * 配置一个管理后台的servlet
	 * 访问地址:http://localhost:8080/druid/
	 * @return
	 */
	@Bean
	public ServletRegistrationBean statViewServlet() {
		ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		Map<String, String> initParameters = new HashMap<String, String>();
		initParameters.put("loginUsername", "admin");//属性见:com.alibaba.druid.support.http.ResourceServlet
		initParameters.put("loginPassword", "123456");
		initParameters.put("allow", "");//默认允许所有
		initParameters.put("deny", "");
		bean.setInitParameters(initParameters);
		return bean;
	}
	
	/**
	 * 配置一个web监控的filter
	 * @return
	 */
	@Bean
	public FilterRegistrationBean webStatFilter() {
		FilterRegistrationBean filterBean = new FilterRegistrationBean();
		filterBean.setFilter(new WebStatFilter());
		filterBean.setUrlPatterns(Arrays.asList("/*"));
		
		Map<String, String> initParameters = new HashMap<String, String>();
		initParameters.put("exclusions", "*.js,*.css,/druid/*");//属性见:com.alibaba.druid.support.http.WebStatFilter
		filterBean.setInitParameters(initParameters);
		
		return filterBean;
	}
	
	
}

 

 四、启动项目,测试数据源连接:

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataSourceController {

	@Autowired
	private DataSource dataSource;
	
	@RequestMapping("/dataSource")
	public String dataSource() {
		try {
			System.out.println("dataSource = "+dataSource);
			Connection conn = dataSource.getConnection();
			System.out.println("conn = "+conn);
			return "success";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "end.";
	}
	
	
}

 

五、查看Druid的监控

浏览器打开:http://localhost:8080/druid

登录的账号密码在DruidConfig类配置有

 

(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!) 


SpringBoot Druid整合,SpringBoot 集成Druid
            
    
    博客分类: javaSpringBootspring蕃薯耀分享 SpringBoot Druid整合SpringBoot 集成DruidDruidSpringBoot蕃薯耀     SpringBoot Druid整合,SpringBoot 集成Druid
            
    
    博客分类: javaSpringBootspring蕃薯耀分享 SpringBoot Druid整合SpringBoot 集成DruidDruidSpringBoot蕃薯耀 

 

================================

©Copyright 蕃薯耀 2018年4月3日

http://fanshuyao.iteye.com/