springboot配置druid
Posted 。低调ヽ继续
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot配置druid相关的知识,希望对你有一定的参考价值。
1.引入druid
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.seegot</groupId> <artifactId>spring-boot-06-data-jdbc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-06-data-jdbc</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.2.6.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--引入druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.6.RELEASE</version> </plugin> </plugins> </build> </project>
以上需要注意的时druid的版本问题,我之前引入了最新的druid相关依赖,此时启动程序后,控制台不报错,但是当访问druid时,页面404,后来查证,是因为版本不兼容问题。所以更换了1.1.10版本。
也有说自1.1.10以后。是否启用StatViewServlet默认值false,是不开启监控页面展示的。 所以你需要配置文件增加配置:spring.datasource.druid.stat-view-servlet.enabled=true就展示了,这种方法,我尝试了几次都没有成功,所以我最终还是选择了1.1.10版本的。
2.application.yml内容如下:
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.0.115:3306/jdbc
type: com.alibaba.druid.pool.DruidDataSource
# druid 数据源其他配置 当添加了如下配置,我们需要在程序中增加configuration配置来将这些配置映射到系统默认配置参数上去。
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
driver-class-name: com.mysql.cj.jdbc.Driver
# 自动加载数据库
# schema:
# - classpath:camera.sql
# initialization-mode: always
3.log4.properties配置文件
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4.DruidConfig.java
package com.seegot.springboot.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; 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 javax.sql.DataSource; import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** * @program: spring-boot-06-data-jdbc * @description: * @author: PP Zhang * @create: 2020-04-20 16:09 */ @Configuration public class DruidConfig {
// @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid(){ return new DruidDataSource(); } // 配置druid的监控 // 1.配置管理后台的servlet public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); Map<String,String> initParams = new HashMap<>(); initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow","localhost"); // 默认是允许所有的访问 initParams.put("deny","192.168.1.106"); // 拒绝谁访问 registrationBean.setInitParameters(initParams); return registrationBean; } // 2.配置监控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>(); initParams.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
以上是关于springboot配置druid的主要内容,如果未能解决你的问题,请参考以下文章
2510-Druid监控功能的深入使用与配置-基于SpringBoot-完全使用java config的形式
优雅写代码系统springboot+mybatis+pagehelper+mybatisplus+druid教你如何优雅写代码
SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源配置 MyBatis事务控制druid 监控)