这里SpringBoot版本为1.5.9.RELEASE。
【1】pom文件添加依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
【2】yml文件对数据源进行配置
yml文件示例如下:
spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: true testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
如果MySQL驱动版本比较高,你可能需要修改url和driver如下:
# jdbc:mysql://127.0.0.1:3306/canteen?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
如下图所示:
从initialSize属性开始,属性颜色呈现为黄色,这是不正常的。SpringBoot中,数据源配置默认对应DataSourceProperties类,很显然我们为Druid数据源配置的属性与默认数据源配置文件类不符。
解决方法: 容器中注册自定义数据源!
【3】注册Druid数据源
MyConfig 类如下:
@Configuration public class MyConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } }
测试如下图,拿到了配置文件中的数据源配置:
【4】配置Druid的监控
之所以使用Druid数据源,除了其性能可靠外,还在于其完善的数据监控机制。其主要是由StatViewServlet和WebStatFilter(用于配置Web和Druid数据源之间的管理关联监控统计)实现。
配置类如下:
@Configuration public class MyConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(); registrationBean.setServlet(new StatViewServlet()); registrationBean.setUrlMappings(Arrays.asList("/druid/*")); //设置初始化参数 Map<String,String> initMap = new HashMap<>(); initMap.put("loginUsername","admin"); initMap.put("loginPassword","123456"); initMap.put("allow",""); initMap.put("deny","192.168.2.110"); registrationBean.setInitParameters(initMap); return registrationBean; } @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new WebStatFilter()); registrationBean.setUrlPatterns(Arrays.asList("/*")); //设置初始化参数 Map<String,String> initMap = new HashMap<>(); initMap.put("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); registrationBean.setInitParameters(initMap); return registrationBean; } }
测试如下:
其对应xml配置如下(SpringBoot中无需xml):
<!-- druid监控配置 --> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>admin</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>123456</param-value> </init-param> <!-- 拒绝 --> <param-name>deny</param-name> <param-value>192.168.2.110</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
【5】Nginx+Tomcat+Druid
如下图所示,Nginx后面跟两个Tomcat:
遇到问题:访问druid登录总是重定向-302。
Tomcat项目部署方式如下(参考Tomcat部署项目的四种方式):
② 解决方式
将Tomcat8081、Tomcat8082的server.xml中配置Context标签部署sessionCookieName属性设置为不同。
浏览器查看websession.html
【6】druid数据库连接池配置参考
下面给出一个全量的druid数据库连接池配置,具体value根据需要进行更改。
#配置初始化大小,最小,最大 spring.datasource.druid.initial-size=1 spring.datasource.druid.max-active=20 spring.datasource.druid.min-idle=1 #配置获取连接等待超时的时间 spring.datasource.druid.max-wait=60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.druid.time-between-eviction-runs-millis=60000 #配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 'x' spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.test-while-idle=true #打开PSCache,并且指定每个连接上PSCache的大小 #如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。 spring.datasource.druid.pool-prepared-statements=false spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 #druid监控配置 spring.datasource.druid.filters=stat,slf4j # WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=2000 # StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置 spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.reset-enable=true spring.datasource.druid.stat-view-servlet.login-username=druid spring.datasource.druid.stat-view-servlet.login-password=123456 spring.datasource.druid.filter.slf4j.enabled=true spring.datasource.druid.filter.slf4j.result-set-log-enabled=false spring.datasource.druid.filter.slf4j.statement-create-after-log-enabled=false spring.datasource.druid.filter.slf4j.statement-close-after-log-enabled=false spring.datasource.druid.filter.slf4j.result-set-open-after-log-enabled=false spring.datasource.druid.filter.slf4j.result-set-close-after-log-enabled=false