springboot配置druid
2021-02-10 16:20
标签:数据源 script lse frame code 方法 connector host idle 1.引入druid 以上需要注意的时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内容如下: 3.log4.properties配置文件 4.DruidConfig.java springboot配置druid 标签:数据源 script lse frame code 方法 connector host idle 原文地址:https://www.cnblogs.com/pengpengzhang/p/12741324.htmlxml 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.0modelVersion>
groupId>com.seegotgroupId>
artifactId>spring-boot-06-data-jdbcartifactId>
version>0.0.1-SNAPSHOTversion>
name>spring-boot-06-data-jdbcname>
description>Demo project for Spring Bootdescription>
properties>
java.version>1.8java.version>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
spring-boot.version>2.2.6.RELEASEspring-boot.version>
properties>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-jdbcartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
scope>runtimescope>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druid-spring-boot-starterartifactId>
version>1.1.10version>
dependency>
dependency>
groupId>log4jgroupId>
artifactId>log4jartifactId>
version>1.2.17version>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
exclusions>
exclusion>
groupId>org.junit.vintagegroupId>
artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
dependencyManagement>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-dependenciesartifactId>
version>${spring-boot.version}version>
type>pomtype>
scope>importscope>
dependency>
dependencies>
dependencyManagement>
build>
plugins>
plugin>
groupId>org.apache.maven.pluginsgroupId>
artifactId>maven-compiler-pluginartifactId>
configuration>
source>1.8source>
target>1.8target>
encoding>UTF-8encoding>
configuration>
plugin>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
version>2.2.6.RELEASEversion>
plugin>
plugins>
build>
project>
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
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
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