canal 客户端api 使用说明
**********************
导入 jar 包
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.server</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.1.5</version>
</dependency>
**********************
相关类与接口
ClientIdentity:客户端标识
public class ClientIdentity implements Serializable {
private static final long serialVersionUID = -8262100681930834834L;
private String destination;
private short clientId;
private String filter;
public ClientIdentity() {
public ClientIdentity(String destination, short clientId) {
public ClientIdentity(String destination, short clientId, String filter) {
public Boolean hasFilter() {
return this.filter == null ? false : StringUtils.isNotBlank(this.filter);
}
public String getFilter() {
public short getClientId() {
public String getDestination() {
public void setFilter(String filter) {
public void setClientId(short clientId) {
public void setDestination(String destination) {
public String toString() {
public int hashCode() {
public boolean equals(Object obj) {
CanalConnector:客户端连接接口
public interface CanalConnector {
void connect() throws CanalClientException;
void disconnect() throws CanalClientException;
boolean checkValid() throws CanalClientException;
void subscribe(String var1) throws CanalClientException;
void subscribe() throws CanalClientException;
void unsubscribe() throws CanalClientException;
Message get(int var1) throws CanalClientException;
Message get(int var1, Long var2, TimeUnit var3) throws CanalClientException;
Message getWithoutAck(int var1) throws CanalClientException;
Message getWithoutAck(int var1, Long var2, TimeUnit var3) throws CanalClientException;
void ack(long var1) throws CanalClientException;
void rollback(long var1) throws CanalClientException;
void rollback() throws CanalClientException;
}
CanalConnectors:客户端连接建立类
public class CanalConnectors {
public CanalConnectors() {
}
public static CanalConnector newSingleConnector(SocketAddress address, String destination, String username, String password) {
SimpleCanalConnector canalConnector = new SimpleCanalConnector(address, username, password, destination);
canalConnector.setSoTimeout(60000);
canalConnector.setIdleTimeout(3600000);
return canalConnector;
}
public static CanalConnector newClusterConnector(List<? extends SocketAddress> addresses, String destination, String username, String password) {
ClusterCanalConnector canalConnector = new ClusterCanalConnector(username, password, destination, new SimpleNodeAccessStrategy(addresses));
canalConnector.setSoTimeout(60000);
canalConnector.setIdleTimeout(3600000);
return canalConnector;
}
public static CanalConnector newClusterConnector(String zkServers, String destination, String username, String password) {
ClusterCanalConnector canalConnector = new ClusterCanalConnector(username, password, destination, new ClusterNodeAccessStrategy(destination, ZkClientx.getZkClient(zkServers)));
canalConnector.setSoTimeout(60000);
canalConnector.setIdleTimeout(3600000);
return canalConnector;
}
}
SimpleCanalConnector:客户端直连canal server
public class SimpleCanalConnector implements CanalConnector {
private static final Logger logger = LoggerFactory.getLogger(SimpleCanalConnector.class);
private SocketAddress address; //canal server地址
private String username; //用户名
private String password; //密码
private int soTimeout; //读取数据超时时间
private int idleTimeout; //空闲超时时间
private String filter; //订阅的数据库、表
private final ByteBuffer readHeader;
private final ByteBuffer writeHeader;
private SocketChannel channel;
private ReadableByteChannel readableChannel;
private WritableByteChannel writableChannel;
private List<Compression> supportedCompressions;
private ClientIdentity clientIdentity;
private ClientRunningMonitor runningMonitor;
private ZkClientx zkClientx;
private BooleanMutex mutex;
private volatile boolean connected;
private boolean rollbackOnConnect;
private boolean rollbackOnDisConnect;
private boolean lazyParseEntry; //默认为false,返回的message为entry数组
//若设置为true,则返回rawEntry
private Object readDataLock;
private Object writeDataLock;
private volatile boolean running;
*********
构造函数
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination) {
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination, int soTimeout) {
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination, int soTimeout, int idleTimeout) {
*********
建立、断开连接
public void connect() throws CanalClientException {
public void disconnect() throws CanalClientException {
*********
订阅、取消订阅
public void subscribe() throws CanalClientException {
this.subscribe("");
}
public void subscribe(String filter) throws CanalClientException {
//客户端订阅的数据库、表
public void unsubscribe() throws CanalClientException {
*********
get、ack、rollback
public Message get(int batchSize) throws CanalClientException {
return this.get(batchSize, (Long)null, (TimeUnit)null);
}
public Message get(int batchSize, Long timeout, TimeUnit unit) throws CanalClientException {
Message message = this.getWithoutAck(batchSize, timeout, unit);
this.ack(message.getId());
return message;
}
public Message getWithoutAck(int batchSize) throws CanalClientException {
public Message getWithoutAck(int batchSize, Long timeout, TimeUnit unit) throws CanalClientException {
this.waitClientRunning();
if (!this.running) {
return null;
} else {
try {
int size = batchSize <= 0 ? 1000 : batchSize;
long time = timeout != null && timeout >= 0L ? timeout : -1L;
if (unit == null) {
unit = TimeUnit.MILLISECONDS;
}
this.writeWithHeader(Packet.newBuilder().setType(PacketType.GET).setBody(Get.newBuilder().setAutoAck(false).setDestination(th