SpringBoot+Slf4j2+ Druid
1.pom.xml文件
org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime com.alibaba druid 1.1.17 org.projectlombok lombok true org.springframework.boot spring-boot-configuration-processor true org.codehaus.jackson jackson-mapper-asl 1.9.12 org.apache.commons commons-lang3 3.5
修改application.properties文件
server: port: 7001 spring: datasource: name: druidDataSource type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/qx?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false username: root password: root filters: stat,wall,slf4j,config #配置监控统计拦截的filters,去掉后监控界面SQL无法进行统计,wall用于防火墙。 max-active: 100 #最大连接数 initial-size: 1 #初始化大小 max-wait: 60000 #获取连接等待超时时间 min-idle: 1 #最小连接数 time-between-eviction-runs-millis: 60000 #间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒。 min-evictable-idle-time-millis: 300000 #一个连接在池中最小生存的时间,单位是毫秒。 validation-query: select 'x' test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20
logging:
config: classpath:logback.xml
# level:
# com.springframe.festmon.dao: trace
config
DruidConfig.java
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.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.servlet.Filter; import javax.servlet.Servlet; import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collections; import java.util.HashMap; import java.util.Map; @Configuration @EnableConfigurationProperties({DruidDataSourceProperties.class}) //@EnableConfigurationProperties注解用于导入上一步自定义的Druid的配置信息。 public class DruidConfig { @Autowired private DruidDataSourceProperties properties; @Bean @ConditionalOnMissingBean public DataSource druidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(properties.getDriverClassName()); druidDataSource.setUrl(properties.getUrl()); druidDataSource.setUsername(properties.getUsername()); druidDataSource.setPassword(properties.getPassword()); druidDataSource.setInitialSize(properties.getInitialSize()); druidDataSource.setMinIdle(properties.getMinIdle()); druidDataSource.setMaxActive(properties.getMaxActive()); druidDataSource.setMaxWait(properties.getMaxWait()); druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis()); druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis()); druidDataSource.setValidationQuery(properties.getValidationQuery()); druidDataSource.setTestWhileIdle(properties.isTestWhileIdle()); druidDataSource.setTestOnBorrow(properties.isTestOnBorrow()); druidDataSource.setTestOnReturn(properties.isTestOnReturn()); druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements()); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize()); try { druidDataSource.setFilters(properties.getFilters()); druidDataSource.init(); } catch (SQLException e) { e.printStackTrace(); } return druidDataSource; } /** * 配置 Druid 监控管理后台的Servlet; * 内置 Servler 容器时没有web.xml文件,所以使用 Spring Boot 的注册 Servlet 方式 * public ServletRegistrationBean druidServlet()相当于WebServlet配置。 */ @Bean @ConditionalOnMissingBean public ServletRegistrationBeandruidServlet(){ ServletRegistrationBean servletServletRegistrationBean = new ServletRegistrationBean (new StatViewServlet(), "/druid/*"); //白名单 servletServletRegistrationBean.addInitParameter("allow","127.0.0.1"); //表示只有本机可以访问,为空或者为null时,表示允许所有访问 //ip黑名单(存在共同时,deny优先于allow) //如果满足deny的话会提示,sorry, you are not permitted to view this page servletServletRegistrationBean.addInitParameter("deny","172.13.13.31"); //登录查看信息的账号和密码,用于登录Druid监控后台 servletServletRegistrationBean.addInitParameter("loginUsername","admin"); servletServletRegistrationBean.addInitParameter("loginPassword","admin"); //是否能重置数据 servletServletRegistrationBean.addInitParameter("resetEnable","true"); return servletServletRegistrationBean; } /** * 配置 Druid 监控 之 web 监控的 filter * WebStatFilter:用于配置Web和Druid数据源之间的管理关联监控统计 * public FilterRegistrationBean filterRegistrationBean()相当于Web Filter配置。 */ @Bean @ConditionalOnMissingBean public FilterRegistrationBean filterFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean (); bean.setFilter(new WebStatFilter()); //exclusions:设置哪些请求进行过滤排除掉,从而不进行统计 Map initParams = new HashMap<>(); initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(initParams); //"/*" 表示过滤所有请求 bean.setUrlPatterns(Collections.singletonList("/*")); return bean; }
DruidDataSourceProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.datasource.druid") //扫描配置类的属性前缀 public class DruidDataSourceProperties { // jdbc private String driverClassName; private String url; private String username; private String password; // jdbc connection pool private int initialSize; private int minIdle; private int maxActive = 100; private long maxWait; private long timeBetweenEvictionRunsMillis; private long minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private boolean poolPreparedStatements; private int maxPoolPreparedStatementPerConnectionSize; // filter private String filters; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getInitialSize() { return initialSize; } public void setInitialSize(int initialSize) { this.initialSize = initialSize; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getMaxActive() { return maxActive; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public long getMaxWait() { return maxWait; } public void setMaxWait(long maxWait) { this.maxWait = maxWait; } public long getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public long getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public String getValidationQuery() { return validationQuery; } public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public boolean isTestWhileIdle() { return testWhileIdle; } public void setTestWhileIdle(boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public boolean isTestOnReturn() { return testOnReturn; } public void setTestOnReturn(boolean testOnReturn) { this.testOnReturn = testOnReturn; } public boolean isPoolPreparedStatements() { return poolPreparedStatements; } public void setPoolPreparedStatements(boolean poolPreparedStatements) { this.poolPreparedStatements = poolPreparedStatements; } public int getMaxPoolPreparedStatementPerConnectionSize() { return maxPoolPreparedStatementPerConnectionSize; } public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) { this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; } public String getFilters() { return filters; } public void setFilters(String filters) { this.filters = filters; } }
配置日志管理
新建logback.xml文件
大家可以根据项目修改LOG_HOME的value值,还有开发环境建议把下面这个注释掉,使用控制台输出日志,方便定位,真实环境,把控制台注释挂掉,改为日志文件输出
logback.xml
<?xml version="1.0" encoding="UTF-8"?>data_server "LOG_HOME" value="wyq_log" /> "log.maxHistory" value="1" /> "log.lever" value="info" /> "fileError" class="ch.qos.logback.core.rolling.RollingFileAppender"> ${LOG_HOME}/log_error.log class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> ${LOG_HOME}/error/log-error-%d{yyyy-MM-dd}.%i.log ${log.maxHistory} class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 100MB true class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n utf-8 class="ch.qos.logback.classic.filter.LevelFilter"> error ACCEPT DENY "fileWarn" class="ch.qos.logback.core.rolling.RollingFileAppender"> ${LOG_HOME}/log_warn.log class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> ${LOG_HOME}/warn/log-warn-%d{yyyy-MM-dd}.%i.log ${log.maxHistory} class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 100MB true class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n utf-8 class="ch.qos.logback.classic.filter.LevelFilter"> warn ACCEPT DENY "fileInfo" class="ch.qos.logback.core.rolling.RollingFileAppender"> ${LOG_HOME}/log_info.log class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> ${LOG_HOME}/info/log-info-%d{yyyy-MM-dd}.%i.log ${log.maxHistory} class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 100MB true class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n utf-8 class="ch.qos.logback.classic.filter.LevelFilter"> info ACCEPT DENY "STDOUT" class="ch.qos.logback.core.ConsoleAppender"> %date{yyyy-MM-dd HH:mm:ss} | %highlight(%p) | %boldYellow(%c) | %M:%boldGreen(%L) | %m%n utf-8 "org.springframework" level="${log.lever}" /> "INFO"> ref ref="STDOUT" />
修改application.properties文件
在application.properties文件中,新增如下配置
#日志
logging.config=classpath:logback-spring.xml