CanalSharp |中文
CanalSharp is a .NET Client of alibaba's open source component Canal
,and it provides .NET developer a much easier way to use Canal.
Cancal
is a incremental Publish&Subscription component based on mysql's binlog
.To learn more a bout Cancal,please visit https://github.com/alibaba/canal/wiki
log based incremental pub&sub model can be applied to many business cases:
- Database Mirroring
- Realtime Database backup
- Multi-level index
- search build
- Busienss cache refresh
- Notification on price change(in e-commerce site) and so on
CanalSharp
as a .NET client of canal,it can be used in situations where Canal is being used.We have listed some using scenarios above,here are some more detailed use cases:
-
As a substitution of polling Database to monitor Database changes,it is much more effective than polling.
-
Realtime Update data in search engine when data in mysql database changed.For example,in a E-commerce site,when Product information changed,it can be updated to Elasticsearch、solr,etc in a realtime way.
-
Realtime update data in cache,Also take E-commerce site as an example,when price,inventory and other product information changed,it can be updated to Redis in a realtime way.
-
Database Backup,synchronization.
-
To trigger some other business logics on data change,for example,when a customer place an order without payment in a certain period of time and so the order is cancelled automatically,we get the changed status of the order and then we can push a notification to the customer(or take some other actions)
-
Push changed data to RabbitMq,kafak or other message queues for consuming.
As a .NET client of Canal,it use Socket
for communication,the transfer Protocol is TCP
,and the data exchange Protocol is Google's Protocol Buffer 3.0
-
Canal connect to Mysql database ,pretented to be a slave of mysql
-
CanalSharp(as mentioned it is a client of Cancal) connect to Canal
-
When data in the database changed,it write the changes to binlog
-
Canal send dump request to mysql database,get binlog and then parse it.
-
CancalSharp send request to Canal for data.
-
Canal send parsed data to CanalSharp
-
CancalSharp get data successfully and then send an acknowledgement to Canal(optional)
-
Canal record the consumption position
The following picture shows how it works:
To Install and configure Canal,please visit https://github.com/alibaba/canal/wiki/QuickStart
for information
Install-Package CanalSharp.Client
or you can install it from nuget manager(a graphic ui) in visual studio
//canal' destination config,default to example
var destination = "example";
//instantiate a simple CanalClient connector object(it does not support clustering),the parameters are canal's IP Address,port number,destination,username and password
var connector = CanalConnectors.NewSingleConnector("127.0.0.1", 11111, destination, "", "");
//connect to Canal
connector.Connect();
//Subscription,the parameter is a Filter,if we provided nothing it use Canal's default Filter. a filter is a rule for filtering data,data go throgh the filter will be passed in.
connector.Subscribe(".*\\..*");
//Get data with acknowledgement to indicate success
connector.Get(batchSize);
//Get data without acknowledgement to indicate success
connector.GetWithoutAck(batchSize);
for more detailed information please visit Sample
git clone https://github.com/CanalSharp/CanalSharp.git
cd CanalSharp
cd docker
docker-compose up -d
ip:the ip address of the server where docker running
mysql username:root
mysql password:000000
mysql port:4406
There is a default database named test
,which contains a table named test
Execute the following sql:
insert into test values(1000,'111');
update test set name='222' where id=1000;
delete from test where id=1000;
we can see that after executing insrt,update,delete
sql,Our CanalSharp get the changed data.
CanalSharp clustering support
We gladly accept community contributions.
1.fork the CanalSharp Project
2.make changes to it
3.make a pull request
Please do not hesitate to make a pull request,your effort will not in vain.