我是靠谱客的博主 幽默网络,最近开发中收集的这篇文章主要介绍springboot整合mysql+druid以及druid监控台1.pom.xml加入依赖2.application.yml配置属性3.bean装载,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.pom.xml加入依赖

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>

2.application.yml配置属性

#应用端口号
server:
port: 8091

#druid监控台配置信息,主要用作sql监控,还能用来监控url等
druid:
allow:
ip: 127.0.0.1
login:
user_name: root
password: root

#数据源配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shop?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
initial-size: 1
min-idle: 1
max-idle: 5
max-wait-millis: 60000
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

3.bean装载

package com.learn.couponapp.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
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.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {


@Value("${druid.login.user_name}")
private String userName;

@Value("${druid.login.password}")
private String password;


@Bean(name = "default_datadatasource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource(){
return new DruidDataSource();
}


/**
* druid监控台配置
* @return
*/
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new StatViewServlet());
servletRegistrationBean.addUrlMappings("/druid/*");
Map<String, String> initParameters = new HashMap<>();
initParameters.put("loginUsername", userName);// 用户名
initParameters.put("loginPassword", password);// 密码
initParameters.put("resetEnable", "false");// 禁用HTML页面上的“Reset All”功能
servletRegistrationBean.setInitParameters(initParameters);
return servletRegistrationBean;
}

@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}

}

转载于:https://www.cnblogs.com/yizhixiaomayuan/p/11374219.html

最后

以上就是幽默网络为你收集整理的springboot整合mysql+druid以及druid监控台1.pom.xml加入依赖2.application.yml配置属性3.bean装载的全部内容,希望文章能够帮你解决springboot整合mysql+druid以及druid监控台1.pom.xml加入依赖2.application.yml配置属性3.bean装载所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(30)

评论列表共有 0 条评论

立即
投稿
返回
顶部