本文介紹SpringBoot使用噹噹Sharding-JDBC進行讀寫分離。java
本文仍是基於噹噹網Sharding-Jdbc的依賴,與上一篇使用Sharding-Jdbc進行分庫分表依賴一致,而且本文大體內容與上一篇文章類似,建議先查看個人另外一篇在查看這篇會簡單許多,傳送門《SpringBoot使用Sharding-JDBC分庫分表》。mysql
這裏須要特殊介紹的是,使用Sharding-JDBC進行讀寫分離的時候,只容許設置一個主庫,從庫的話能夠設置多個,訪問策略的話從源碼上看只有兩種輪詢(ROUND_ROBIN)和隨機(RANDOM)。git
源碼代碼以下:web
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave;
public enum MasterSlaveLoadBalanceStrategyType {
ROUND_ROBIN(new RoundRobinMasterSlaveLoadBalanceStrategy()),
RANDOM(new RandomMasterSlaveLoadBalanceStrategy());
private final MasterSlaveLoadBalanceStrategy strategy;
public static MasterSlaveLoadBalanceStrategyType getDefaultStrategyType() {
return ROUND_ROBIN;
}
private MasterSlaveLoadBalanceStrategyType(MasterSlaveLoadBalanceStrategy strategy) {
this.strategy = strategy;
}
public MasterSlaveLoadBalanceStrategy getStrategy() {
return this.strategy;
}
}
複製代碼
因爲本地環境並無使用Mysql主從複製,只是建立了三個庫,其中database0做爲主庫,database1和database2做爲從庫。主庫進行增刪改操做,從庫進行查詢操做,以下圖爲本文數據庫的三個表。spring
如上圖分別是三個數據庫中的user表,其中master-user爲database0數據庫中的user表,salve-user1爲database1中的user表,salve-user2爲database2中的user表。sql
本文使用SpringBoot2.0.3,SpringData-JPA,Druid鏈接池,和噹噹的sharding-jdbc。數據庫
建立表和數據庫的SQL以下所示,這裏默認在從庫內分別插入了一條數據,name值分別存放dalaoyang1和dalaoyang2便於區分。apache
CREATE DATABASE database0;
USE database0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id bigint(64) not null,
city varchar(20) not null,
name varchar(20) not null,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE DATABASE database1;
USE database1;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id bigint(64) not null,
city varchar(20) not null,
name varchar(20) not null,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `database1`.`user`(`id`, `city`, `name`) VALUES (101, 'beijing', 'dalaoyang1');
CREATE DATABASE database2;
USE database2;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id bigint(64) not null,
city varchar(20) not null,
name varchar(20) not null,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `database2`.`user`(`id`, `city`, `name`) VALUES (102, 'beijing', 'dalaoyang2');
複製代碼
新建項目,依賴文件仍是噹噹的sharding-jdbc-core依賴和druid鏈接池,完整pom文件代碼以下所示。api
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalaoyang</groupId>
<artifactId>springboot2_shardingjdbc_dxfl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2_shardingjdbc_dxfl</name>
<description>springboot2_shardingjdbc_dxfl</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- sharding-jdbc -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
在配置信息中配置了三個數據庫的信息和JPA的簡單配置。springboot
##Jpa配置
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
##數據庫配置
##數據庫database0地址
database0.url=jdbc:mysql://localhost:3306/database0?characterEncoding=utf8&useSSL=false
##數據庫database0用戶名
database0.username=root
##數據庫database0密碼
database0.password=root
##數據庫database0驅動
database0.driverClassName=com.mysql.jdbc.Driver
##數據庫database0名稱
database0.databaseName=database0
##數據庫database1地址
database1.url=jdbc:mysql://localhost:3306/database1?characterEncoding=utf8&useSSL=false
##數據庫database1用戶名
database1.username=root
##數據庫database1密碼
database1.password=root
##數據庫database1驅動
database1.driverClassName=com.mysql.jdbc.Driver
##數據庫database1名稱
database1.databaseName=database1
##數據庫database2地址
database2.url=jdbc:mysql://localhost:3306/database2?characterEncoding=utf8&useSSL=false
##數據庫database1用戶名
database2.username=root
##數據庫database1密碼
database2.password=root
##數據庫database1驅動
database2.driverClassName=com.mysql.jdbc.Driver
##數據庫database1名稱
database2.databaseName=database2
複製代碼
上一篇文章說到在啓動類加入了@EnableAutoConfiguration去除數據庫自動配置,當時也沒太注意,其實能夠直接在@SpringBootApplication註解上去除數據庫自動配置,剩下的和上一篇同樣,使用@EnableTransactionManagement開啓事務,使用@EnableConfigurationProperties註解加入配置實體,啓動類完整代碼以下所示。
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableTransactionManagement(proxyTargetClass = true)
@EnableConfigurationProperties
public class Springboot2ShardingjdbcDxflApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2ShardingjdbcDxflApplication.class, args);
}
}
複製代碼
User實體類。
package com.dalaoyang.entity;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name="user")
@Data
public class User {
@Id
private Long id;
private String city;
private String name;
}
複製代碼
UserRepository類。
package com.dalaoyang.repository;
import com.dalaoyang.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User,Long> {
}
複製代碼
數據庫配置類,Database0Config。
package com.dalaoyang.database;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* @author yangyang
* @date 2019/1/30
*/
@Data
@ConfigurationProperties(prefix = "database0")
@Component
public class Database0Config {
private String url;
private String username;
private String password;
private String driverClassName;
private String databaseName;
public DataSource createDataSource() {
DruidDataSource result = new DruidDataSource();
result.setDriverClassName(getDriverClassName());
result.setUrl(getUrl());
result.setUsername(getUsername());
result.setPassword(getPassword());
return result;
}
}
複製代碼
數據庫配置類,Database1Config。
package com.dalaoyang.database;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* @author yangyang
* @date 2019/1/30
*/
@Data
@ConfigurationProperties(prefix = "database1")
@Component
public class Database1Config {
private String url;
private String username;
private String password;
private String driverClassName;
private String databaseName;
public DataSource createDataSource() {
DruidDataSource result = new DruidDataSource();
result.setDriverClassName(getDriverClassName());
result.setUrl(getUrl());
result.setUsername(getUsername());
result.setPassword(getPassword());
return result;
}
}
複製代碼
數據庫配置類,Database2Config。
package com.dalaoyang.database;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* @author yangyang
* @date 2019/1/30
*/
@Data
@ConfigurationProperties(prefix = "database2")
@Component
public class Database2Config {
private String url;
private String username;
private String password;
private String driverClassName;
private String databaseName;
public DataSource createDataSource() {
DruidDataSource result = new DruidDataSource();
result.setDriverClassName(getDriverClassName());
result.setUrl(getUrl());
result.setUsername(getUsername());
result.setPassword(getPassword());
return result;
}
}
複製代碼
建立一個DataSourceConfig類來設置讀寫分離,這裏其實也與分庫分表相似,也能夠在分庫分表的基礎上進行讀寫分離,須要建立一個Map集合來接收從庫。在建立數據源時須要傳入五個參數,分別是:
固然,也可使用其餘方法建立數據源,本文代碼以下:
package com.dalaoyang.database;
import com.dangdang.ddframe.rdb.sharding.api.MasterSlaveDataSourceFactory;
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.MasterSlaveLoadBalanceStrategyType;
import com.dangdang.ddframe.rdb.sharding.keygen.DefaultKeyGenerator;
import com.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* @author yangyang
* @date 2019/1/29
*/
@Configuration
public class DataSourceConfig {
@Autowired
private Database0Config database0Config;
@Autowired
private Database1Config database1Config;
@Autowired
private Database2Config database2Config;
@Bean
public DataSource getDataSource() throws SQLException {
return buildDataSource();
}
private DataSource buildDataSource() throws SQLException {
//設置從庫數據源集合
Map<String, DataSource> slaveDataSourceMap = new HashMap<>();
slaveDataSourceMap.put(database1Config.getDatabaseName(), database1Config.createDataSource());
slaveDataSourceMap.put(database2Config.getDatabaseName(), database2Config.createDataSource());
//獲取數據源對象
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource("masterSlave",database0Config.getDatabaseName()
,database0Config.createDataSource(), slaveDataSourceMap, MasterSlaveLoadBalanceStrategyType.getDefaultStrategyType());
return dataSource;
}
@Bean
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
}
複製代碼
Controller作爲測試類,建立兩個方法,save方法和getAll方法,其中:
UserController類以下所示。
package com.dalaoyang.controller;
import com.dalaoyang.entity.User;
import com.dalaoyang.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("save")
public void save(){
User user = new User();
user.setId(100L);
user.setName("dalaoyang");
user.setCity("beijing");
userRepository.save(user);
}
@GetMapping("getAll")
public Object getAll(){
return userRepository.findAll();
}
}
複製代碼
使用postman訪問http://localhost:8080/save,控制檯如圖所示。
再次訪問,如圖。
主鍵衝突了,其實這是因爲插入的時候使用的database0,可是查詢使用的是database1和database2,可是我在從庫內並無ID是100的數據,因此JPA斷定我爲插入,可是數據庫內缺有這樣的數據。
咱們接下來測試一下查詢。訪問http://localhost:8080/getAll
再次訪問,如圖。
證實從庫的讀取是正常的,接下來修改從庫的ID爲100。而後訪問http://localhost:8080/save,查看控制檯如圖。
由於存在了ID爲100的數據,因此SQL爲修改語句。