use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project canal by alibaba.
the class CanalInstanceWithManager method initEventParser.
protected void initEventParser() {
logger.info("init eventParser begin...");
SourcingType type = parameters.getSourcingType();
List<List<DataSourcing>> groupDbAddresses = parameters.getGroupDbAddresses();
if (!CollectionUtils.isEmpty(groupDbAddresses)) {
// 取第一个分组的数量,主备分组的数量必须一致
int size = groupDbAddresses.get(0).size();
List<CanalEventParser> eventParsers = new ArrayList<CanalEventParser>();
for (int i = 0; i < size; i++) {
List<InetSocketAddress> dbAddress = new ArrayList<InetSocketAddress>();
SourcingType lastType = null;
for (List<DataSourcing> groupDbAddress : groupDbAddresses) {
if (lastType != null && !lastType.equals(groupDbAddress.get(i).getType())) {
throw new CanalException(String.format("master/slave Sourcing type is unmatch. %s vs %s", lastType, groupDbAddress.get(i).getType()));
}
lastType = groupDbAddress.get(i).getType();
dbAddress.add(groupDbAddress.get(i).getDbAddress());
}
// 初始化其中的一个分组parser
eventParsers.add(doInitEventParser(lastType, dbAddress));
}
if (eventParsers.size() > 1) {
// 如果存在分组,构造分组的parser
GroupEventParser groupEventParser = new GroupEventParser();
groupEventParser.setEventParsers(eventParsers);
this.eventParser = groupEventParser;
} else {
this.eventParser = eventParsers.get(0);
}
} else {
// 创建一个空数据库地址的parser,可能使用了tddl指定地址,启动的时候才会从tddl获取地址
this.eventParser = doInitEventParser(type, new ArrayList<InetSocketAddress>());
}
logger.info("init eventParser end! \n\t load CanalEventParser:{}", eventParser.getClass().getName());
}
use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.
the class CanalAction method doAdd.
/**
* 添加canal
*/
public void doAdd(@FormGroup("canalInfo") Group canalInfo, @FormGroup("canalParameterInfo") Group canalParameterInfo, @FormField(name = "formCanalError", group = "canalInfo") CustomErrors err, @FormField(name = "formHeartBeatError", group = "canalParameterInfo") CustomErrors heartBeatErr, Navigator nav) throws Exception {
Canal canal = new Canal();
CanalParameter parameter = new CanalParameter();
canalInfo.setProperties(canal);
canalParameterInfo.setProperties(parameter);
String zkClustersString = canalParameterInfo.getField("zkClusters").getStringValue();
String[] zkClusters = StringUtils.split(zkClustersString, ";");
parameter.setZkClusters(Arrays.asList(zkClusters));
Long zkClusterId = canalParameterInfo.getField("autoKeeperClusterId").getLongValue();
parameter.setZkClusterId(zkClusterId);
canal.setCanalParameter(parameter);
String dbAddressesString = canalParameterInfo.getField("groupDbAddresses").getStringValue();
// 第一层的分号代表主备概念,,第二层逗号代表分组概念
if (StringUtils.isNotEmpty(dbAddressesString)) {
List<List<DataSourcing>> dbSocketAddress = new ArrayList<List<DataSourcing>>();
String[] dbAddresses = StringUtils.split(dbAddressesString, ";");
for (String dbAddressString : dbAddresses) {
List<DataSourcing> groupDbSocketAddress = new ArrayList<DataSourcing>();
String[] groupDbAddresses = StringUtils.split(dbAddressString, ",");
for (String groupDbAddress : groupDbAddresses) {
String[] strs = StringUtils.split(groupDbAddress, ":");
InetSocketAddress address = new InetSocketAddress(strs[0].trim(), Integer.valueOf(strs[1]));
SourcingType type = parameter.getSourcingType();
if (strs.length > 2) {
type = SourcingType.valueOf(strs[2]);
}
groupDbSocketAddress.add(new DataSourcing(type, address));
}
dbSocketAddress.add(groupDbSocketAddress);
}
parameter.setGroupDbAddresses(dbSocketAddress);
}
String positionsString = canalParameterInfo.getField("positions").getStringValue();
if (StringUtils.isNotEmpty(positionsString)) {
String[] positions = StringUtils.split(positionsString, ";");
parameter.setPositions(Arrays.asList(positions));
}
if (parameter.getDetectingEnable() && StringUtils.startsWithIgnoreCase(parameter.getDetectingSQL(), "select")) {
heartBeatErr.setMessage("invaliedHeartBeat");
return;
}
try {
canalService.create(canal);
} catch (RepeatConfigureException rce) {
err.setMessage("invalidCanal");
return;
}
if (parameter.getSourcingType().isMysql() && parameter.getSlaveId() == null) {
parameter.setSlaveId(10000 + canal.getId());
// 再次更新一下slaveId
try {
canalService.modify(canal);
} catch (RepeatConfigureException rce) {
err.setMessage("invalidCanal");
return;
}
}
nav.redirectTo(WebConstant.CANAL_LIST_LINK);
}
use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.
the class CanalAction method doEdit.
/**
* 修改canal
*/
public void doEdit(@FormGroup("canalInfo") Group canalInfo, @FormGroup("canalParameterInfo") Group canalParameterInfo, @FormField(name = "formCanalError", group = "canalInfo") CustomErrors err, @FormField(name = "formHeartBeatError", group = "canalParameterInfo") CustomErrors heartBeatErr, Navigator nav) throws Exception {
Canal canal = new Canal();
CanalParameter parameter = new CanalParameter();
canalInfo.setProperties(canal);
canalParameterInfo.setProperties(parameter);
String zkClustersString = canalParameterInfo.getField("zkClusters").getStringValue();
String[] zkClusters = StringUtils.split(zkClustersString, ";");
parameter.setZkClusters(Arrays.asList(zkClusters));
Long zkClusterId = canalParameterInfo.getField("autoKeeperClusterId").getLongValue();
parameter.setZkClusterId(zkClusterId);
String dbAddressesString = canalParameterInfo.getField("groupDbAddresses").getStringValue();
if (StringUtils.isNotEmpty(dbAddressesString)) {
List<List<DataSourcing>> dbSocketAddress = new ArrayList<List<DataSourcing>>();
String[] dbAddresses = StringUtils.split(dbAddressesString, ";");
for (String dbAddressString : dbAddresses) {
List<DataSourcing> groupDbSocketAddress = new ArrayList<DataSourcing>();
String[] groupDbAddresses = StringUtils.split(dbAddressString, ",");
for (String groupDbAddress : groupDbAddresses) {
String[] strs = StringUtils.split(groupDbAddress, ":");
InetSocketAddress address = new InetSocketAddress(strs[0].trim(), Integer.valueOf(strs[1]));
SourcingType type = parameter.getSourcingType();
if (strs.length > 2) {
type = SourcingType.valueOf(strs[2]);
}
groupDbSocketAddress.add(new DataSourcing(type, address));
}
dbSocketAddress.add(groupDbSocketAddress);
}
parameter.setGroupDbAddresses(dbSocketAddress);
}
String positionsString = canalParameterInfo.getField("positions").getStringValue();
if (StringUtils.isNotEmpty(positionsString)) {
String[] positions = StringUtils.split(positionsString, ";");
parameter.setPositions(Arrays.asList(positions));
}
if (parameter.getDetectingEnable() && StringUtils.startsWithIgnoreCase(parameter.getDetectingSQL(), "select")) {
heartBeatErr.setMessage("invaliedHeartBeat");
return;
}
canal.setCanalParameter(parameter);
try {
canalService.modify(canal);
} catch (RepeatConfigureException rce) {
err.setMessage("invalidCanal");
return;
}
nav.redirectToLocation("canalList.htm");
}
use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.
the class NumberFormatUtil method formatGroupDbAddress.
public String formatGroupDbAddress(SourcingType defaultType, List<List<DataSourcing>> groupDbAddresses) {
StringBuilder builder = new StringBuilder();
for (List<DataSourcing> groupDbAddress : groupDbAddresses) {
List<String> address = new ArrayList<String>();
for (DataSourcing dbAddress : groupDbAddress) {
StringBuilder dbAddressBuilder = new StringBuilder();
dbAddressBuilder.append(dbAddress.getDbAddress().getAddress().getHostName());
dbAddressBuilder.append(":");
dbAddressBuilder.append(String.valueOf(dbAddress.getDbAddress().getPort()));
if (!defaultType.equals(dbAddress.getType())) {
dbAddressBuilder.append(":").append(dbAddress.getType().name());
}
address.add(dbAddressBuilder.toString());
}
builder.append(StringUtils.join(address, ',')).append(";");
}
return builder.toString();
}
use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.
the class SeniorCanal method getUrl.
public String getUrl() {
CanalParameter parameter = getCanalParameter();
if (parameter.getHaMode().isMedia()) {
return "media://" + parameter.getMediaGroup();
} else {
StringBuilder address = new StringBuilder("jdbc://");
for (List<DataSourcing> groupAddress : parameter.getGroupDbAddresses()) {
int i = 0;
for (DataSourcing dbAddress : groupAddress) {
++i;
address.append(dbAddress.getDbAddress().getAddress().getHostName()).append(":").append(dbAddress.getDbAddress().getPort());
if (i < groupAddress.size()) {
address.append(',');
}
}
address.append(';');
}
return address.toString();
}
}
Aggregations