基於Sharding-Jdbc 實現的讀寫分離實現
1. pom 檔案依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0-beta</version>
</dependency>
<dependency>
<groupId>io.shardingjdbc</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. application配置
server:
port: 9002
mybatis-plus:
# mapper-locations: classpath*:/mapper/*.xml
global-config:
db-config:
column-underline: true
#shardingjdbc配置
sharding:
jdbc:
data-sources:
###配置第一個從資料庫
ds_slave_0:
password: root
jdbc-url: jdbc:mysql://192.168.212.203:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
driver-class-name: com.mysql.jdbc.Driver
username: root
###主資料庫配置
ds_master:
password: root
jdbc-url: jdbc:mysql://192.168.212.202:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
driver-class-name: com.mysql.jdbc.Driver
username: root
###配置讀寫分離
master-slave-rule:
###配置從庫選擇策略,提供輪詢與隨機,這裡選擇用輪詢
load-balance-algorithm-type: round_robin
####指定從資料庫
slave-data-source-names: ds_slave_0
name: ds_ms
####指定主資料庫
master-data-source-name: ds_master
3 .master 和 slave 配置檔案。
import java.sql.SQLException;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.collect.Maps;
import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
import lombok.extern.log4j.Log4j2;
@Configuration
@EnableConfigurationProperties(ShardingMasterSlaveConfig.class)
@Log4j2
// 讀取ds_master主資料來源和讀寫分離配置
@ConditionalOnProperty({ "sharding.jdbc.data-sources.ds_master.jdbc-url",
"sharding.jdbc.master-slave-rule.master-data-source-name" })
public class ShardingDataSourceConfig {
@Autowired
private ShardingMasterSlaveConfig shardingMasterSlaveConfig;
@Bean
public DataSource masterSlaveDataSource() throws SQLException {
final Map<String, DataSource> dataSourceMap = Maps.newHashMap();
dataSourceMap.putAll(shardingMasterSlaveConfig.getDataSources());
final Map<String, Object> newHashMap = Maps.newHashMap();
// 建立 MasterSlave資料來源
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap,
shardingMasterSlaveConfig.getMasterSlaveRule(), newHashMap);
log.info("masterSlaveDataSource config complete");
return dataSource;
}
}
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.zaxxer.hikari.HikariDataSource;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import lombok.Data;
@Data
@ConfigurationProperties(prefix = "sharding.jdbc")
public class ShardingMasterSlaveConfig {
// 存放本地多個資料來源
private Map<String, HikariDataSource> dataSources = new HashMap<>();
private MasterSlaveRuleConfiguration masterSlaveRule;
}
相關文章
- 搭建基於springmvc,ibatis的工程實現讀寫分離,配置分離SpringMVCBAT
- Sharding-JDBC基本使用,整合Springboot實現分庫分表,讀寫分離JDBCSpring Boot
- 資料庫中介軟體sharding-jdbc實現讀寫分離資料庫JDBC
- 【Mongo】Mongo讀寫分離的實現Go
- ProxySQL實現MySQL讀寫分離MySql
- 關於Dapper實現讀寫分離的個人思考APP
- Kubernetes 中實現 MySQL 的讀寫分離MySql
- 【Tony 老師】基於 Maxscale 實現讀寫分離和負載均衡負載
- ShardingSphere(七) 讀寫分離配置,實現分庫讀寫操作
- Docker實現Mariadb分庫分表、讀寫分離Docker
- PostgreSQL+Pgpool實現HA讀寫分離SQL
- docker+atlas+mysql實現讀寫分離DockerMySql
- Mycat實現mysql的負載均衡讀寫分離MySql負載
- SpringBoot使用Sharding-JDBC讀寫分離Spring BootJDBC
- Spring Aop實現資料庫讀寫分離Spring資料庫
- ProxySQL實現Mysql讀寫分離 - 部署手冊MySql
- SpringBoot 專案優雅實現讀寫分離Spring Boot
- 搭建MySQL主從實現Django讀寫分離MySqlDjango
- MHA+ProxySQL實現讀寫分離高可用SQL
- 位元組面試:什麼是讀寫分離?讀寫分離的底層如何實現?面試
- MySQL怎麼實現主從同步和Django實現MySQL讀寫分離MySql主從同步Django
- Mycat中介軟體實現Percona Cluster讀寫分離
- redis客戶端實現高可用讀寫分離Redis客戶端
- ShardingSphere + Mysql,實現分庫分表、讀寫分離,並整合 SpringBootMySqlSpring Boot
- 分庫分表(6)--- SpringBoot+ShardingSphere實現分表+ 讀寫分離Spring Boot
- MySQL-SpringBoot整合JPA實現資料讀寫分離MySqlSpring Boot
- 資料庫讀寫分離,主從同步實現方法資料庫主從同步
- Mycat中介軟體實現Mysql主從讀寫分離MySql
- ShardingSphere-proxy +PostgreSQL實現讀寫分離(靜態策略)SQL
- 基於MySql主從分離的程式碼層實現MySql
- springboot+mybatis+druid實現mysql主從讀寫分離(五)Spring BootMyBatisUIMySql
- 搭建Redis哨兵叢集並使用RedisTemplate實現讀寫分離Redis
- 使用ProxySQL實現MySQL Group Replication的故障轉移、讀寫分離(一)MySql
- mysql讀寫分離的最佳實踐MySql
- Spring Boot + Mybatis 多資料來源配置實現讀寫分離Spring BootMyBatis
- Mycat2+Mysql一主一從實現讀寫分離配置MySql
- StoneDB 讀寫分離實踐方案
- 快速掌握mongoDB(六)——讀寫分離的副本集實現和Sharing介紹MongoDB