SpringBoot进阶教程 | 第三篇:整合Druid连接池以及Druid监控

这篇文篇将介绍,如何通过SpringBoot整合Druid数据库链接池,实时监控数据库链接信息,为优化数据库性能提供更好的指导,同样将通过YML配置文件形式进行配置,方便简洁。

准备工作

环境:

windows
jdk 8
maven 3.0
IDEA

创建数据库表

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sno` int(15) NOT NULL,
  `sname` varchar(50) DEFAULT NULL,
  `sex` char(2) DEFAULT NULL,
  `dept` varchar(25) DEFAULT NULL,
  `birth` date DEFAULT NULL,
  `age` int(3) DEFAULT NULL,
  PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '李同学', '1', '王同学学习成绩很不错', '2010-07-22', '17');

构建工程


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>

	<parent>
		<groupId>cn.zhangboxgroupId>
		<artifactId>spring-boot-studyartifactId>
		<version>1.0-SNAPSHOTversion>
	parent>

	<groupId>cn.zhangboxgroupId>
	<artifactId>spring-boot-druidartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<packaging>jarpackaging>

	<name>spring-boot-druidname>
	<description>this project for Spring Bootdescription>



	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
		
		<commons-lang3.version>3.4commons-lang3.version>
		<commons-codec.version>1.10commons-codec.version>
		<mybatis-spring-boot.version>1.2.0mybatis-spring-boot.version>
		<lombok.version>1.16.14lombok.version>
		<fastjson.version>1.2.41fastjson.version>
		<druid.version>1.1.2druid.version>
	properties>

	<repositories>
		
		<repository>
			<id>aliyunmavenid>
			<url>http://maven.aliyun.com/nexus/content/groups/public/url>
		repository>
	repositories>

	<dependencies>

		
		<dependency>
			<groupId>org.mybatis.spring.bootgroupId>
			<artifactId>mybatis-spring-boot-starterartifactId>
			<version>${mybatis-spring-boot.version}version>
		dependency>
		

		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		

		
		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<scope>runtimescope>
		dependency>
		

		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
		

		
		<dependency>
			<groupId>org.apache.commonsgroupId>
			<artifactId>commons-lang3artifactId>
			<version>${commons-lang3.version}version>
		dependency>
		<dependency>
			<groupId>commons-codecgroupId>
			<artifactId>commons-codecartifactId>
			<version>${commons-codec.version}version>
		dependency>
		

		
		<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>fastjsonartifactId>
			<version>${fastjson.version}version>
		dependency>
		

		
		<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>druid-spring-boot-starterartifactId>
			<version>${druid.version}version>
		dependency>
		

		
		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<version>${lombok.version}version>
		dependency>
		
	dependencies>

	<build>
		<finalName>spring-boot-druidfinalName>
		<plugins>
		<plugin>
			<groupId>org.apache.maven.pluginsgroupId>
			<artifactId>maven-compiler-pluginartifactId>
			<version>3.5.1version>
			<configuration>
				<source>1.8source>
				<target>1.8target>
				<encoding>UTF-8encoding>
			configuration>
		plugin>
		<plugin>
			<groupId>org.apache.maven.pluginsgroupId>
			<artifactId>maven-surefire-pluginartifactId>
			<version>2.19.1version>
		plugin>

		<plugin>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-maven-pluginartifactId>
			<dependencies>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>springloadedartifactId>
					<version>1.2.4.RELEASEversion>
				dependency>
			dependencies>
			<configuration>
				<mainClass>cn.zhangbox.admin.SpringBootDruidApplicationmainClass>
				<jvmArguments>-Dfile.encoding=UTF-8 -Xdebug
					-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
				jvmArguments>
				<executable>trueexecutable>
				<fork>truefork>
			configuration>
		plugin>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
				<configuration>
					<mainClass>cn.zhangbox.admin.SpringBootDruidApplicationmainClass>
					<jvmArguments>-Dfile.encoding=UTF-8 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005jvmArguments>
					<executable>trueexecutable>
					<fork>truefork>
				configuration>
			plugin>
		plugins>
	build>

project>

注意:这里引入了lombok插件节省编写实体类时候写getset方法,这里在idea中进行setget操作需要下载lombok插件,在设置页面的plugins中搜索lombok插件在中央插件库下载后重启idea即可,更详细的lombok使用教程可以查考:

程序员DD的lombok系列教程:

Lombok:让JAVA代码更优雅

修改YML配置

#公共配置
server:
    port: 80
    tomcat:
      uri-encoding: UTF-8
spring:
  #激活哪一个环境的配置文件
  profiles:
    active: dev
  #连接池配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 配置测试查询语句
      validationQuery: SELECT 1 FROM DUAL
      # 初始化大小,最小,最大
      initialSize: 10
      minIdle: 10
      maxActive: 200
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 180000
      testOnBorrow: false
      testWhileIdle: true
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: true
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxOpenPreparedStatements: 100
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,log4j
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

#mybatis
mybatis:
  # 实体类扫描
  type-aliases-package: cn.zhangbox.springboot.entity
  # 配置映射文件位置
  mapper-locations: classpath:mapper/*.xml
  # 开启驼峰匹配
  mapUnderscoreToCamelCase: true

---
#开发环境配置
server:
  #端口
  port: 8080
spring:
  profiles: dev
  # 数据源配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf8&useSSL=false&tinyInt1isBit=true
    username: root
    password: 123456
#日志
logging:
  config: classpath:log/logback.xml
  path: log/spring-boot-druid

---
#测试环境配置
server:
  #端口
  port: 80
spring:
  profiles: test
  # 数据源配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/spring-boot-druid?useUnicode=true&characterEncoding=utf8&useSSL=false&tinyInt1isBit=true
    username: root
    password: 123456
#日志
logging:
  config: classpath:log/logback.xml
  path: /home/log/spring-boot-druid

---
#生产环境配置
server:
  #端口
  port: 8080
spring:
  profiles: prod
  # 数据源配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/spring-boot-druid?useUnicode=true&characterEncoding=utf8&useSSL=false&tinyInt1isBit=true
    username: root
    password: 123456
#日志
logging:
  config: classpath:log/logback.xml
  path: /home/log/spring-boot-druid

这里进行了mybatis整合,如果不会mybatis整合可以参考我写的这篇文章:

SpringBoot非官方教程 | 第六篇:SpringBoot整合mybatis

创建日志配置文件

在工程resources文件夹下新建文件夹log,并在该文件夹下创建logback.xml文件,加入以下配置:


<configuration scan="true" scanPeriod="10 seconds">
    
    

    
    <property name="APP_DIR" value="spring-boot-druid" />

    
    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}Pattern>
            <charset>UTF-8charset> 
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>infolevel>
        filter>
    appender>


    
    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/log_debug.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/debug/log-debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>debuglevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/log_info.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/info/log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>infolevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/log_warn.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/warn/log-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warnlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/log_error.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>errorlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    <logger name="org.springframework.web" level="info"/>
    <logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/>
    <logger name="cn.zhangbox.springboot" level="debug"/>

    
    <springProfile name="dev">
        <root level="info">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="DEBUG_FILE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="WARN_FILE" />
            <appender-ref ref="ERROR_FILE" />
        root>
    springProfile>

    
    <springProfile name="test">
        <root level="info">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="WARN_FILE" />
            <appender-ref ref="ERROR_FILE" />
        root>
    springProfile>

    
    <springProfile name="prod">
        <root level="info">
            <appender-ref ref="DEBUG_FILE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="ERROR_FILE" />
        root>
    springProfile>

configuration>

注意:loback配置文件中

<logger name="cn.zhangbox.springboot" level="debug"/>

name的属性值一定要是当前工程的java代码的完整目录,因为mybatis打印的日志级别是debug级别的,因此需要配置debug级别日志扫描的目录。

创建Druid配置类

在工程java代码目录下创建config的目录在下面创建DruidDBConfig类加入以下代码:

@Configuration
public class DruidDBConfig {

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        //设置控制台管理用户
        reg.addInitParameter("loginUsername","root");
        reg.addInitParameter("loginPassword","root");
        // 禁用HTML页面上的“Reset All”功能
        reg.addInitParameter("resetEnable","false");
        //reg.addInitParameter("allow", "127.0.0.1"); //白名单
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<String, String>();
        //忽略过滤的形式
        initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}

**注意:**这里ServletRegistrationBean 配置bean中通过addInitParameter设置了管控台的用户名密码都是root,可以在这里进行自定义配置也可以将这里的用户名密码通过转移数据库进行定制化配置实现。

创建实体

在工程java代码目录下创建entity的目录在下面创建Student类加入以下代码:

@Data
@EqualsAndHashCode(callSuper = false)
public class Student {

    private static final long serialVersionUID = 1L;

	/**
     * 主键id
     */
	private Integer sno;
    /**
     * 学生姓名
     */
	private String sname;
    /**
     * 性别
     */
	private String sex;
    /**
     * 生日
     */
	private String birth;
    /**
     * 年龄
     */
	private String age;
    /**
     * 简介
     */
	private String dept;

}

创建Controller

在工程java代码目录下创建controller的目录在下面创建StudentConteroller类加入以下代码:

@Controller
@RequestMapping("/student")
public class StudentConteroller {
    private static final Logger LOGGER = LoggerFactory.getLogger(StudentConteroller.class);

    @Autowired
    protected StudentService studentService;

    /**
     * 查询所有的学生信息
     *
     * @param sname
     * @param age
     * @param modelMap
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public String list(String sname, Integer age, ModelMap modelMap) {
        String json = null;
        try {
            List<Student> studentList = studentService.getStudentList(sname, age);
            modelMap.put("ren_code", "0");
            modelMap.put("ren_msg", "查询成功");
            modelMap.put("studentList", studentList);
            json = JSON.toJSONString(modelMap);
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.put("ren_code", "0");
            modelMap.put("ren_msg", "查询失败===>" + e);
            LOGGER.error("查询失败===>" + e);
            json = JSON.toJSONString(modelMap);
        }
        return json;
    }
}

创建Service

在工程java代码目录下面创建service目录在下面创建StudentService类加入以下代码:

public interface StudentService {

    /**
     * 查询所有的学生信息
     *
     * @param sname
     * @param age
     * @return
     */
    List<Student> getStudentList(String sname, Integer age);
}

创建ServiceImpl

在工程java代码目录下的service的目录下面创建impl目录在下面创建StudentServiceImpl类加入以下代码:

@Service("StudentService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {

	@Autowired
	StudentDao studentDao;

	@Override
	public List<Student> getStudentList(String sname, Integer age) {
		return studentDao.getStudentList(sname,age);
	}
}

创建Dao

在工程java代码目录下创建dao的目录在下面创建StudentDao类加入以下代码:

public interface StudentDao {

	List<Student> getStudentList(@Param("sname")String sname, @Param("age")Integer age);

}

创建Mapper映射文件

在工程resource目录下创建mapper的目录在下面创建StudentMapper.xml映射文件加入以下代码:



<mapper namespace="cn.zhangbox.springboot.dao.StudentDao" >
    
    <select id="getStudentList" resultType="cn.zhangbox.springboot.entity.Student">
        SELECT
            s.sno,
            s.sname,
            s.sex,
            s.dept,
            s.birth,
            s.age
        FROM
            student s
        WHERE
        1 = 1
        <if test="sname != null">
            and s.sname = #{sname}
        if>
        <if test="age != null">
            and s.age = #{age}
        if>
    select>
mapper>

创建启动类

@SpringBootApplication
@MapperScan("cn.zhangbox.springboot.dao")//配置mybatis接口包扫描
public class SpringBootDruidApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDruidApplication.class, args);
	}
}

启动项目进行测试:

控制台打印

    .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2018-07-07 10:54:52.404  INFO 24084 --- [           main] c.z.s.SpringBootDruidApplication         : Starting SpringBootDruidApplication on HP with PID 24084 (C:\Users\Administrator\Desktop\spring-boot-study\spring-boot-druid\target\classes started by Administrator in C:\Users\Administrator\Desktop\spring-boot-study)
2018-07-07 10:54:52.445  INFO 24084 --- [           main] c.z.s.SpringBootDruidApplication         : The following profiles are active: dev
2018-07-07 10:54:52.570  INFO 24084 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53142455: startup date [Sat Jul 07 10:54:52 CST 2018]; root of context hierarchy
2018-07-07 10:54:52.997  INFO 24084 --- [kground-preinit] o.h.validator.internal.util.Version      : HV000001: Hibernate Validator 5.3.5.Final
2018-07-07 10:54:53.736  INFO 24084 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'filterRegistrationBean' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=druidDBConfig; factoryMethodName=filterRegistrationBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [cn/zhangbox/springboot/config/DruidDBConfig.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.alibaba.druid.spring.boot.autoconfigure.stat.DruidWebStatFilterConfiguration; factoryMethodName=filterRegistrationBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/stat/DruidWebStatFilterConfiguration.class]]
2018-07-07 10:54:54.887  INFO 24084 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-07-07 10:54:54.903  INFO 24084 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2018-07-07 10:54:54.905  INFO 24084 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.14
2018-07-07 10:54:55.123  INFO 24084 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-07-07 10:54:55.124  INFO 24084 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2554 ms
2018-07-07 10:54:55.522  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'statViewServlet' to [/druid/*]
2018-07-07 10:54:55.525  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-07-07 10:54:55.525  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'statViewServlet' to [/druid/*]
2018-07-07 10:54:55.526  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet statViewServlet was not registered (possibly already registered?)
2018-07-07 10:54:55.531  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-07 10:54:55.531  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-07 10:54:55.532  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-07 10:54:55.533  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-07 10:54:55.533  INFO 24084 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webStatFilter' to urls: [/*]
2018-07-07 10:54:57.216  INFO 24084 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53142455: startup date [Sat Jul 07 10:54:52 CST 2018]; root of context hierarchy
2018-07-07 10:54:57.322  INFO 24084 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/student/list],methods=[GET]}" onto public java.lang.String cn.zhangbox.springboot.controller.StudentConteroller.list(java.lang.String,java.lang.Integer,org.springframework.ui.ModelMap)
2018-07-07 10:54:57.326  INFO 24084 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-07 10:54:57.328  INFO 24084 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-07 10:54:57.376  INFO 24084 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-07 10:54:57.376  INFO 24084 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-07 10:54:57.451  INFO 24084 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-07 10:54:58.066  INFO 24084 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-07 10:54:58.068  INFO 24084 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'statFilter' has been autodetected for JMX exposure
2018-07-07 10:54:58.069  INFO 24084 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-07-07 10:54:58.077  INFO 24084 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.alibaba.druid.spring.boot.autoconfigure:name=dataSource,type=DruidDataSourceWrapper]
2018-07-07 10:54:58.081  INFO 24084 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'statFilter': registering with JMX server as MBean [com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter]
2018-07-07 10:54:58.101  INFO 24084 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Initializing ProtocolHandler ["http-nio-8080"]
2018-07-07 10:54:58.118  INFO 24084 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Starting ProtocolHandler ["http-nio-8080"]
2018-07-07 10:54:58.145  INFO 24084 --- [           main] o.a.tomcat.util.net.NioSelectorPool      : Using a shared selector for servlet write/read
2018-07-07 10:54:58.172  INFO 24084 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-07-07 10:54:58.179  INFO 24084 --- [           main] c.z.s.SpringBootDruidApplication         : Started SpringBootDruidApplication in 7.666 seconds (JVM running for 10.386)
2018-07-07 11:00:00.245  INFO 24084 --- [nio-8080-exec-5] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-07 11:00:00.246  INFO 24084 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-07 11:00:00.302  INFO 24084 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 56 ms
2018-07-07 11:00:01.606  INFO 24084 --- [nio-8080-exec-5] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited

浏览器请求测试

图片.png

本地日志打印效果

SpringBoot进阶教程 | 第三篇:整合Druid连接池以及Druid监控_第1张图片
图片.png

2018-07-07 10:54:52.439 [main] DEBUG cn.zhangbox.springboot.SpringBootDruidApplication - Running with Spring Boot v1.5.3.RELEASE, Spring v4.3.8.RELEASE
2018-07-07 11:00:01.715 [http-nio-8080-exec-5] DEBUG c.z.springboot.dao.StudentDao.getStudentList - ==>  Preparing: SELECT s.sno, s.sname, s.sex, s.dept, s.birth, s.age FROM student s WHERE 1 = 1 
2018-07-07 11:00:01.881 [http-nio-8080-exec-5] DEBUG c.z.springboot.dao.StudentDao.getStudentList - ==> Parameters: 
2018-07-07 11:00:01.931 [http-nio-8080-exec-5] DEBUG c.z.springboot.dao.StudentDao.getStudentList - <==      Total: 2

这里使用logback配置中将不同级别的日志设置了在不同文件中打印,这样很大程度上方便项目出问题查找问题。

Druid管控台

SpringBoot进阶教程 | 第三篇:整合Druid连接池以及Druid监控_第2张图片
SpringBoot进阶教程 | 第三篇:整合Druid连接池以及Druid监控_第3张图片
SpringBoot进阶教程 | 第三篇:整合Druid连接池以及Druid监控_第4张图片
SpringBoot进阶教程 | 第三篇:整合Druid连接池以及Druid监控_第5张图片

源码地址

Spring Boot整合Druid连接池以及Druid监控

写在最后

欢迎关注喜欢、和点赞后续将推出更多的学习文章,敬请期待。
欢迎关注我的微信公众号获取更多更全的学习资源,视频资料,技术干货!
欢迎扫码关注资源领取方式
公众号回复“学习”,拉你进程序员技术讨论群干货资源第一时间分享。

公众号回复“视频”,领取800GJava视频学习资源。

公众号回复“全栈”,领取1T前端Java产品经理微信小程序Python等资源合集大放送。

公众号回复“慕课”,领取1T慕课实战学习资源。

公众号回复“实战”,领取750G项目实战学习资源。

公众号回复“面试”,领取20G面试实战学习资源。

QQ群:

QQ交流学习群

QQ群中你可以获得的福利有:

1.最全的学习资源,最多的学习大佬!
2.最全的学习升级帮助,帮助你快速打怪升级。
3.最重要是提供就业指导以及兼职机会!

你可能感兴趣的:(SpringBoot,技术提升,Yaml)