标签:数据库连接 web mini return ams 账号密码 tab admin created
springboot 1.5.9.RELEASE + JDK1.8
分两步,1 配置数据源 2 配置监控
1 配置数据源
package com.company.project.support.druid;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import java.sql.SQLException;
/**
* 纯java config的方式去配置druid
* javaBean方式注册数据库连接池
* Created by hxy on 2018/6/2.
*/
@Configuration
public class DataSourceConfig {
@Bean
public DruidDataSource getDataSourceConfig() throws SQLException {
DruidDataSource ds = new DruidDataSource();
// 基本属性
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://数据库的IP:3306/数据库名?characterEncoding=utf-8&useSSL=false&useUnicode=true");
ds.setUsername("账户");
ds.setPassword("密码");
// 配置初始化大小、最小、最大
ds.setInitialSize(5);
ds.setMinIdle(5);
ds.setMaxActive(50);
// 配置获取连接等待超时的时间
ds.setMaxWait(60000);
// 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
ds.setTimeBetweenEvictionRunsMillis(60000);
// 配置一个连接在池中最小生存的时间,单位是毫秒
ds.setMinEvictableIdleTimeMillis(300000);
ds.setValidationQuery("SELECT ‘X‘");
ds.setTestWhileIdle(true);
ds.setTestOnBorrow(false);
ds.setTestOnReturn(false);
// 打开PSCache,并且指定每个连接上PSCache的大小
ds.setPoolPreparedStatements(false);
ds.setMaxPoolPreparedStatementPerConnectionSize(20);
// 配置监控统计拦截的filters
ds.setFilters("stat,wall");
return ds;
}
/*
* JDBC模板
*/
@Bean
public JdbcTemplate jdbcTemplate(DruidDataSource druidDataSource) throws SQLException {
return new JdbcTemplate(druidDataSource);
}
}
2 配置监控页面
package com.company.project.support.druid;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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;
/**
* druid监控页面的配置
*/
@Configuration
public class DruidStatViewServletConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet());
servletRegistrationBean.addUrlMappings("/druid/*");
//添加初始化参数:initParams
//白名单:
// servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
// servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
//是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
2510-Druid监控功能的深入使用与配置-基于SpringBoot-完全使用java config的形式
标签:数据库连接 web mini return ams 账号密码 tab admin created
原文地址:https://www.cnblogs.com/starmoon1994/p/9126404.html