Hable sobre la identidad del cliente de Canal

Orden

Este artículo estudia principalmente la identidad del cliente del canal

Identidad del cliente

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 define el destino, clientId, atributos de filtro

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 proporciona los métodos subscribe, unsubscribe, listAllSubscribe; su método de suscripción recibe el parámetro clientIdentity y luego usa canalInstance.getMetaManager (). GetCursor (clientIdentity) para obtener la posición, si la posición es nula, usa canalInstance.getEventStore (). GetFirstPosition (). Actualice el cursor a través de canalInstance.getMetaManager (). UpdateCursor (clientIdentity, position), y finalmente ejecute canalInstance.subscribeChange (clientIdentity); el método unsubscribe ejecuta canalInstance.getMetaManager (). Unsubscribe (clientIdentity); listAllSubscribe el método ejecuta canalInstance.get. listAllSubscribeInfo (destino)

Resumen

ClientIdentity define los atributos de destino, clientId y filtro; CanalServerWithEmbedded proporciona los métodos subscribe, unsubscribe y listAllSubscribe; donde los métodos subscribe y unsubscribe reciben el parámetro clientIdentity, y el método listAllSubscribe devuelve la lista ClientIdentity

Doc

Supongo que te gusta

Origin juejin.im/post/5e9a75106fb9a03c550fec7b
Recomendado
Clasificación