您好,登錄后才能下訂單哦!
作為輕量級java框架,sharding JDBC在Java的jdbc層提供了額外的服務,可以理解為增強版的jdbc驅動。其中,分庫分表的操作是其中的重要一環,接下來就跟隨我來看一看,這一操作如何進行。
環境準備
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 { }
分庫分表
案例模型
把數據分別存放在兩臺服務器的兩個數據庫中表,通過分片算法來決定當前的數據存放在哪個數據庫的哪個表中,由于一個連接池只能連接一個特定的數據庫,所以這里需要創建多個連接池對象
建表
-- 分別在2臺服務器中建立數據庫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
/**
底層會根據分片規則,把我們寫的邏輯表改寫為數據庫中的真實表
/@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);
}}
優缺點
?拆分后單表數據量比較小,單表大數據被拆分,解決了單表大數據訪問問題
?分表以什么切分如果弄的不好,導致多次查詢,而且有時候要跨庫操作,甚至導致join無法使用,對排序分組等有性能影響
?之前的原子操作被拆分成多個操作,事務處理變得復雜
?多個DB維護成本增加
分庫分表也僅僅是shardingJDBC中小小的一部分,還有很多內容需要大家繼續研究,如果你對此感興趣的話,不妨關注我,接下來我還會發布更多的相關教程供大家學習,如果遇到問題我也會盡力幫助你。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。