Project address: github.com/shizhenshua…
Development environment:
jdk
: JDK1.8 +gradle
: Gradle4.6 +Spring
2.1.3. RELEASE +
When the database data reaches a certain number, the response of the database will be slow, and some measures will be taken. For example, read and write separation, table and library separation, master-slave services, caching technology and so on. Here is the use of multi – library multi – table. Alter table user_info from test
Alter table test2 user_info
Import dependence
-
Compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.3.RELEASE'
-
Compile group: 'org.springframework.boot', name: 'spring-boot-starter-JDBC ', version: '2.1.3.RELEASE'
-
CompileOnly Group: 'org. projectLombok ', name: 'lombok', version: '1.18.6'
-
Compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
-
Compile group: 'org.mybatis. Generator ', name: 'mybatis-generator-core', version: '1.3.5'
-
Compile group: 'tk.mybatis', name: 'mapper', version: '4.0.2'
-
Compile group: 'org.mybatis. Spring. boot', name: 'mybatis-spring-boot-starter', version: '2.0.0'
HikariCP has been introduced in Spring-boot-starter-JDBC (spring default data source)
The project structure
The configuration file configures (application.yaml) default data source information
spring:
datasource:
Url: JDBC: mysql: / / 127.0.0.1:3306 / test? useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&allowMultiQuer ies=true
username: root
password: admin
sql-script-encoding: UTF-8
Read the configuration information DataSourceConfig. Java to create the default data source
-
package utry.hikaricp.config;
-
import com.zaxxer.hikari.HikariDataSource;
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.transaction.annotation.EnableTransactionManagement;
-
import utry.hikaricp.DataSourceProvider;
-
import utry.hikaricp.info.DataSourceInfo;
-
import utry.hikaricp.source.MyHikariDataSource;
-
@Configuration
-
@EnableTransactionManagement
-
publicclassDataSourceConfig{
-
@Value("${spring.datasource.url}")
-
privateString url;
-
@Value("${spring.datasource.username}")
-
privateString userName;
-
@Value("${spring.datasource.password}")
-
privateString password;
-
@Bean
-
@ConditionalOnMissingBean(MyHikariDataSource.class)
-
publicMyHikariDataSource dataSource() {
-
DataSourceInfo info = newDataSourceInfo();
-
info.setUrl(url);
-
info.setUsername(userName);
-
info.setPassword(password);
-
HikariDataSource dataSource = DataSourceProvider.create(info);
-
MyHikariDataSource hikariCPDataSource = newMyHikariDataSource();
-
hikariCPDataSource.updateDataSourceMap("1", dataSource);
-
return hikariCPDataSource;
-
}
-
}
Create a HikariDataSource using the DataSourceProvider
-
package utry.hikaricp;
-
import com.zaxxer.hikari.HikariConfig;
-
import com.zaxxer.hikari.HikariDataSource;
-
import utry.hikaricp.info.DataSourceInfo;
-
publicclassDataSourceProvider{
-
publicstaticHikariDataSource create(DataSourceInfo sourceInfo) {
-
HikariConfig hikariConfig = newHikariConfig();
-
hikariConfig.setUsername(sourceInfo.getUsername());
-
hikariConfig.setPassword(sourceInfo.getPassword());
-
hikariConfig.setJdbcUrl(sourceInfo.getUrl());
-
hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
-
returnnewHikariDataSource(hikariConfig);
-
}
-
}
The core data source class is MyHikariDataSource. Java
-
package utry.hikaricp.source;
-
import com.zaxxer.hikari.HikariDataSource;
-
import org.springframework.jdbc.datasource.AbstractDataSource;
-
import javax.sql.DataSource;
-
import java.io.Closeable;
-
import java.io.IOException;
-
import java.sql.Connection;
-
import java.sql.SQLException;
-
import java.util.HashMap;
-
import java.util.Map;
-
publicclassMyHikariDataSourceextendsAbstractDataSource{
-
privatestaticMap<String, HikariDataSource> dataSourceMap = newHashMap<>(1);
-
@Override
-
publicConnection getConnection() throwsSQLException{
-
return dataSourceMap.get("1").getConnection();
-
}
-
@Override
-
publicConnection getConnection(String username, String password) throwsSQLException{
-
return dataSourceMap.get("1").getConnection(username, password);
-
}
-
privatevoid destroy(){
-
DataSource dataSource = dataSourceMap.get("1");
-
Closeable closeable = (Closeable) dataSource;
-
if(closeable ! = null) {
-
try{
-
closeable.close();
-
} catch(IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
publicvoid updateDataSourceMap(String key, HikariDataSource value) {
-
destroy();
-
dataSourceMap.put(key, value);
-
}
-
}
Change the HikariDataSource in the DataSourceMap collection of this class to a new HikariDataSource (). Key =1 (key=1)
Entity class the UserInfo. Java
-
package utry.hikaricp.model;
-
publicclassUserInfo{
-
privateInteger id;
-
privateString remarks;
-
publicUserInfo() {
-
}
-
publicUserInfo(Integer id, String remarks) {
-
this.id = id;
-
this.remarks = remarks;
-
}
-
publicInteger getId() {
-
return id;
-
}
-
publicvoid setId(Integer id) {
-
this.id = id;
-
}
-
publicString getRemarks() {
-
return remarks;
-
}
-
publicvoid setRemarks(String remarks) {
-
this.remarks = remarks;
-
}
-
}
Hikaricpcontroller.java:
-
Update data source
-
Querying User Information
-
package utry.hikaricp.controller;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
import utry.hikaricp.factory.HikariCPDataSourceFactory;
-
import utry.hikaricp.mapper.UserInfoMapper;
-
import utry.hikaricp.model.UserInfo;
-
@RestController
-
publicclassHikariCpController{
-
@Autowired
-
privateUserInfoMapper mapper;
-
@Autowired
-
privateHikariCPDataSourceFactory factory;
-
@RequestMapping("update")
-
publicvoid info(String username, String password, String url) {
-
factory.reload(username, password, url);
-
}
-
@RequestMapping("get")
-
publicUserInfoget() {
-
try{
-
UserInfo userInfo = mapper.selectByPrimaryKey(1);
-
return userInfo;
-
} catch(Exception e) {
-
e.printStackTrace();
-
}
-
returnnull;
-
}
-
}
Start the project
1. Call the get interface and return data from the test library
Update request to update the data source
After the call succeeds, you can see that the previous data source has been shutdown and a new data source has been initialized
3. Call the GET request again, and you can see the data from the test2 database