数据同步框架MS Sync Framework-不同场景使用例子和简要分析

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: 上一篇http://www.cnblogs.com/2018/archive/2011/02/22/1961654.html 对这个框架一个总体介绍,这篇通过SDK内带的例子和一个综合的例子描述一下这个框架的使用 [例子基于SDK2.

上一篇http://www.cnblogs.com/2018/archive/2011/02/22/1961654.html 对这个框架一个总体介绍,这篇通过SDK内带的例子和一个综合的例子描述一下这个框架的使用

[例子基于SDK2.1]

SDK的例子

C:\Program Files (x86)\Microsoft SDKs\Microsoft Sync Framework\2.1\Samples

WebSharingAppDemo-CEProviderEndToEnd

img_882c93e71a67be29aaab6d914df10524.png

运行效果如上图,可以进行SQL、SQLCE数据库的相互同步

Sync101RCA_Cached和其他例子展示了Provider等的实现,可参考其中的说明

基本使用

使用这个框架进行数据库同步涉及的几个方面:

Ø Provisioning:对数据库架构和内容进行修改以记录修改的内容,为同步做准备

Ø Synchronizing:同步过程,利用以上的信息进行信息的传递

Ø Snapshot:在一个同步好的数据基础上建立快照,这样其他的客户端可以快速的部署

几个场景的例子

详细代码参考:http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/SyncTest.rar 

具体代码的使用如下:

Database

N-Tier的同,SQl Server和SQL CE

Contracts: 接口定义

SqlWebSvc:WCF服务

SyncClient:客户端端

1.  建立SyncDB数据库,执行Data.edmx.sqldemoData.sql建立表和数据
2.  启动SqlWebSvc服务
3.  启动SyncClient
a)  先Provison服务端数据库,定义出同步的表和条件[只要一次即可,需要修改可以DeProvision或使用1的步骤重建]
b)  此时可远程同步[界面的中其他本地同步、架构建立只是说明]
c)   快照:只要有一个同步好的数据库,这个文件就可以拷贝到其他的客户端,对于大数据量的初始化很有用
   
   
 
总结:
Ø  数据的Provision和Deprovision简单
Ø  代码稍微多一些

Database2

类似CustomerProvider的同步

数据库Provision在Custome_SyncSamplesDb.sql建立

WCFService:服务端

SyncClient:客户端

1. 使用Custome_SyncSamplesDb.sql建立数据库和表

2. 运行WCFService

3. 运行SyncClient,可以看到同步的信息

总结:
Ø  数据的Provision和Deprovision需要通过脚本编写,采用SQL Server Tracking或自定义的形式

DBCache-IDE

VS IDE提供的支持,编程模型更简单

DataService:服务端

PresentationTier:客户端

1. 使用Northwind.sql建立数据库和表

2. 运行DataService服务

3. 运行PresentationTier

总结

Ø 提供了一系列的设置简化同步操作

Ø 只能针对SQL Server系列的数据库

Offline

SDK中Synchronizing Databases /Offline-Only Scenarios的例子

Tracking Changes in the Server Database

Use a Custom Change Tracking System

展示了以上两种跟踪变化的形式

Custome_SyncSamplesDb.sqlCustomeChangeTrack.cs

SqlServer_ChangeTracking.sqlSqlserverChangeTrack.cs

以上的脚本和代码配合可以完成同步的过程,通过这个可以了解更多的实现细节,对自定义Provider提供一些帮助

Code Specific To N-Tier

在SDK文档How to: Deliver Changes in Batches (SQL Server)“How to: Deliver Changes in Batches (SQL Server)”这个主题下有详细的说明,注记如下:

The remainder of the code examples apply only to the n-tier scenario in WebSharingAppDemo. The relevant n-tier code is contained in three files:

· The service contract: IRelationalSyncContract

· The Web service: RelationalWebSyncService

· The proxy: RelationalProviderProxy

The two providers SqlSyncProvider and SqlCeSyncProvider both inherit from RelationalSyncProvider, so this code applies to both providers. Additional store-specific functionality is separated into proxy and service files for each type of provider.

To understand how batching works in an n-tier scenario, consider a synchronization session in which the server is the source and the client is the destination. After changes have been written to the local directory on the server, the following process occurs for downloaded changes:

1. The GetChangeBatch method is called on the client proxy. As demonstrated later in the sample code, this method should include specific code to handle batching.

2. The service gets a batch file from SqlSyncProvider. The service removes the complete path information and sends only the file name over the network. This prevents exposing the directory structure of the server to the clients.

3. The proxy call to GetChangeBatch returns.

1. The proxy detects that changes are batched so it calls DownloadBatchFile by passing the batch file name as an argument.

2. The proxy creates a unique directory (if one doesn’t exist for the session) under RelationalProviderProxy.BatchingDirectory to hold these batch files locally. The directory name is the replica ID of the peer that is enumerating changes. This ensures that the proxy and service have one unique directory for each enumerating peer.

4. The proxy downloads the file and stores it locally. The proxy replaces the filename in the context with the new full path to the batch file on the local disk.

5. The proxy returns the context back to the orchestrator.

6. Repeat steps 1 through 6 until the last batch is received by proxy.

The following process occurs for uploaded changes

1. The orchestrator calls ProcessChangeBatch on the proxy.

2. The proxy determines that it is a batch file, so it performs the following steps:

1. Removes the complete path information and sends only the file name over the network.

2. Calls HasUploadedBatchFile to determine if the file has already been uploaded. If it has, step C is not necessary.

3. If HasUploadedBatchFile returns false, calls UploadBatchFile on the service, and uploads the batch file contents.

The service will receive the call to UploadBatchFile and store the batch locally. Directory creation is similar to step 4 above.

4. Calls ApplyChanges on the service.

3. The server receives the ApplyChanges call and determines that it is a batch file. It replaces the filename in the context with the new full path to the batch file on the local disk.

4. The server passes the DbSyncContext to local SqlSyncProvider.

5. Repeat steps 1 through 6 until the last batch is sent.

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
相关文章
|
6月前
|
消息中间件 大数据 Kafka
多云与混合云场景下的数据同步方案-KAFKA
多云与混合云场景下的数据同步方案-KAFKA
|
1月前
|
数据库
阿里云DTS数据迁移和数据同步的差异性分析
阿里云DTS作为一款常用的数据库表迁移工具,提供了功能非常类似的两个功能:数据迁移、数据同步。阿里云DTS产品官网对这两个功能模块进行了简单的区分: 场景1:存量数据批量迁移,建议使用数据迁移功能。 场景2:增量数据实时同步,建议使用数据同步功能。 实际上,无论是数据迁移还是数据同步,都可以做 “结构初始化”+“全量数据迁移”+“增量迁移”,因此两者功能差异并不明显。笔者在多个项目实践DTS数据迁移,在简单需求场景下,将DTS的数据迁移、数据同步进行对比和总结。
|
canal 监控 负载均衡
秃头也要学习的微服务进阶场景实战:基于Bifrost的数据同步方案
技术选型 项目组决定找一个开源中间件,它需要满足以下5点要求。 1)支持实时同步。 2)支持增量同步。 3)不用写业务逻辑。 4)支持MySQL之间的同步。 5)活跃度高。
|
5月前
|
canal NoSQL 关系型数据库
淘东电商项目(22) -Canal数据同步框架
淘东电商项目(22) -Canal数据同步框架
65 0
|
11月前
|
分布式计算 DataWorks Cloud Native
带你读《全链路数据治理-全域数据集成》之1:1. 数据集成简介
带你读《全链路数据治理-全域数据集成》之1:1. 数据集成简介
400 0
|
11月前
|
分布式计算 DataWorks MaxCompute
带你读《全链路数据治理-全域数据集成》之2:2. 同步业务场景和技术方案选择对照表
带你读《全链路数据治理-全域数据集成》之2:2. 同步业务场景和技术方案选择对照表
175 0
|
11月前
|
数据采集 运维 监控
带你读《全链路数据治理-全域数据集成》之3:3.数据同步增值能力
带你读《全链路数据治理-全域数据集成》之3:3.数据同步增值能力
218 0
|
11月前
|
DataWorks 关系型数据库 网络安全
带你读《全链路数据治理-全域数据集成》之19:5. 场景(上)
带你读《全链路数据治理-全域数据集成》之19:5. 场景(上)
183 0
|
11月前
|
DataWorks 关系型数据库 网络安全
带你读《全链路数据治理-全域数据集成》之19:5. 场景(下)
带你读《全链路数据治理-全域数据集成》之19:5. 场景(下)
149 0
|
canal 缓存 架构师
disruptor在数据同步场景下的应用实战
disruptor在数据同步场景下的应用实战
disruptor在数据同步场景下的应用实战

热门文章

最新文章