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

html5 网站正在建设中如何制作一个网页

html5 网站正在建设中,如何制作一个网页,广州 网站定制,网站建设与 维护实训报告范文什么是数据分片? 简单来说,就是指通过某种特定的条件,将我们存放在同一个数据库中的数据分散存放到多个数据库(主机)上面,以达到分散单台设备负载的效果。 数据的切分(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://www.hkea.cn/news/257046/

相关文章:

  • 网站开发模式名词外贸谷歌优化
  • 网站素材 下载产品推广渠道
  • 网站后台维护怎么做seo专员工资一般多少
  • 中国网站推广黄页名录微商推广哪家好
  • 哈尔滨网站开发电话电商培训基地
  • 如何用php数据库做网站搜索seo优化托管
  • 中国城乡建设部人力网站首页优化落实疫情防控
  • 做网站到底能不能赚钱网络优化工程师前景
  • 乌镇网站建设标书百度站长工具域名查询
  • 制作公司网站价格腾讯广告代理商加盟
  • 大学生活动网站开发文案苏州seo门户网
  • 阿里云认证网站建设题库seo助理
  • 凤岗网站仿做靠谱seo外包定制
  • xampp安装wordpress说明徐州seo外包
  • 啥网站都能看的浏览器下载百度收录查询工具
  • 福田附近公司做网站建设哪家效益快奶糖 seo 博客
  • 临沂免费自助建站模板品牌整合营销
  • iis做本地视频网站找客户资源的网站
  • 做调查用哪个网站网络推广有多少种方法
  • 开发一个交易网站多少钱在线工具
  • 网站平台怎么建立的软文范例
  • 移动应用开发专业学什么东莞seo软件
  • 做宣传网站的公司手机百度极速版app下载安装
  • 私人可以做慈善网站吗外贸如何推广
  • 网站页面模板页面布局如何成为百度广告代理商
  • 瑞安外贸网站建设曲靖百度推广
  • 先做网站还是服务器销售营销方案100例
  • 用卫生纸做的礼物街网站免费网页空间到哪申请
  • 手游网站做cpc还是cpm广告号厦门网页搜索排名提升
  • 人个做外贸用什么网站好宁波百度seo点击软件