您好,登錄后才能下訂單哦!
Sharding JDBC的操作分為配置使用、讀寫分離、分庫分表以及應(yīng)用等,今天我們主要來了解一下關(guān)于分庫分表的操作,如果你對此感興趣的話,那我們就開始吧。
環(huán)境準(zhǔn)備
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version></parent>
<properties>
<java.version>1.8</java.version>
<sharding.version>3.1.0</sharding.version></properties>
<dependencies>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding.version}</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency></dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins></build>
domain
// 建立domain@Setter@Getter@ToString@NoArgsConstructor@AllArgsConstructorpublic class Employee {
private Long id;
private String name;}
配置類
@SpringBootApplication@MapperScan("cn.wolfcode.sharding.mapper")public class ShardingApplication { }
分庫分表
案例模型
把數(shù)據(jù)分別存放在兩臺服務(wù)器的兩個數(shù)據(jù)庫中表,通過分片算法來決定當(dāng)前的數(shù)據(jù)存放在哪個數(shù)據(jù)庫的哪個表中,由于一個連接池只能連接一個特定的數(shù)據(jù)庫,所以這里需要創(chuàng)建多個連接池對象
建表
-- 分別在2臺服務(wù)器中建立數(shù)據(jù)庫sharding,并且建表employee_0和employee_1CREATE TABLE employee_0
(id
bigint(20) PRIMARY KEY AUTO_INCREMENT,name
varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ###################################CREATE TABLE employee_1
(id
bigint(20) PRIMARY KEY AUTO_INCREMENT,name
varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.properties
sharding.jdbc.datasource.names=db0,db1
sharding.jdbc.datasource.db0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db0.url=jdbc:mysql://db0Ip:port/sharing
sharding.jdbc.datasource.db0.username=xxx
sharding.jdbc.datasource.db0.password=xxx
sharding.jdbc.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db1.url=jdbc:mysql://db1Ip:port/sharing
sharding.jdbc.datasource.db1.username=xxx
sharding.jdbc.datasource.db1.password=xxx
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=db$->{id % 2}
sharding.jdbc.config.sharding.binding-tables=employee
sharding.jdbc.config.sharding.tables.employee.actual-data-nodes=db$->{0..1}.employee$->{0..1}
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.algorithm-expression=employee$->{id % 2}
sharding.jdbc.config.sharding.tables.employee.key-generator-column-name=id
sharding.jdbc.config.props.sql.show=true
mapper
/**
底層會根據(jù)分片規(guī)則,把我們寫的邏輯表改寫為數(shù)據(jù)庫中的真實(shí)表
/@Mapperpublic interface EmployeeMapper {
@Select("select from employee")
List<Employee> selectAll();
@Insert("insert into employee (name) values (#{name})")
void inser(Employee entity);}
測試
@RunWith(SpringRunner.class)@SpringBootTest(classes=ShardingApplication.class)public class ShardingApplicationTests {
@Autowired
private EmployeeMapper employeeMapper;
@Test
public void save() {
for (int i = 0; i < 10; i++) {
Employee employee = new Employee();
employee.setName("xx"+i);
employeeMapper.inser(employee);
}
}
@Test
public void list() {
employeeMapper.selectAll().forEach(System.out::println);
}}
優(yōu)缺點(diǎn)
?拆分后單表數(shù)據(jù)量比較小,單表大數(shù)據(jù)被拆分,解決了單表大數(shù)據(jù)訪問問題
?分表以什么切分如果弄的不好,導(dǎo)致多次查詢,而且有時候要跨庫操作,甚至導(dǎo)致join無法使用,對排序分組等有性能影響
?之前的原子操作被拆分成多個操作,事務(wù)處理變得復(fù)雜
?多個DB維護(hù)成本增加
看完這些操作后不妨自己去試試,實(shí)踐才能檢驗(yàn)真知,如果遇到了問題,也可以及時向我詢問,我也會盡我所力幫助你。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。