Sharding-Sphere 3.X 與spring與mybatis整合(分庫分表)demo
最近在弄這個sharding-sphere,公司內部分庫分表是在此業務程式碼上進行邏輯分庫分表,但是這種總是不好,也調研了幾款分庫分表中介軟體、mycat、網易cetus、阿里DRDS、這幾種就是背景強大,大公司經過大量的實戰,成熟度很高,而框架sharding-sphere比較輕量級,最近比較火,它是以jar包形式提供服務,可以無縫連線ORM框架,並不需要額外的部署,不需要依賴,運維可以不需要改動,很多人都把sharding-sphere當成增強版的jdbc驅動,遷移程式碼其實沒那麼複雜。對於巨頭公司,公司內部都會有自己研發的元件,中介軟體,來供整個公司使用,對於中小型公司,需要使用開源的中介軟體來支撐公司業務發展。
個人建議,還是以官方文件為主,官方文件例子都很全,文件也很清楚,執行也都沒問題,不要去搜網上的demo。但是有一點建議一下,可以看著官網的examples,根據公司的程式碼框架寫個簡單的demo什麼的,可以執行。
sql指令碼:
/* */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_order_0 -- ---------------------------- DROP TABLE IF EXISTS `t_order_0`; CREATE TABLE `t_order_0` ( `order_id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `status` varchar(50) DEFAULT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_1 -- ---------------------------- DROP TABLE IF EXISTS `t_order_1`; CREATE TABLE `t_order_1` ( `order_id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `status` varchar(50) DEFAULT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_item_0 -- ---------------------------- DROP TABLE IF EXISTS `t_order_item_0`; CREATE TABLE `t_order_item_0` ( `order_item_id` bigint(20) NOT NULL , `order_id` bigint(20) DEFAULT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`order_item_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_item_1 -- ---------------------------- DROP TABLE IF EXISTS `t_order_item_1`; CREATE TABLE `t_order_item_1` ( `order_item_id` bigint(20) NOT NULL, `order_id` bigint(20) DEFAULT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`order_item_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
database.properties 配置檔案
sharding.jdbc.datasource.names=separate_entity_0,separate_entity_1 sharding.jdbc.datasource.separate_entity_0.url=jdbc:mysql://127.0.0.1:3307/separate_entity_0 sharding.jdbc.datasource.separate_entity_0.username=root sharding.jdbc.datasource.separate_entity_0.password= sharding.jdbc.datasource.separate_entity_1.url=jdbc:mysql://127.0.0.1:3307/separate_entity_1 sharding.jdbc.datasource.separate_entity_1.username=root sharding.jdbc.datasource.separate_entity_1.password= sharding.jdbc.datasource.actual.data.nodes.order=separate_entity_$->{0..1}.t_order_$->{0..1} sharding.jdbc.datasource.actual.data.nodes.orderitem=separate_entity_$->{0..1}.t_order_item_$->{0..1}
``
sharding-databases.xml 配置檔案:
<!-- 可能不同的業務表有不同的業務規則,會出現好多個分庫,分表策略-->
<bean id="preciseModuloDatabaseShardingAlgorithm" class="com.sharding.demo.algorithm.DatabaseShardingAlgorithm" />
<bean id="preciseModuloTableShardingAlgorithm" class="com.sharding.demo.algorithm.TableShardingAlgorithm" />
<!-- 可能不同的業務表有不同的業務規則,會出現好多個分庫,分表策略,這塊指定了-->
<sharding:standard-strategy id="databaseShardingStrategy" sharding-column="user_id" precise-algorithm-ref="preciseModuloDatabaseShardingAlgorithm" />
<sharding:standard-strategy id="tableShardingStrategy" sharding-column="order_id" precise-algorithm-ref="preciseModuloTableShardingAlgorithm" />
<sharding:data-source id="shardingDataSource">
<sharding:sharding-rule data-source-names="separate_entity_0,separate_entity_1">
<sharding:table-rules>
<sharding:table-rule logic-table="t_order"
actual-data-nodes="${sharding.jdbc.datasource.actual.data.nodes.order}"
database-strategy-ref="databaseShardingStrategy" table-strategy-ref="tableShardingStrategy"
generate-key-column-name="order_id" />
<sharding:table-rule logic-table="t_order_item"
actual-data-nodes="${sharding.jdbc.datasource.actual.data.nodes.orderitem}"
database-strategy-ref="databaseShardingStrategy"
table-strategy-ref="tableShardingStrategy"
generate-key-column-name="order_item_id" />
</sharding:table-rules>
</sharding:sharding-rule>
</sharding:data-source>
分庫分表:
public final class DatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}
}
public final class TableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}
}
啟動步驟:
1、需要一個tomcat
2、啟動成功後直接訪問http://ip:port/SSM/demo/test
3、訪問類是在ShardingDemoController這個類裡
相關推薦
Sharding-Sphere 3.X 與spring與mybatis整合(分庫分表)demo
最近在弄這個sharding-sphere,公司內部分庫分表是在此業務程式碼上進行邏輯分庫分表,但是這種總是不好,也調研了幾款分庫分表中介軟體、mycat、網易cetus、阿里DRDS、這幾種就是背景強大,大公司經過大量的實戰,成熟度很高,而框架sharding-sphere
Sharding-Sphere 3.X 與spring與mybatis集成(分庫分表)demo
sharding jdbc 分庫分表最近在弄這個sharding-sphere,公司內部分庫分表是在此業務代碼上進行邏輯分庫分表,但是這種總是不好,也調研了幾款分庫分表中間件、mycat、網易cetus、阿裏DRDS、這幾種就是背景強大,大公司經過大量的實戰,成熟度很高,而框架sharding-sphere比
Sharding-Sphere 3.X萬眾矚目登場,如約而至!
從Sharding-JDBC到Sharding-Sphere,老鐵粉陪它一同走過,新朋友也在陸續加入。Sharding-Sphere是什麼?做什麼?做的如何?三大經典提問幫助新老朋友一同溫故知新。 Sharding-Sphere是什麼? Sharding-Sphere
Sharding-JDBC 3.x 原理篇之基本介紹(一)
簡介 Sharding-JDBC是噹噹開源的資料庫水平切分的中介軟體,其代表了客戶端類的分庫分表技術框架(這一點與MyCat不同,MyCat本質上是一種資料庫代理)。Sharding-JDBC定位為輕量級資料庫驅動,由客戶端直連資料庫,以jar包形式提供服務,未
Sharding-JDBC 3.x 原理篇之基本介紹(二)附1.x~3.x版本文件
前言 上一篇描述了Sharding-JDBC的歷史演進過程和設計理念,本篇將具體描述Sharding-Sphere的具體功能和個版本特性。後續將對常用核心功能逐一進行分析和探討。 版本功能 1.x功能列表 分庫分表 SQL解析功能完善,支援聚合,分組,排序,LI
python 3.x 分析日誌的模塊(正則匹配)
默認值 .get 文件 file 打印 files 日誌 當前 表達式 #導入正則模塊 import re auth="no_shutdown_" ‘‘‘ 分析日誌的模塊,查找日誌中標誌性信息產生的次數 ‘‘‘ #定義你需要查找的對象的正則表達式wordcheck #需要分
spring、springmvc和mybatis整合(java config方式)
ada vat req style face sat roo art 實體 之前項目中使用ssm框架大多是基於xml的方式,spring3.0以後就提供java config的模式來構建項目,並且也推薦使用這種方式,自從接觸過springboot後,深深感受到這種純ja
Netty遊戲伺服器實戰開發(11):Spring+mybatis 手寫分庫分表策略(續)
在大型網路遊戲中,傳統的遊戲伺服器無法滿足效能上的需求。所以有了分散式和微服務新起,在傳統web伺服器中,我們儲存使用者等資訊基本都是利用一張單表搞定,但是在遊戲伺服器中,由於要求比較高,我們不能存在大表操作,即分庫分表策略。在以前的文章中有關介紹分庫分表的,下面我們來實戰一下,首先我們
Spring boot Mybatis 整合(完整版)
個人開源專案 springboot+mybatis+thymeleaf+docker構建的個人站點開源專案(集成了個人主頁、個人作品、個人部落格) 推薦開源專案 開源的springboot介面文件元件swagger2 更多幹貨 SpringBoot
Mybatis—spring和mybatis整合(十)
上一篇文章我們講解了《Mybatis—查詢快取(九)》下面我們繼續來講解spring和mybatis整合。 【整合思路】 需要spring通過單例方式管理SqlSessionFactory。 spring和mybatis整合生成代理物件,使用SqlSe
Spring boot Mybatis 整合(註解版)
之前寫過一篇關於springboot 與 mybatis整合的博文,使用了一段時間spring-data-jpa,發現那種方式真的是太爽了,mybatis的xml的對映配置總覺得有點麻煩。介面定義和對映離散在不同的檔案中,閱讀起來不是很方便。於是,準備使用
spring aop基礎上得分庫分表(1)
1.環境說明:spring,mybtis,mysql;2.分兩種方式實現自定義切庫3.第一種方式通過自定義註解得方式實現基本思路:我們在使用mybatis得時候,一個數據源配套一套mybatis配置。若要連結多個數據源,那麼就需要多套mybatis配置。通過不同的配置操作相應
JavaEE Spring與MyBatis的整合之傳統DAO方式整合(教材學習筆記)
在實際開發中MyBatis都是與Spring整合在一起使用的,在之前學習了MyBatis與Spring,現在來學習如何使他們整合 首先建立一個名為chapter10的web專案 一、環境搭建 1.準備好所有的有關jar包,具體如下: 將上面所有jar包新增到專案lib目錄下
Spring+SpringMVC+Mybatis整合系列(一)Maven安裝與配置
最近有專案要做,想著還是來寫一篇SSM搭建的文件吧!此搭建文件僅供參考! Spring+SpringMVC+Mybatis簡稱SSM,首先說明一下,由於搭建SSM的篇幅略長,如果放到一篇博文中,可能會給讀者感覺閱讀起來很吃力。其實,寫博文也需要解耦,長篇大論並
mybatis與spring整合(基於配置檔案)
本文主要介紹瞭如何將mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作為資料庫連線池。1.編寫資料訪問介面(UserDao.java)package com.mybatis;publicinterf
python 3 之日期與時間處理模塊(date和datetime)
python 時間 處理模塊 前言相關術語的解釋時間的表現形式time模塊datetime模塊時間格式碼總結前言 在開發工作中,我們經常需要用到日期與時間,如: 作為日誌信息的內容輸出計算某個功能的執行時間用日期命名一個日誌文件的名稱記錄或展示某文章的發布或修改時間其他Python中提供了多個用於
框架整合——SpringMVC與MyBatis整合(超詳細)
SpringMVC與MyBatis是我們現在最流行的開發框架組合之一,這裡我來整理一下框架的整合搭建過程 前言 使用IDE:IntelliJ IDEA JDK:1.8 開啟IDEA,新建maven工程 第一步:開啟IDEA,點選Create New Pro
Kafka的安裝及與Spring Boot的整合
安裝JDK 下載jdk-8u202-ea-bin-b03-linux-x64-07_nov_2018.tar.gz 解壓 配置 $ vi /etc/profile,在最後加入下面兩行 export JAVA_HOME=/usr/local/bigdata/jdk
spring cloud 入門(五)【負載均衡與 FeignClient 容錯機制一(通過類容錯)】
一 負載均衡 spring cloud 負載均衡非常簡單,現在服務提供者 menu,重新copy 一份munu ,命名 menu2 ,將埠 修改為 8764 ,其他配置都不變 這個時候 如何 再次呼叫 user/getMenu 介面, 檢視 menu 和 menu2 的日誌,我們可以看出來,
Sharding-Sphere 3.1.0.M1 正式釋出
Sharding-Sphere 3.1.0.M1 釋出。 Sharding-Sphere 是一套開源的分散式資料庫中介軟體解決方案組成的生態圈,它由 Sharding-JDBC、Sharding-Proxy 和 Sharding-Sidecar(規劃中)這3款相互獨立的產