4.0.0
com.example
demo
war
1.0-SNAPSHOT
demo Maven Webapp
UTF-8
1.8
1.0.24
5.1.27
1.4.5
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
de.codecentric
spring-boot-admin-starter-client
${spring-boot-admin.version}
org.springframework.boot
spring-boot-starter
com.alibaba
druid
${druid.version}
mysql
mysql-connector-java
${mysql.version}
org.springframework.boot
spring-boot-maven-plugin
org.springframework.boot
spring-boot-maven-plugin
org.springframework
springloaded
${basedir}/src/main/webapp/WEB-INF/lib/springloaded-1.2.5.RELEASE.jar
repackage
exec
###application.properties配置###
server.context-path=/springbootdemo
#数据库访问配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myblog
spring.datasource.username=root
spring.datasource.password=root
#数据源配置,初始化大小、最小、最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
#连接等待超时时间
spring.datasource.maxWait=60000
#配置隔多久进行一次检测(检测可以关闭的空闲连接)
spring.datasource.timeBetweenEvictionRunsMillis=60000
#配置连接在池中的最小生存时间
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
###方式一原生的servlet和filter方式###
编写Servlet类:
package com.example.web.servlet;
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* Created by Administrator on 2017/4/28.
*/
@WebServlet(urlPatterns = "/druid/*",
initParams = {
@WebInitParam(name = "allow", value = "192.168.10.25,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
@WebInitParam(name = "deny", value = "192.168.1.73"),// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name = "loginUsername", value = "admin"),// 用户名
@WebInitParam(name = "loginPassword", value = "123"),// 密码
@WebInitParam(name = "resetEnable", value = "false")// 禁用HTML页面上的“Reset All”功能)
}
)
public class DruidStatViewServlet extends StatViewServlet{
}
Filter类:
package com.example.web.filter;
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
* Created by Administrator on 2017/4/28.
*/
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
}
)
public class DruidWebStatFilter extends WebStatFilter{
}
然后,需要在Spring Boot启动类里设置Servlet自动扫描,不然会出现404页面找不到错误,使用@ServletComponentScan注解
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@ServletComponentScan
@EnableAsync
public class Application implements EmbeddedServletContainerCustomizer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(8087);
}
}
###方式二使用代码注册Servlet和Filter###
package com.example.config;
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;
/**
* Created by Administrator on 2017/4/28.
*/
@Configuration
public class DruidConfiguration {
/**
* 注册ServletRegistrationBean
* @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid1/*");
/** 初始化参数配置,initParams**/
//白名单
bean.addInitParameter("allow", "127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
bean.addInitParameter("deny", "192.168.1.73");
//登录查看信息的账号密码.
bean.addInitParameter("loginUsername", "admin2");
bean.addInitParameter("loginPassword", "123");
//是否能够重置数据.
bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 注册FilterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
bean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return bean;
}
}
###项目监控###
然后输入url访问,我的项目访问路径:http://localhost:8087/springbootdemo/druid/login.html,这个需要自己修改,我的Context配置为springbootdemo,端口配置为8087,这些可以参考我上一篇博客
通过平台进行监控