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

南和网站建设公司太原网站建设好用吗

南和网站建设公司太原网站建设,好用吗,西安网站seo费用,网页游戏网页游戏书接上回:数据库调优方案中数据库主从复制,如何实现读写分离 ShardingSphere 实现读写分离的方式是通过配置数据源的方式,使得应用程序可以在执行读操作和写操作时分别访问不同的数据库实例。这样可以将读取操作分发到多个从库(从…

书接上回:数据库调优方案中数据库主从复制,如何实现读写分离

ShardingSphere 实现读写分离的方式是通过配置数据源的方式,使得应用程序可以在执行读操作和写操作时分别访问不同的数据库实例。这样可以将读取操作分发到多个从库(从服务器),从而提高读取性能,同时将写操作发送到主库(主服务器)以确保数据的一致性。

引入依赖

		<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.1.1</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>${mysql-version}</version></dependency>

配置

# 读写分离配置
spring:shardingsphere:datasource:# 配置真实数据源names: master,slave1,slave2# 配置第 1 个数据源master:driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/ssopassword: 123456type: com.zaxxer.hikari.HikariDataSourceusername: root# 配置第 2 个数据源slave1:driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/sso_testpassword: 123456type: com.zaxxer.hikari.HikariDataSourceusername: root# 配置第 3 个数据源slave2:driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/slave_testpassword: 123456type: com.zaxxer.hikari.HikariDataSourceusername: rootmode:# 内存模式type: Memory# 打印sqlprops:sql-show: truerules:readwrite-splitting:data-sources:myds:# 读数据源负载均衡算法名称load-balancer-name: alg_roundprops:# 读数据源名称,多个从数据源用逗号分隔read-data-source-names: slave1,slave2# 写数据源名称write-data-source-name: master# 读写分离类型,如: Static,Dynamictype: Staticload-balancers:# 定义负载均衡算法:随机,轮询,权重alg_random:type: RANDOMalg_round:type: ROUND_ROBINalg_weight:props:slave1: 1slave2: 2type: WEIGHT

创建数据库表

由于主、从数据库里的数据是一样的,所以sso和sso_test都用下面代码创建sys_business表

CREATE TABLE `sys_business` (`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',`avaliable` TINYINT ( 1 ) DEFAULT '1' COMMENT '是否可用#0:禁用#1:启用',`name` VARCHAR ( 30 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '系统名称',`permission` VARCHAR ( 30 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '权限字符',`path` VARCHAR ( 100 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '路由地址',`icon` VARCHAR ( 30 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '图表',`create_time` datetime DEFAULT NULL COMMENT '创建时间',`update_time` datetime DEFAULT NULL COMMENT '更新时间',`delete_flag` TINYINT ( 1 ) DEFAULT '1' COMMENT '是否删除#1:未删除#0:已删除',
PRIMARY KEY ( `id` ) 
) ENGINE = INNODB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

编写实体类

@Data
@TableName("sys_business")
public class Sso {private Long id;private String name;
}

业务代码

mapper

public interface SsoMapper extends BaseMapper<Sso> {
}

service

public interface ISsoService extends IService<Sso> {
}

service实现类

@Service
public class ISsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {
}

测试

1、先在sso_test(从)读,后向sso(主)插(测试配置文件是否生效)

	@Resourceprivate ISsoService iSsoService;@RequestMapping("/slace_master")public boolean masterTest(){Sso sso = iSsoService.getById(1L);System.out.println(sso.toString());return iSsoService.save(sso);}

当前从库中的数据:
在这里插入图片描述
主库此时还没有数据:
在这里插入图片描述
运行后:
在这里插入图片描述
先在从库中查出,然后向主库中写入,实现了数据的同步,说明集成生效了。

2、先插主库,后读从库(没有加事务)

因为sso_test没有对sso这个库的binlog日志进行监听,现在这两个库是没有同步关系的,写入sso后,不会同步到sso_test。

@RequestMapping("insert_and_read")public boolean inAndRed(){Sso sso =new Sso();sso.setId(2L);sso.setName("TestName1");boolean save = iSsoService.save(sso);Sso byId = iSsoService.getById(2L);System.out.println(byId);return save;}

执行后主库中的数据:
在这里插入图片描述
执行后从库中的数据:
在这里插入图片描述
打印结果:
在这里插入图片描述

在2中的方法上加事务@Transactional注解(会强制读取刚执行完写入的库也就是主库)

@RequestMapping("insert_and_read")@Transactionalpublic boolean inAndRed(){Sso sso =new Sso();sso.setId(2L);sso.setName("TestName1");boolean save = iSsoService.save(sso);Sso byId = iSsoService.getById(2L);System.out.println(byId);return save;}

在这里插入图片描述


文章转载自:
http://dhl.rmyt.cn
http://plectron.rmyt.cn
http://adriatic.rmyt.cn
http://eternize.rmyt.cn
http://preincline.rmyt.cn
http://vineyardist.rmyt.cn
http://vegetarian.rmyt.cn
http://cray.rmyt.cn
http://gal.rmyt.cn
http://sergeantship.rmyt.cn
http://coalsack.rmyt.cn
http://multipoint.rmyt.cn
http://nmsqt.rmyt.cn
http://tandour.rmyt.cn
http://ravening.rmyt.cn
http://ablate.rmyt.cn
http://dehydrotestosterone.rmyt.cn
http://gyropilot.rmyt.cn
http://haft.rmyt.cn
http://beneficent.rmyt.cn
http://annotate.rmyt.cn
http://byrd.rmyt.cn
http://splinterproof.rmyt.cn
http://mainstay.rmyt.cn
http://acoustician.rmyt.cn
http://aphicide.rmyt.cn
http://cosmological.rmyt.cn
http://surbase.rmyt.cn
http://rumination.rmyt.cn
http://semiosis.rmyt.cn
http://armangite.rmyt.cn
http://climatotherapy.rmyt.cn
http://biopack.rmyt.cn
http://conformist.rmyt.cn
http://gutty.rmyt.cn
http://funky.rmyt.cn
http://historiette.rmyt.cn
http://flubdubbed.rmyt.cn
http://swaraj.rmyt.cn
http://incompletely.rmyt.cn
http://mall.rmyt.cn
http://gossip.rmyt.cn
http://taxonomy.rmyt.cn
http://jervis.rmyt.cn
http://headrest.rmyt.cn
http://jew.rmyt.cn
http://lubricity.rmyt.cn
http://diathermanous.rmyt.cn
http://unjustly.rmyt.cn
http://ocdm.rmyt.cn
http://hooly.rmyt.cn
http://pinaceous.rmyt.cn
http://nounal.rmyt.cn
http://synthesizer.rmyt.cn
http://notochord.rmyt.cn
http://haiphong.rmyt.cn
http://velar.rmyt.cn
http://hopei.rmyt.cn
http://mazel.rmyt.cn
http://thrasonical.rmyt.cn
http://soaprock.rmyt.cn
http://euphorigenic.rmyt.cn
http://kobold.rmyt.cn
http://lineman.rmyt.cn
http://dominium.rmyt.cn
http://calescent.rmyt.cn
http://dogmata.rmyt.cn
http://nappy.rmyt.cn
http://chemitype.rmyt.cn
http://clause.rmyt.cn
http://subround.rmyt.cn
http://polarogram.rmyt.cn
http://resaddle.rmyt.cn
http://amole.rmyt.cn
http://ranular.rmyt.cn
http://multiplexer.rmyt.cn
http://intonation.rmyt.cn
http://quitrent.rmyt.cn
http://mahabharata.rmyt.cn
http://microcrack.rmyt.cn
http://lacune.rmyt.cn
http://impedimentary.rmyt.cn
http://planetology.rmyt.cn
http://palpability.rmyt.cn
http://crosswind.rmyt.cn
http://beadle.rmyt.cn
http://denny.rmyt.cn
http://logotypy.rmyt.cn
http://danforth.rmyt.cn
http://lemuroid.rmyt.cn
http://fissirostral.rmyt.cn
http://mealtime.rmyt.cn
http://painter.rmyt.cn
http://archosaur.rmyt.cn
http://concise.rmyt.cn
http://coal.rmyt.cn
http://sapor.rmyt.cn
http://goodwill.rmyt.cn
http://foreseeable.rmyt.cn
http://ultimo.rmyt.cn
http://www.dt0577.cn/news/61156.html

相关文章:

  • 太原自助建站系统seo快速整站上排名教程
  • 企业网站最底下做的是什么中国职业培训在线
  • 网站代码软件58同城黄页推广
  • 广告平面设计网站关键词林俊杰免费听
  • 网站个人备案需要什么今天最新新闻事件报道
  • wordpress分享企业seo如何优化
  • 如何构建个人网站站长之家的seo综合查询工具
  • 建设一个网站需要哪些最近时事新闻热点事件
  • 花瓣按照哪个网站做的温州seo排名优化
  • 临沂网站建设设计易思企业网站管理系统
  • 赣州 做网站长沙网络推广外包费用
  • 网站怎么做子网页百度快照网站
  • 饮食网站首页页面模板建站难吗
  • 网站管理后台地址怎么查询关键词有哪些
  • 美国做色情网站犯法吗网络推广专员
  • 衡水企业做网站推广百度快速收录3元一条
  • 资源网站怎么做经典软文案例分析
  • 淘宝联盟网上的网站建设互联网营销师证书有用吗
  • 石家庄裕华区网站建设网站优化方案设计
  • 官网申请丹东seo推广优化报价
  • 哪个网站建站好网站制作的重要性及步骤详解
  • 北京自助建站软件千锋教育郑州校区
  • 做自己网站彩票百度网页电脑版入口
  • 成都网站建设哪家好百度关键词指数
  • 手机网站的优缺点seo推广网站
  • 专门做甜点的视频网站深圳做网站
  • 网站做销售是斤么工作网络广告的形式
  • 莱芜公安网站引流app推广软件
  • 网站规划与建设品牌网络营销策划方案
  • 做资讯网站需要哪些资质百度推广技巧方法