更新
代码上传到github:https://github.com/kalayan/symmetricDS-webapp
网上很少有关于SymmetricDS的embedded模式的文章,我也是调了一段时间才调通,所以写一篇文章避免大家掉坑节省时间。
一开始我想的是直接复制SymmetricDS在github上的代码,不用他提供的jar包进行试验,因为我想一步步看SymmetricDS是如何实现数据同步的,但是还有其他问题没有解决,于是先写用他提供的jar包进行试验。
去 https://www.symmetricds.org/download 下载symmetric-server-3.8.29.zip ,目前最新版本是3.8.29
新建maven的webapp项目,将symmetric-server解压之后的lib下的jar包以及web ->WEB-INF -> lib 文件夹下的jar包复制粘贴到项目中去,可以在src\main\resourses\下面新建一个lib文件夹把所有的jar包复制进去,并build path -> add to build path
将symmetric-server解压之后的web ->WEB-INF下的web.xml替换掉项目里的src\main\webapp\WEB-INF下的web.xml
将symmetric-server解压之后的conf文件夹直接复制到项目下面,并且项目右键 -> Properties -> Java Build Path -> Libraries -> Add External Folder 选择项目的conf文件夹加进去。
这里的数据库我用的是H2数据库,所以配置文件就直接参考symmetric-server里面的Sample的properties文件。
Stroe的properties,名称为store-001.properties, 放在src\main\resourses\下面
#
# Licensed to JumpMind Inc under one or more contributor
# license agreements. See the NOTICE file distributed
# with this work for additional information regarding
# copyright ownership. JumpMind Inc licenses this file
# to you under the GNU General Public License, version 3.0 (GPLv3)
# (the "License"); you may not use this file except in compliance
# with the License.
#
# You should have received a copy of the GNU General Public License,
# version 3.0 (GPLv3) along with this library; if not, see
# <http://www.gnu.org/licenses/>.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
engine.name=store-001
# The class name for the JDBC Driver
#db.driver=com.mysql.jdbc.Driver
#db.driver=oracle.jdbc.driver.OracleDriver
#db.driver=org.postgresql.Driver
#db.driver=org.apache.derby.jdbc.EmbeddedDriver
#db.driver=org.hsqldb.jdbcDriver
#db.driver=net.sourceforge.jtds.jdbc.Driver
#db.driver=com.ibm.db2.jcc.DB2Driver
#db.driver=com.informix.jdbc.IfxDriver
#db.driver=org.firebirdsql.jdbc.FBDriver
#db.driver=interbase.interclient.Driver
#db.driver=org.sqlite.JDBC
#db.driver=com.sybase.jdbc4.jdbc.SybDriver
db.driver=org.h2.Driver
# The JDBC URL used to connect to the database
#db.url=jdbc:mysql://localhost/store001?tinyInt1isBit=false
#db.url=jdbc:oracle:thin:@127.0.0.1:1521:store001
#db.url=jdbc:postgresql://localhost/store001?protocolVersion=2&stringtype=unspecified
#db.url=jdbc:derby:store001;create=true
#db.url=jdbc:hsqldb:file:store001;shutdown=true
#db.url=jdbc:jtds:sqlserver://localhost:1433/store001;useCursors=true;bufferMaxMemory=10240;lobBuffer=5242880
#db.url=jdbc:db2://localhost/store001
#db.url=jdbc:informix-sqli://localhost:9088/store001:INFORMIXSERVER=ol_ids_1150_1
#db.url=jdbc:firebirdsql:localhost:/var/lib/firebird/data/databasename
#db.url=jdbc:interbase://localhost//opt/interbase/data/store001.gdb
#db.url=jdbc:sqlite:store001.sqlite
#db.url=jdbc:sybase:Tds:localhost:5000/databasename
db.url=jdbc:h2:store001;AUTO_SERVER=TRUE;LOCK_TIMEOUT=60000
# The user to login as who can create and update tables
db.user=sa
# The password for the user to login as
db.password=
# The HTTP URL of the root node to contact for registration
registration.url=http://localhost:31415/sync/corp-000
# Do not change these for running the demo
group.id=store
external.id=001
# This is how often the routing job will be run in milliseconds
job.routing.period.time.ms=5000
# This is how often the push job will be run.
job.push.period.time.ms=10000
# This is how often the pull job will be run.
job.pull.period.time.ms=10000
Corp的properties,名称为Corp-000.properties, 放在src\main\resourses\下面
#
# Licensed to JumpMind Inc under one or more contributor
# license agreements. See the NOTICE file distributed
# with this work for additional information regarding
# copyright ownership. JumpMind Inc licenses this file
# to you under the GNU General Public License, version 3.0 (GPLv3)
# (the "License"); you may not use this file except in compliance
# with the License.
#
# You should have received a copy of the GNU General Public License,
# version 3.0 (GPLv3) along with this library; if not, see
# <http://www.gnu.org/licenses/>.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
engine.name=corp-000
# The class name for the JDBC Driver
#db.driver=com.mysql.jdbc.Driver
#db.driver=oracle.jdbc.driver.OracleDriver
#db.driver=org.postgresql.Driver
#db.driver=org.apache.derby.jdbc.EmbeddedDriver
#db.driver=org.hsqldb.jdbcDriver
#db.driver=net.sourceforge.jtds.jdbc.Driver
#db.driver=com.ibm.db2.jcc.DB2Driver
#db.driver=com.informix.jdbc.IfxDriver
#db.driver=org.firebirdsql.jdbc.FBDriver
#db.driver=interbase.interclient.Driver
#db.driver=org.sqlite.JDBC
#db.driver=com.sybase.jdbc4.jdbc.SybDriver
db.driver=org.h2.Driver
# The JDBC URL used to connect to the database
#db.url=jdbc:mysql://localhost/corp?tinyInt1isBit=false
#db.url=jdbc:oracle:thin:@127.0.0.1:1521:corp
#db.url=jdbc:postgresql://localhost/corp?protocolVersion=2&stringtype=unspecified
#db.url=jdbc:derby:corp;create=true
#db.url=jdbc:hsqldb:file:corp;shutdown=true
#db.url=jdbc:jtds:sqlserver://localhost:1433;useCursors=true;bufferMaxMemory=10240;lobBuffer=5242880
#db.url=jdbc:db2://localhost/corp
#db.url=jdbc:informix-sqli://localhost:9088/corp:INFORMIXSERVER=ol_ids_1150_1
#db.url=jdbc:firebirdsql:localhost:/var/lib/firebird/data/databasename
#db.url=jdbc:interbase://localhost//opt/interbase/data/corp.gdb
#db.url=jdbc:sqlite:corp.sqlite
#db.url=jdbc:sybase:Tds:localhost:5000/databasename
db.url=jdbc:h2:corp;AUTO_SERVER=TRUE;LOCK_TIMEOUT=60000
# The user to login as who can create and update tables
db.user=sa
# The password for the user to login as
db.password=
registration.url=
sync.url=http://localhost:31415/sync/corp-000
# Do not change these for running the demo
group.id=corp
external.id=000
# Don't muddy the waters with purge logging
job.purge.period.time.ms=7200000
# This is how often the routing job will be run in milliseconds
job.routing.period.time.ms=5000
# This is how often the push job will be run.
job.push.period.time.ms=10000
# This is how often the pull job will be run.
job.pull.period.time.ms=10000
# Kick off initial load
initial.load.create.first=true
Java代码
Stroe类
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.jumpmind.symmetric.ClientSymmetricEngine;
public class Store {
private ClientSymmetricEngine cEngine;
private File propFile;
public Store(File file) throws FileNotFoundException, IOException {
propFile = file;
Properties propertiesFile = new Properties();
propertiesFile.load(new FileReader(propFile));
cEngine = new ClientSymmetricEngine(propertiesFile, true);
getcEngine().openRegistration("client", "001");// client is the name of the node group and 001 is the ID
getcEngine().setup();
getcEngine().start();
}
public ClientSymmetricEngine getcEngine() {
return cEngine;
}
public void setcEngine(ClientSymmetricEngine cEngine) {
this.cEngine = cEngine;
}
}
Crop类
import java.io.File;
import org.jumpmind.symmetric.SymmetricWebServer;
public class Crop {
public static void main(String[] args) throws Exception {
new Store(new File("src/main/resources/store-001.properties"));
SymmetricWebServer node = new SymmetricWebServer("src/main/resources/corp-000.properties");
node.setWebAppDir("src/main/webapp");
node.setJoin(false);
node.start(31415);
// this will stop the node
//node.stop();
}
}
运行Corp,symmetricDS的embedded模式就开始运行了。
但是如果你是第一次运行,Stroe类会报错,因为H2数据库还没有进行初始化symmetricDS所需要的表。这时候,按照http://www.cnblogs.com/bobozhu/p/3694599.html的指示进行部署一遍,直接复制他们的四个文件
到项目下面,就可以了。
然后通过用h2数据库的控制台链接corp.h2.db和store001.h2.db进行数据修改和查看,就会看到他们数据会进行同步了。
关于symmetricDS的原理和源码解析会陆续放上来。
若有错误请指正,谢谢。