CanalのClientIdentityについて話す

注文する

この記事は主に運河の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;
        }
        return StringUtils.isNotBlank(filter);
    }

    //......
}
复制代码
  • ClientIdentityは宛先、clientId、フィルター属性を定義します

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); // 执行一下meta订阅

        Position position = canalInstance.getMetaManager().getCursor(clientIdentity);
        if (position == null) {
            position = canalInstance.getEventStore().getFirstPosition();// 获取一下store中的第一条
            if (position != null) {
                canalInstance.getMetaManager().updateCursor(clientIdentity, position); // 更新一下cursor
            }
            logger.info("subscribe successfully, {} with first position:{} ", clientIdentity, position);
        } else {
            logger.info("subscribe successfully, use last cursor position:{} ", clientIdentity, position);
        }

        // 通知下订阅关系变化
        canalInstance.subscribeChange(clientIdentity);
    }

    /**
     * 取消订阅
     */
    @Override
    public void unsubscribe(ClientIdentity clientIdentity) throws CanalServerException {
        CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
        canalInstance.getMetaManager().unsubscribe(clientIdentity); // 执行一下meta订阅

        logger.info("unsubscribe successfully, {}", clientIdentity);
    }

    /**
     * 查询所有的订阅信息
     */
    public List<ClientIdentity> listAllSubscribe(String destination) throws CanalServerException {
        CanalInstance canalInstance = canalInstances.get(destination);
        return canalInstance.getMetaManager().listAllSubscribeInfo(destination);
    }

    //......

}
复制代码
  • CanalServerWithEmbeddedはsubscribe、unsubscribe、listAllSubscribeメソッドを提供します;そのsubscribeメソッドはclientIdentityパラメータを受け取り、canalInstance.getMetaManager()を使用して位置を取得します。positionがnullの場合、canalInstance.getEventStore()を使用して取得し、次にcanalInstance.getMetaManager()。UpdateCursor(clientIdentity、position)を介してカーソルを更新し、最後にcanalInstance.subscribeChange(clientIdentity);を実行します。unsubscribeメソッドはcanalInstance.getMetaManager()を実行します。unsubscribe(clientIdentity); listAllSubscribeメソッドはcanalInstance.getMetaManager()を実行します。 listAllSubscribeInfo(宛先)

まとめ

ClientIdentityは宛先、clientId、およびフィルター属性を定義します; CanalServerWithEmbeddedはsubscribe、unsubscribe、およびlistAllSubscribeメソッドを提供します; subscribeおよびunsubscribeメソッドはclientIdentityパラメーターを受け取り、listAllSubscribeメソッドはClientIdentityリストを返します

文書

おすすめ

転載: juejin.im/post/5e9a75106fb9a03c550fec7b