Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控

前言

图片监控css

做为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC做为 Java 基础内容,它提供了一种基准,据此能够构建更高级的工具和接口,使数据库开发人员可以编写数据库应用程序。下面演示下 Springboot 中如何使用 JDBC 操做,并配置使用 Druid 链接池,体验 Druid 对数据库操做强大的监控和扩展功能。Alibaba-Durid 官方手册点这里。java

1. 数据库准备

使用mysql数据库建立数据库 springboot,并在库中新建数据表 user 并新增两条信息。mysql

CREATE TABLE `user` (
  `id` int(11NOT NULL AUTO_INCREMENT,
  `age` int(11DEFAULT NULL,
  `birthday` datetime DEFAULT NULL,
  `password` varchar(32NOT NULL,
  `skills` varchar(255DEFAULT NULL,
  `username` varchar(32NOT NULL,
  PRIMARY KEY (`id`)
ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

# 新增数据
INSERT INTO `springboot`.`user`(`id``age``birthday``password``skills``username`VALUES (117'2019-01-12 21:02:30''123''Go''Darcy');
INSERT INTO `springboot`.`user`(`id``age``birthday``password``skills``username`VALUES (323'2019-01-01 00:11:22''456''Java''Chris');

2. 添加依赖

新建一个 Springboot项目,这里不说。添加依赖以下。git

    <dependencies>
        <!-- spring jdbc 操做模版 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- springboot web开发 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mysql 数据库链接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 引入druid数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

3. 配置数据源信息

常规的 JDBC 配置不须要配置这么多内容,这里由于使用了 Druid 链接池,因此配置了 Druid 部分。对自动配置不理解的能够查看系列文章Springboot 系列(二)Spring Boot 配置文件。github

spring:
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #   配置监控统计拦截的filters,去掉后监控界面sql没法统计,'wall'用于防火墙
    filters: stat
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

配置完毕以后,配置信息还不能绑定到 Druid数据源中,还须要新建一个配置类绑定数据源和配置信息。web

/**
 * <p>
 * Druid 数据源配置
 *
 * @Author niujinpeng
 * @Date 2019/1/14 22:20
 */

@Configuration
public class DruidConfig {
    /**
     * 配置绑定
     * @return
     */

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druid() {
        return new DruidDataSource();
    }
}

到这里,数据源已经配置完毕,编写测试方法测试 druid 链接池是否生效。spring

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDataJdbcApplicationTests {
    @Autowired
    DataSource dataSource;
    /**
     * 测试JDBC数据源
     * @throws SQLException
     */

    @Test
    public void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
}

运行看到 contextLoads 输出信息。sql

class com.alibaba.druid.pool.DruidDataSource
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2019-02-27 14:14:56.144  INFO 12860 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3e104d4b

输出日志中的 com.alibaba.druid 说明 Druid 已经生效。数据库

4. 使用 Spring-JDBC

传统的 JDBC 使用中,须要编写大量代码,从构造 PreparedStatement 到查询不胜其烦。面对这样的开发痛点,Spring 封装了 Spring-jdbc. 让咱们使用 JdbcTemplate 便可轻松的操做数据库。Spring-jdbc 的详细使用不是这篇文章重点,只简单演示下是否生效。 
编写控制器,查询一个 user 信息。springboot

@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @ResponseBody
    @GetMapping("/query")
    public Map<String, Object> map() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM user");
        return list.get(0);
    }
}

启动spring 项目,请求 /query 接口获得正常响应。

{
"id"1,
"age"17,
"birthday""2019-01-12T13:02:30.000+0000",
"password""123",
"skills""Go",
"username""Darcy"
}

可见 Spring-JDBC 已经从数据库中取出了数据信息。

5. 使用 Druid 监控

若是使用 Druid 链接池却不使用监控功能,那么就有点暴殄天物了。下面开始配置 Druid 的 SQL 监控功能。在上面写的 DruidConfig 配置类中增长配置 Druid 的 Servlet 和 Filter.

     /**
     * Druid的servlet
     * @return
     */

    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet());
        Map<String, String> initParams = new HashMap<>();
        initParams.put("loginUsername""admin");
        initParams.put("loginPassword""123");
        initParams.put("allow","127.0.0.1");
        bean.setInitParameters(initParams);
        bean.setUrlMappings(Arrays.asList("/druid/*"));
        return bean;
    }
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter());
        HashMap<String, String> initParams = new HashMap<>();
        initParams.put("exclusions""/css,/druid/*");
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

上面配置了 Druid 监控访问路径为 /druid、登陆用户是 admin、登陆密码是123、容许访问的IP是127.0.0.1 本机、不须要监控的请求是 /css 和 /druid 开头的请求。

从新启动项目,访问测试 /query,而后访问 /durid 登陆页。

图片Druid 登陆页

登陆后能够看到 SQL 监控信息和 URL 监控等信息。

图片SQL 监控


URL 监控。

图片URL 监控

文章代码已经上传到 GitHub Spring Boot(https://github.com/niumoo/springboot/tree/master/springboot-data-jdbc)。

相关文章
相关标签/搜索