table of Contents
Chapter 4 Improving database access performance
Use relational database application system performance bottleneck eventually database. With the rapid growth of the business, the amount of data will continue to increase, gradually expose weaknesses in relational databases, namely serious performance degradation. Urgent task is to enhance the performance of the developer's access relational databases. The following procedures from the perspective of development, to enhance the accessibility of the database will be presented and discussed.
Examples of the use of sub-project for this embodiment of module design, function of each module as shown in Table 4-1.
project | engineering | Types of | Features |
---|---|---|---|
Extended function module | dpexpan | Process Integration | JPA extensions and Redis configuration, etc. |
Data management module | mysql | Process Integration | MySQL solid modeling and persistence, etc. |
Web application module | website | Web Applications | Web application example |
4.1 Use Druid
Druid is a relational database connection pool, it is Alibaba's an open source project. Druid supports all JDBC-compliant databases, including Oracle, MySQL, Derby, PostgreSQL, SQL Server, H2 and so on. Druid has obvious advantages in monitoring, scalability, stability and performance. Monitoring functions provided by the Druid, real-time database connection pool and observe the work of the SQL query. Use Druid connection pool, to a certain extent, can improve the performance of database access.
4.1.1 Configuration Druid dependence
From https://mvnrepository.com/ find dependent configuration Druid to find the appropriate version, and then copy them to the Maven configuration examples of project expansion modules dpexpan in. Figure 4-1 shows the results we found, using the 1.0.18 version. FIG 4-1 HomePage is Druid source link address.
4.1.2 About the XML configuration
When using the Spring development framework, XML configuration is a configuration often used, wherein the data source is configured using an XML configuration. Listing 4-1 is a use XML Druid connection pool configuration. Spring Boot Framework using XML configuration can also be used, as long as the program entry using an annotation, such as @ImportResource ({ "classpath: spring-datasource.xml"}), can be introduced into the XML configuration. However, Spring Boot does not recommend such use, but concentrated in a configuration file to configure application.properties or application.yml in.
4.1.3 Druid data source configuration
Spring Boot default type of data source is arranged org.apache.tomcat.jdbc.pool.DataSource, in order to use the connection pool Druid, the data source can change the type of com.alibaba.druid.pool.DruidDataSource, as Listing 4- 2 shown in FIG. Which, url, username, password is to connect MySQL server configuration parameters, other parameters set Druid's work.
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
The above configuration filters: stat monitor indicates ready to use filters, in combination define a time filter, can be used to monitor the use of the database.
Note: In the version of the low Spring Boot data source configuration, there is no set of data sources to provide this type of function, then if you want to use this configuration above, it is necessary to use a custom configure the parameters to achieve.
4.1.4 open monitoring
Open Druid monitoring function, you can process the application is running, multi-dimensional data provided by the monitoring operation to analyze the use of the database, which can adjust the program designed to optimize access to database performance.
Listing 4-3 defines a monitoring server and a filter, monitor server access control address set the background for the "/ druid / *", set the white and black lists to access the database, namely by visitors IP addresses to control access to the source, increasing the security settings of the database, it is also equipped with a druid user used to log in to monitor the background, and set the password is 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;
}
}