sequence
This paper mainly studies Canal’s ClientIdentity
ClientIdentity
Canal – 1.1.4 / protocol/SRC/main/Java/com/alibaba/otter/canal/protocol/ClientIdentity. Java
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){
this.clientId = clientId;
this.destination = destination;
}
public ClientIdentity(String destination, short clientId, String filter){
this.clientId = clientId;
this.destination = destination;
this.filter = filter;
}
public Boolean hasFilter() {
if (filter == null) {
return false;
}
returnStringUtils.isNotBlank(filter); } / /... }Copy the code
- ClientIdentity Defines the destination, clientId, and filter attributes
CanalServerWithEmbedded
Canal – 1.1.4 / server/SRC/main/Java/com/alibaba/otter/canal/server/embedded/CanalServerWithEmbedded. Java
public class CanalServerWithEmbedded extends AbstractCanalLifeCycle implements CanalServer, CanalService { private static final Logger logger = LoggerFactory.getLogger(CanalServerWithEmbedded.class); private Map<String, CanalInstance> canalInstances; // private Map<ClientIdentity, Position> lastRollbackPostions; private CanalInstanceGenerator canalInstanceGenerator; private int metricsPort; private CanalMetricsService metrics = NopCanalMetricsService.NOP; private String user; private String passwd; / /... @Override public void subscribe(ClientIdentity clientIdentity) throws CanalServerException { checkStart(clientIdentity.getDestination()); CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());if(! canalInstance.getMetaManager().isStart()) { canalInstance.getMetaManager().start(); } canalInstance.getMetaManager().subscribe(clientIdentity); / / perform a subscription Position meta Position. = canalInstance getMetaManager (). The getCursor (clientIdentity);if(position == null) { position = canalInstance.getEventStore().getFirstPosition(); // Get the first item in storeif(position ! = null) { canalInstance.getMetaManager().updateCursor(clientIdentity, position); // Update cursor} logger.info("subscribe successfully, {} with first position:{} ", clientIdentity, position);
} else {
logger.info("subscribe successfully, use last cursor position:{} ", clientIdentity, position); } / / subscription relations under the notification canalInstance change subscribeChange (clientIdentity); } /** * Unsubscribe */ @override public void unsubscribe(ClientIdentity ClientIdentity) throws CanalServerException { CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination()); canalInstance.getMetaManager().unsubscribe(clientIdentity); // Execute the meta subscription logger.info("unsubscribe successfully, {}", clientIdentity); } /** * Query all subscription information */ public List<ClientIdentity> listAllSubscribe(String destination) throws CanalServerException { CanalInstance canalInstance = canalInstances.get(destination);returncanalInstance.getMetaManager().listAllSubscribeInfo(destination); } / /... }Copy the code
- CanalServerWithEmbedded provides subscribe, unsubscribe, listAllSubscribe methods; The subscribe method receives clientIdentity parameters, and then use the canalInstance. GetMetaManager (). The getCursor (clientIdentity) obtaining the position, If position is null then use canalInstance. GetEventStore () getFirstPosition (), Then through canalInstance. GetMetaManager (.) updateCursor clientIdentity, position update cursor, Finally perform canalInstance. SubscribeChange (clientIdentity); Unsubscribe method is executed canalInstance. GetMetaManager (). Unsubscribe (clientIdentity); ListAllSubscribe method is executed canalInstance. GetMetaManager () listAllSubscribeInfo (destination)
summary
ClientIdentity Defines the destination, clientId, and filter attributes. CanalServerWithEmbedded provides subscribe, unsubscribe, listAllSubscribe methods; The SUBSCRIBE and unsubscribe methods receive the clientIdentity parameter, and the listAllSubscribe method returns the clientIdentity list
doc
- ClientIdentity