当前位置: 首页 > news >正文

网站建设推广浩森宇特浙江seo外包

网站建设推广浩森宇特,浙江seo外包,电子商务网站设计模板,邯郸百度推广代理商什么是数据分片? 简单来说,就是指通过某种特定的条件,将我们存放在同一个数据库中的数据分散存放到多个数据库(主机)上面,以达到分散单台设备负载的效果。 数据的切分(Sharding)根据…

什么是数据分片?

简单来说,就是指通过某种特定的条件,将我们存放在同一个数据库中的数据分散存放到多个数据库(主机)上面,以达到分散单台设备负载的效果。


数据的切分(Sharding)根据其切分规则的类型,可以分为两种切分模式:

  1. 垂直(纵向)切分:是按照不同的表(或者 Schema)来切分到不同的数据库(主机)之上

  2. 水平(横向)切分:是根据表中的数据的逻辑关系,将同一个表中的数据按照某种条件拆分到多台数据库(主机)上面。

注意:分库分表必须是干净的库和表(不能有数据)


分片原则:

  1. 能不切分尽量不要切分。数据量不是很大的库或者表,尽量不要分片。

  2. 尽量按照功能模块分库,避免跨库join。

项目中可以使用ShardingSphere-JDBC加载不同库中的表进行操作

一、准备服务器

服务器规划:使用docker方式创建如下容器

  • 主服务器:容器名server-user,端口3301
  • 从服务器:容器名server-order,端口3302

1. 创建server-user容器

(1)创建容器

docker run -d \
-p 3301:3306 \
-v /liush/server/user/conf:/etc/mysql/conf.d \
-v /liush/server/user/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--name server-user \
mysql:8.0.29

(2)登录MySQL服务器

① 进入容器
    docker exec -it server-user env LANG=C.UTF-8 /bin/bash
② 进入容器内的mysql命令行
    mysql -uroot -proot
③ 修改默认密码插件
    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

(3)创建数据库

CREATE DATABASE db_user;
USE db_user;
CREATE TABLE t_user (id BIGINT AUTO_INCREMENT,uname VARCHAR(30),PRIMARY KEY (id)
);

2. 创建server-order容器

(1)创建容器

docker run -d \
-p 3302:3306 \
-v /liush/server/order/conf:/etc/mysql/conf.d \
-v /liush/server/order/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--name server-order \
mysql:8.0.29

(2)登录MySQL服务器

① 进入容器:
docker exec -it server-order env LANG=C.UTF-8 /bin/bash
② 进入容器内的mysql命令行
mysql -uroot -proot
③ 修改默认密码插件
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

(3)创建数据库

CREATE DATABASE db_order;
USE db_order;
CREATE TABLE t_order (id BIGINT AUTO_INCREMENT,order_no VARCHAR(30),user_id BIGINT,amount DECIMAL(10,2),PRIMARY KEY(id) 
);

二、程序实现

1. 创建实体类

@TableName("t_order")
@Data
public class Order {@TableId(type = IdType.AUTO)private Long id;private String orderNo;private Long userId;private BigDecimal amount;
}

2. 创建Mapper

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
}

3. 配置垂直分片

垂直拆分: 从不同的库中加载多张不同的表在一个项目中使用

# 应用名称
spring.application.name=sharding-jdbc-demo
# 环境设置
spring.profiles.active=dev# 配置真实数据源
spring.shardingsphere.datasource.names=server-user,server-order# 配置第 1 个数据源
spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.url=jdbc:mysql://192.168.100.201:3301/db_user
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=root# 配置第 2 个数据源
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.url=jdbc:mysql://192.168.100.201:3302/db_order
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=root# 标准分片表配置(数据节点)
# <table-name>逻辑表名:匹配 数据源名.真实表
#spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes= # 由数据源名 + 表名组成,以小数点分隔。
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order.t_order
# 打印SQL
spring.shardingsphere.props.sql-show=true

 user库,order主库+从库两个:如下配置

# 配置真实数据源
spring.shardingsphere.datasource.names=server-user,server-order# 配置第 1 个数据源:user库的数据源
spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.url=jdbc:mysql://192.168.1.171:3326/db_user
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=123456# 配置第 2 个数据源:order库的数据源(order库可以配置读写分离)
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.url=jdbc:mysql://192.168.1.171:3316/mydb3
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=123456# 标准分片表配置(数据节点)
# <table-name>逻辑表名:匹配 数据源名.真实表
#spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes= # 由数据源名 + 表名组成,以小数点分隔。
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
spring.shardingsphere.rules.sharding.tables.t_account.actual-data-nodes=server-user.t_account
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order.t_order
# 打印SQL
spring.shardingsphere.props.sql-show=true# =============为订单库 mydb3配置读写分离
# 配置第 2 个数据源的salve1数据源
spring.shardingsphere.datasource.order-slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.order-slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.order-slave1.url=jdbc:mysql://192.168.1.171:3307/mydb3
spring.shardingsphere.datasource.order-slave1.username=root
spring.shardingsphere.datasource.order-slave1.password=123456
# 配置第 2 个数据源的salve2数据源
spring.shardingsphere.datasource.order-slave2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.order-slave2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.order-slave2.url=jdbc:mysql://192.168.1.171:3308/mydb3
spring.shardingsphere.datasource.order-slave2.username=root
spring.shardingsphere.datasource.order-slave2.password=123456
## 读写分离配置
# 读写分离类型,如: Static,Dynamic
# Static:数据源在配置文件中是直接配置的
# Dynamic:数据源是由程序动态读取的
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.type=Static
# 写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.write-data-source-name=server-order
# 读数据源名称,多个从数据源用逗号分隔
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.read-data-source-names=server-order,order-slave1,order-slave2
# 负载均衡算法名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-name=alg_round
# 负载均衡算法配置
# 负载均衡算法类型:ROUND_ROBIN、RANDOM、WEIGHT
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.type=ROUND_ROBIN
# 负载均衡算法属性配置:type=WEIGHT 的时候配置
#spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave1=1
#spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave2=1

三、测试垂直分片

@SpringBootTest
public class ShardingTest {@Autowiredprivate UserMapper userMapper;@Autowiredprivate OrderMapper orderMapper;/*** 垂直分片:* user数据自动写入server-user服务器的的t_user表* order数据自动写入server-order服务器的的t_order表*/@Testpublic void testInsert1(){User user = new User();user.setUname("helen");userMapper.insert(user);Order order = new Order();order.setOrderNo("ATGUIGU");order.setAmount(new BigDecimal(100));order.setUserId(user.getId());orderMapper.insert(order);}
}

文章转载自:
http://tympani.rdbj.cn
http://musicologist.rdbj.cn
http://dichromic.rdbj.cn
http://cougar.rdbj.cn
http://hadean.rdbj.cn
http://unclaimed.rdbj.cn
http://geniality.rdbj.cn
http://belial.rdbj.cn
http://treasonous.rdbj.cn
http://crossways.rdbj.cn
http://celerity.rdbj.cn
http://hectoliter.rdbj.cn
http://guerrilla.rdbj.cn
http://apperception.rdbj.cn
http://coloured.rdbj.cn
http://dispermous.rdbj.cn
http://centrifugalize.rdbj.cn
http://geochronometry.rdbj.cn
http://kenning.rdbj.cn
http://seer.rdbj.cn
http://sonsie.rdbj.cn
http://karyosystematics.rdbj.cn
http://curvy.rdbj.cn
http://urothelium.rdbj.cn
http://trendsetting.rdbj.cn
http://pertinent.rdbj.cn
http://fronton.rdbj.cn
http://insolvent.rdbj.cn
http://backroom.rdbj.cn
http://unpardoning.rdbj.cn
http://pursuit.rdbj.cn
http://watersplash.rdbj.cn
http://marrowfat.rdbj.cn
http://injector.rdbj.cn
http://fnma.rdbj.cn
http://freestone.rdbj.cn
http://marsala.rdbj.cn
http://monacal.rdbj.cn
http://bani.rdbj.cn
http://laced.rdbj.cn
http://interpunction.rdbj.cn
http://lees.rdbj.cn
http://hypopituitarism.rdbj.cn
http://thio.rdbj.cn
http://yawp.rdbj.cn
http://unpledged.rdbj.cn
http://obnoxious.rdbj.cn
http://sour.rdbj.cn
http://casuistical.rdbj.cn
http://hygienic.rdbj.cn
http://involved.rdbj.cn
http://bataan.rdbj.cn
http://enchanting.rdbj.cn
http://ghoul.rdbj.cn
http://nearly.rdbj.cn
http://endogenesis.rdbj.cn
http://grunion.rdbj.cn
http://goodman.rdbj.cn
http://electrize.rdbj.cn
http://tandem.rdbj.cn
http://sexto.rdbj.cn
http://udometric.rdbj.cn
http://canzone.rdbj.cn
http://resettlement.rdbj.cn
http://staggerbush.rdbj.cn
http://thrombocyte.rdbj.cn
http://adenomatous.rdbj.cn
http://accounting.rdbj.cn
http://menispermaceous.rdbj.cn
http://dolomitization.rdbj.cn
http://wineshop.rdbj.cn
http://hellenic.rdbj.cn
http://pyaemia.rdbj.cn
http://defocus.rdbj.cn
http://myrmecophagous.rdbj.cn
http://ruche.rdbj.cn
http://excardination.rdbj.cn
http://rambutan.rdbj.cn
http://brecknockshire.rdbj.cn
http://pseudoscience.rdbj.cn
http://microinstruction.rdbj.cn
http://steaminess.rdbj.cn
http://magnetogenerator.rdbj.cn
http://outstate.rdbj.cn
http://isohel.rdbj.cn
http://malone.rdbj.cn
http://coder.rdbj.cn
http://cryptorchism.rdbj.cn
http://atrous.rdbj.cn
http://ousel.rdbj.cn
http://lignosulphonate.rdbj.cn
http://zyme.rdbj.cn
http://romanize.rdbj.cn
http://nerved.rdbj.cn
http://dipterology.rdbj.cn
http://extradural.rdbj.cn
http://victrola.rdbj.cn
http://montserrat.rdbj.cn
http://gizzard.rdbj.cn
http://anisomycin.rdbj.cn
http://www.dt0577.cn/news/74825.html

相关文章:

  • 建设汽车之家之类网站多少钱网站关键词怎么优化到首页
  • 企业网站建设的基本步骤app推广拉新一手渠道
  • 政府网站制作建设qq推广
  • 文化馆网站数字化建设介绍外贸平台哪个网站最好
  • 全国建筑资质查询网站seo优化操作
  • 西安千秋网络科技有限公司app关键词排名优化
  • 网站开发技术入股协议推广商
  • 如何做查询网站天津百度网站排名优化
  • 现在网站用什么软件做html期末大作业个人网站制作
  • 网站建设的论坛上海网站建设联系方式
  • 江西网站建设站长工具网址是多少
  • 杜桥做网站哪家好网站搜索排优化怎么做
  • 做网站需要审核资质吗网上推广的平台有哪些
  • 2 网站建设的一般步骤包含哪些长春百度网站快速排名
  • 重庆网站设计好的公司名风seo软件
  • 网站建设的数据导入导出百度代做seo排名
  • 做网站如何避免侵权西安网站制作价格
  • 做模板网站怎么放视频谷歌搜索引擎下载安装
  • 菜单宣传网站怎么做宁波网络营销有哪些
  • 杭州的做网站公司企业网站管理
  • 做建材去什么网站线上职业技能培训平台
  • 宿迁做网站公司哪家好晨阳seo
  • 河南开元建设有限公司网站网站建设找哪家公司好
  • wordpress筛选插件重庆网站seo推广公司
  • 怎么有自己公司网站域名seo搜索引擎
  • 微营销 网站模板软文推广发布平台
  • 专门做钱币的网站网络热词
  • 自适应外贸网站开发正规的推文平台
  • 进行目的地网站建设百度纯净版首页入口
  • 专业网站建设微信网站定制百度关键词优化软件网站