《深刻实践Spring Boot》第4章 提升数据库访问性能

第4章 提升数据库访问性能

使用关系型数据库的应用系统的性能瓶颈最终仍是数据库。随着业务的迅速增加,数据量会不断增大,会逐渐暴露出关系型数据库的弱点,即性能大幅降低。提高关系型数据库的访问性能是开发者的迫切任务。下面从程序开发角度,对提高数据库的访问性进行介绍和探讨。css

本章的实例工程使用了分模块的方式设计,各个模块的功能如表4-1所示。java

项目 工程 类型 功能
扩展功能模块 dpexpan 程序集成 JPA功能扩展和Redis配置等
数据管理模块 mysql 程序集成 MySQL实体建模和持久化等
Web应用模块 website Web应用 Web应用实例

4.1 使用Druid

Druid是一个关系型数据库链接池,它是阿里巴巴的一个开源项目。Druid支持全部JDBC兼容的数据库,包括Oracle、MySQL、Derby、PostgreSQL、SQL Server、H2等。Druid在监控、可扩展性、稳定性和性能方面具备明显的优点。经过Druid提供的监控功能,能够实时观察数据库链接池和SQL查询的工做状况。使用Druid链接池,在必定程度上能够提升数据库的访问性能。mysql

4.1.1 配置Druid依赖

能够从https://mvnrepository.com/中查找Druid的依赖配置,找到合适的版本,而后复制其中的Maven的配置到实例工程的扩展功能模块dpexpan中。图4-1是咱们查到的结果,使用的是1.0.18版本。图4-1中的HomePage是Druid的源代码连接地址。web

4.1.2 关于XML配置

使用Spring开发框架时,XML配置是常常使用的一种配置方法,其中数据源配置就是使用XML配置中的一种。代码清单4-1是一个使用Druid链接池的XML配置。使用Spring Boot框架也能使用XML配置,只要在程序入口使用一个注解,如@ImportResource({“classpath:spring-datasource.xml”}),便可导入XML配置。可是,Spring Boot不推荐这样使用,而是集中在配置文件application.properties或application.yml中进行配置。spring

4.1.3 Druid数据源配置

Spring Boot的数据源配置的默认类型是org.apache.tomcat.jdbc.pool.DataSource,为了使用Druid链接池,能够将数据源类型更改成com.alibaba.druid.pool.DruidDataSource,如代码清单4-2所示。其中,url、username、password是链接MySQL服务器的配置参数,其余一些参数设定Druid的工做方式。sql

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
    username: root
    password: 123456
    # 初始化大小,最小,最大
    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置获取链接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测须要关闭的空闲链接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个链接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 打开PSCache,而且指定每一个链接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 配置监控统计拦截的filters,去掉后监控界面sql没法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    # 经过connectProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    # 合并多个DruidDataSource的监控数据
    #useGlobalDataSourceStat=true

上面配置中的filters:stat表示已经能够使用监控过滤器,这时结合定义一个过滤器,就能够用来监控数据库的使用状况。数据库

注意:在Spring Boot低版本的数据源配置中,是没有提供设定数据源类型这一功能的,这时若是要使用上面这种配置方式,就须要使用自定义的配置参数来实现。apache

4.1.4 开启监控功能

开启Druid的监控功能,能够在应用运行的过程当中,经过监控提供的多维度数据来分析使用数据库的运行状况,从而能够调整程序设计,以优化数据库的访问性能。tomcat

代码清单4-3定义了一个监控服务器和一个过滤器,监控服务器设定了访问监控后台的地址为“/druid/*”,设定了访问数据库的白名单和黑名单,即经过访问者的IP地址来控制访问来源,增长了数据库的安全设置,还配置了一个用来登陆监控后台的用户druid,并将密码设置为123456。安全

package com.test.dbexpand;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfiguration {
    @Bean
    public ServletRegistrationBean statViewServle() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //白名单:
        servletRegistrationBean.addInitParameter("allow", "192.168.1.218,127.0.0.1");
        //IP黑名单 (存在共同时,deny优先于allow) : 若是知足deny的即提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
        //登陆查看信息的帐号密码.
        servletRegistrationBean.addInitParameter("loginUsername", "druid");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        //是否可以重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不须要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
相关文章
相关标签/搜索