Encomendar
Este artigo estuda principalmente a ClientIdentity do canal
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 define destino, clientId, atributos de filtro
CanalServerWithEmbedded
canal-1.1.4 / servidor / src / principal / java / com / alibaba / lontra / canal / servidor / incorporado / 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);
}
//......
}
复制代码
- O CanalServerWithEmbedded fornece métodos de inscrição, cancelamento de inscrição, listAllSubscribe; seu método de assinatura recebe o parâmetro clientIdentity e, em seguida, usa canalInstance.getMetaManager (). GetCursor (clientIdentity) para obter a posição, se a posição for nula, usa canalInstance.getEventStore (). GetFirstPosition (). Atualize o cursor por canalInstance.getMetaManager (). UpdateCursor (clientIdentity, position) e, finalmente, execute canalInstance.subscribeChange (clientIdentity); o método unsubscribe executa canalInstance.getMetaManager (). Unsubscribe (clientIdentity); o método listAllSubscribe executa canalInstance.getMetaManager (). listAllSubscribeInfo (destino)
Sumário
ClientIdentity define os atributos de destino, clientId e filter; CanalServerWithEmbedded fornece métodos de inscrição, cancelamento de inscrição e listAllSubscribe; onde os métodos de inscrição e cancelamento de inscrição recebem o parâmetro clientIdentity e o método listAllSubscribe retorna a lista ClientIdentity