server sql 水平分表_springboot集成Shardingsphere进行分库分表
當公司業務量上去之后,單表支撐不了的時候,分庫分表就是一個繞不開的話題,小弟最近新入職一家公司,發現這邊公司在用ShardingSphere來進行分庫分表,之前沒接觸過這方面,所以就寫了個demo學習一下,下面文章就記錄一下如何用ShardingSphere來進行分庫分表!(能力有限,本章不會講原理奧,只是記錄如何分庫分表,原理方面后面我學習了再寫,現在先會用先0.0,)
1、官網文檔地址
這是ShardingSphere官網文檔的地址,有需要的可以點進去看一下。
2、技術及環境
| mysql5.6 | springboot、mybatis、shardingsphere |
3、建庫建表
1、新建兩個數據庫:一個user0,一個user1。2、在user0數據庫新建兩個表:一個us_admin0,一個us_admin1,新建語句如下:
CREATE?TABLE?`us_admin0`?(??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT?COMMENT?'主鍵ID',
??`user_id`?int(11)?NOT?NULL?COMMENT?'用戶id',
?`addr_id`?int(11)?NOT?NULL?COMMENT?'地址id',
??`user_name`?varchar(64)?NOT?NULL?COMMENT?'用戶編號',
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='后臺用戶表';
CREATE?TABLE?`us_admin1`?(
??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT?COMMENT?'主鍵ID',
??`user_id`?int(11)?NOT?NULL?COMMENT?'用戶id',
?`addr_id`?int(11)?NOT?NULL?COMMENT?'地址id',
??`user_name`?varchar(64)?NOT?NULL?COMMENT?'用戶編號',
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='后臺用戶表';
3、在user1數據庫新建兩個表:一個us_admin0,一個us_admin1,新建語句如下:
CREATE?TABLE?`us_admin0`?(??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT?COMMENT?'主鍵ID',
??`user_id`?int(11)?NOT?NULL?COMMENT?'用戶id',
?`addr_id`?int(11)?NOT?NULL?COMMENT?'地址id',
??`user_name`?varchar(64)?NOT?NULL?COMMENT?'用戶編號',
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='后臺用戶表';
CREATE?TABLE?`us_admin1`?(
??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT?COMMENT?'主鍵ID',
??`user_id`?int(11)?NOT?NULL?COMMENT?'用戶id',
?`addr_id`?int(11)?NOT?NULL?COMMENT?'地址id',
??`user_name`?varchar(64)?NOT?NULL?COMMENT?'用戶編號',
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='后臺用戶表';
4、搭建springboot項目
這部分就idea自己創建一下就好了,pom文件及配置文件內容如下:
pom文件:
<?xml ?version="1.0"?encoding="UTF-8"?>"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?https://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0org.springframework.bootspring-boot-starter-parent2.0.7.RELEASE?com.shardingsphere1.0.0-SNAPSHOTsphereDemo?project?for?Spring?Boot1.8Hoxton.SR33.8.28.0.12junitjunit4.12testorg.springframework.bootspring-boot-starter-web-servicesorg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.apache.shardingspheresharding-jdbc-spring-boot-starter4.0.0-RC1com.alibabadruid-spring-boot-starter1.1.14mysqlmysql-connector-javaorg.springframework.bootspring-boot-starter-actuatororg.mybatis.spring.bootmybatis-spring-boot-starter2.1.2org.springframework.bootspring-boot-maven-plugin
application.yml配置:
#?服務端口server:
??port:?8888
#?服務名
spring:
??application:
????name:?test-user
??#?配置sharding?jdbc分片規則
??shardingsphere:
????datasource:
??????#?連接名稱(下面要用這個名稱來區分庫)
??????names:?ds0,ds1
??????ds0:
????????type:?com.alibaba.druid.pool.DruidDataSource
????????driver-class-name:?com.mysql.jdbc.Driver
????????url:?jdbc:mysql://192.168.1.19:3306/user0?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
????????username:?root
????????password:?123
????????initialSize:?5??????#初始化大小
????????maxActive:?200???????#最大值
????????maxWait:?2000??????#最大等待時間,配置獲取連接等待超時,時間單位都是毫秒ms
????????timeBetweenEvictionRunsMillis:?60000????#配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接
??????ds1:
????????type:?com.alibaba.druid.pool.DruidDataSource
????????driver-class-name:?com.mysql.jdbc.Driver
????????url:?jdbc:mysql://192.168.1.19:3306/user1?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
????????username:?root
????????password:?123
????????initialSize:?5??????#初始化大小
????????maxActive:?200???????#最大值
????????maxWait:?2000??????#最大等待時間,配置獲取連接等待超時,時間單位都是毫秒ms
????????timeBetweenEvictionRunsMillis:?60000????#配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接
????#?配置分片規則
????sharding:
??????#?按表來區分
??????tables:
????????us_admin:
??????????#?配置數據節點
??????????actualDataNodes:?ds${0..1}.us_admin${0..1}
??????????#?分庫策略
??????????databaseStrategy:
????????????inline:
??????????????#?分庫的規則?用user_id這個字段來分庫?總共有兩個庫?及ds0(user0)與ds1(user1)
??????????????shardingColumn:?user_id
??????????????algorithmExpression:?ds${user_id?%?2}
??????????#?分表策略
??????????tableStrategy:
????????????inline:
??????????????shardingColumn:?addr_id
??????????????algorithmExpression:?us_admin${addr_id?%?2}
??????????keyGenerator:
????????????column:?id
????????????type:?SNOWFLAKE
??????bindingTables:
????????us_admin
??????broadcastTables:
????????t_config
??????defaultDataSourceName:?ds0
????props:
??????sql.show:?true
#?配置xml?的掃描路徑
mybatis:
??mapper-locations:?classpath:mapper/*.xml
??check-config-location:?true
??type-aliases-package:?com.sharding.sphere.model
??configuration:
????cacheEnabled:?true
????mapUnderscoreToCamelCase:?true
5、接口測試
編寫增刪改接口進行測試,分庫根據user_id來切片,分表根據addr_id來切片,分別插入一些數據,結果如下:
@RestControllerpublic?class?UserController?{
????@Resource
????UserService?userService;
????@RequestMapping("add")
????public?Integer?add(@RequestBody?UsAdmin?usAdmin){
????????Integer?add?=?userService.add(usAdmin);
????????return?add;
????}
????@RequestMapping("select")
????public?List?select(){
????????List?select?=?userService.select();return?select;
????}@RequestMapping("delect")public?Integer?delect(Long?id){
????????Integer?delect?=?userService.delect(id);return?delect;
????}
}
查詢sql語句:
<select?id="selectAll"?resultMap="BaseResultMap">????select
??????id,?addr_id,?user_id,?user_name
????from?us_admin
??select>
新增sql語句:
<insert?id="insertSelective"?parameterType="com.sharding.sphere.model.UsAdmin">????insert?into?us_admin
????<trim?prefix="("?suffix=")"?suffixOverrides=",">
??????<if?test="id?!=?null">
????????id,
??????if>
??????<if?test="userId?!=?null">
????????user_id,
??????if>
??????<if?test="addrId?!=?null">
????????addr_id,
??????if>
??????<if?test="userName?!=?null">
????????user_name,
??????if>
????trim>
????<trim?prefix="values?("?suffix=")"?suffixOverrides=",">
??????<if?test="id?!=?null">
????????#{id,jdbcType=BIGINT},
??????if>
??????<if?test="userId?!=?null">
????????#{userId,jdbcType=INTEGER},
??????if>
??????<if?test="addrId?!=?null">
????????#{addrId,jdbcType=INTEGER},
??????if>
??????<if?test="userName?!=?null">
????????#{userName,jdbcType=VARCHAR},
??????if>
????trim>
??insert>
刪除sql語句:
?delete?from?us_admin????where?id?=?#{id,jdbcType=BIGINT}
數據新增分表分庫結果:
image查詢結果:
image可以看到,新增數據的時候以user_id%2來計算分庫,雙數在user0庫,單數在user1庫,addr_id作為分表id,雙數在us_admin0表,單數在us_admin1表,先確定庫然后確定表,而查詢一條語句可以查詢到所有,不過ShardingSphere好像有些sql語句是不支持的,比如關聯自己這種操作(us_admin left join us_admin這種),還有一些不支持的sql,具體的可以百度看看。
講道理來看,ShardingSphere其實對代碼的侵入量并不算多,只是有一些配置,配置好之后該寫的sql跟原來一樣的,可能是還沒有踩到該踩的坑吧,等周末研究一下原理之后再寫一篇文章分析分析。
6、公眾號
如果你覺得我的文章對你有幫助話,歡迎關注我的微信公眾號:"一個快樂又痛苦的程序員"(無廣告,單純分享原創文章、已pj的實用工具、各種Java學習資源,期待與你共同進步)
總結
以上是生活随笔為你收集整理的server sql 水平分表_springboot集成Shardingsphere进行分库分表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (二分+区间搜索 )Mountain W
- 下一篇: (DFS or BFS)Find The