mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
fixed canal-admin
This commit is contained in:
@@ -23,8 +23,8 @@ public class CanalConstants {
|
||||
public static final String CANAL_ADMIN_PORT = ROOT + "." + "admin.port";
|
||||
public static final String CANAL_ADMIN_USER = ROOT + "." + "admin.user";
|
||||
public static final String CANAL_ADMIN_PASSWD = ROOT + "." + "admin.passwd";
|
||||
public static final String CANAL_ADMIN_AUTO_REGISTER = ROOT + "." + "admin.auto.register";
|
||||
public static final String CANAL_ADMIN_AUTO_CLUSTER = ROOT + "." + "admin.auto.cluster";
|
||||
public static final String CANAL_ADMIN_AUTO_REGISTER = ROOT + "." + "admin.register.auto";
|
||||
public static final String CANAL_ADMIN_AUTO_CLUSTER = ROOT + "." + "admin.register.cluster";
|
||||
public static final String CANAL_ZKSERVERS = ROOT + "." + "zkServers";
|
||||
public static final String CANAL_WITHOUT_NETTY = ROOT + "." + "withoutNetty";
|
||||
|
||||
|
||||
@@ -106,17 +106,12 @@ public class CanalController {
|
||||
// 准备canal server
|
||||
ip = getProperty(properties, CanalConstants.CANAL_IP);
|
||||
registerIp = getProperty(properties, CanalConstants.CANAL_REGISTER_IP);
|
||||
port = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_PORT));
|
||||
adminPort = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_ADMIN_PORT));
|
||||
port = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_PORT, "11111"));
|
||||
adminPort = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_ADMIN_PORT, "11110"));
|
||||
embededCanalServer = CanalServerWithEmbedded.instance();
|
||||
embededCanalServer.setCanalInstanceGenerator(instanceGenerator);// 设置自定义的instanceGenerator
|
||||
try {
|
||||
int metricsPort = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_METRICS_PULL_PORT));
|
||||
embededCanalServer.setMetricsPort(metricsPort);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.info("No valid metrics server port found, use default 11112.");
|
||||
embededCanalServer.setMetricsPort(11112);
|
||||
}
|
||||
int metricsPort = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_METRICS_PULL_PORT, "11112"));
|
||||
embededCanalServer.setMetricsPort(metricsPort);
|
||||
|
||||
this.adminUser = getProperty(properties, CanalConstants.CANAL_ADMIN_USER);
|
||||
this.adminPasswd = getProperty(properties, CanalConstants.CANAL_ADMIN_PASSWD);
|
||||
@@ -286,7 +281,21 @@ public class CanalController {
|
||||
if (config != null) {
|
||||
ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(destination);
|
||||
if (runningMonitor.isStart()) {
|
||||
runningMonitor.release();
|
||||
boolean release = runningMonitor.release();
|
||||
if (!release) {
|
||||
// 如果是单机模式,则直接清除配置
|
||||
instanceConfigs.remove(destination);
|
||||
// 停掉服务
|
||||
runningMonitor.stop();
|
||||
if (instanceConfigMonitors.containsKey(InstanceConfig.InstanceMode.MANAGER)) {
|
||||
ManagerInstanceConfigMonitor monitor = (ManagerInstanceConfigMonitor) instanceConfigMonitors.get(InstanceConfig.InstanceMode.MANAGER);
|
||||
Map<String, InstanceAction> instanceActions = monitor.getActions();
|
||||
if (instanceActions.containsKey(destination)) {
|
||||
// 清除内存中的autoScan cache
|
||||
monitor.release(destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,7 +306,9 @@ public class CanalController {
|
||||
instanceConfigMonitors = MigrateMap.makeComputingMap(new Function<InstanceMode, InstanceConfigMonitor>() {
|
||||
|
||||
public InstanceConfigMonitor apply(InstanceMode mode) {
|
||||
int scanInterval = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_AUTO_SCAN_INTERVAL));
|
||||
int scanInterval = Integer.valueOf(getProperty(properties,
|
||||
CanalConstants.CANAL_AUTO_SCAN_INTERVAL,
|
||||
"5"));
|
||||
|
||||
if (mode.isSpring()) {
|
||||
SpringInstanceConfigMonitor monitor = new SpringInstanceConfigMonitor();
|
||||
@@ -322,8 +333,6 @@ public class CanalController {
|
||||
monitor.setDefaultAction(defaultAction);
|
||||
String managerAddress = getProperty(properties, CanalConstants.CANAL_ADMIN_MANAGER);
|
||||
monitor.setConfigClient(getManagerClient(managerAddress));
|
||||
monitor.setIp(registerIp);
|
||||
monitor.setPort(adminPort);
|
||||
return monitor;
|
||||
} else {
|
||||
throw new UnsupportedOperationException("unknow mode :" + mode + " for monitor");
|
||||
@@ -444,6 +453,15 @@ public class CanalController {
|
||||
return config;
|
||||
}
|
||||
|
||||
public static String getProperty(Properties properties, String key, String defaultValue) {
|
||||
String value = getProperty(properties, key);
|
||||
if (StringUtils.isEmpty(value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getProperty(Properties properties, String key) {
|
||||
key = StringUtils.trim(key);
|
||||
String value = System.getProperty(key);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class CanalLauncher {
|
||||
if (StringUtils.isNotEmpty(managerAddress)) {
|
||||
String user = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_USER);
|
||||
String passwd = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PASSWD);
|
||||
String adminPort = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PORT);
|
||||
String adminPort = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PORT, "11110");
|
||||
boolean autoRegister = BooleanUtils.toBoolean(CanalController.getProperty(properties,
|
||||
CanalConstants.CANAL_ADMIN_AUTO_REGISTER));
|
||||
String autoCluster = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_AUTO_CLUSTER);
|
||||
@@ -75,8 +75,9 @@ public class CanalLauncher {
|
||||
Properties managerProperties = canalConfig.getProperties();
|
||||
// merge local
|
||||
managerProperties.putAll(properties);
|
||||
int scanIntervalInSecond = Integer.valueOf(CanalController.getProperty(properties,
|
||||
CanalConstants.CANAL_AUTO_SCAN_INTERVAL));
|
||||
int scanIntervalInSecond = Integer.valueOf(CanalController.getProperty(managerProperties,
|
||||
CanalConstants.CANAL_AUTO_SCAN_INTERVAL,
|
||||
"5"));
|
||||
executor.scheduleWithFixedDelay(new Runnable() {
|
||||
|
||||
private PlainCanal lastCanalConfig;
|
||||
|
||||
@@ -28,26 +28,21 @@ import com.google.common.collect.MigrateMap;
|
||||
*/
|
||||
public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle implements InstanceConfigMonitor, CanalLifeCycle {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(ManagerInstanceConfigMonitor.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(ManagerInstanceConfigMonitor.class);
|
||||
private long scanIntervalInSecond = 5;
|
||||
private InstanceAction defaultAction = null;
|
||||
private Map<String, InstanceAction> actions = new MapMaker().makeMap();
|
||||
private Map<String, PlainCanal> configs = MigrateMap
|
||||
.makeComputingMap(new Function<String, PlainCanal>() {
|
||||
private Map<String, PlainCanal> configs = MigrateMap.makeComputingMap(new Function<String, PlainCanal>() {
|
||||
|
||||
public PlainCanal apply(String destination) {
|
||||
return new PlainCanal();
|
||||
}
|
||||
});
|
||||
public PlainCanal apply(String destination) {
|
||||
return new PlainCanal();
|
||||
}
|
||||
});
|
||||
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1,
|
||||
new NamedThreadFactory("canal-instance-scan"));
|
||||
new NamedThreadFactory("canal-instance-scan"));
|
||||
|
||||
private volatile boolean isFirst = true;
|
||||
private PlainCanalConfigClient configClient;
|
||||
private String ip;
|
||||
private int port;
|
||||
private String lastInstanceMD5;
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
@@ -86,7 +81,7 @@ public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle impleme
|
||||
}
|
||||
|
||||
private void scan() {
|
||||
String instances = configClient.findInstances(lastInstanceMD5);
|
||||
String instances = configClient.findInstances(null);
|
||||
final List<String> is = Lists.newArrayList(StringUtils.split(instances, ','));
|
||||
List<String> start = Lists.newArrayList();
|
||||
List<String> stop = Lists.newArrayList();
|
||||
@@ -117,7 +112,6 @@ public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle impleme
|
||||
|
||||
stop.forEach(instance -> {
|
||||
notifyStop(instance);
|
||||
configs.remove(instance);
|
||||
});
|
||||
|
||||
restart.forEach(instance -> {
|
||||
@@ -127,6 +121,7 @@ public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle impleme
|
||||
start.forEach(instance -> {
|
||||
notifyStart(instance);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void notifyStart(String destination) {
|
||||
@@ -141,11 +136,14 @@ public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle impleme
|
||||
|
||||
private void notifyStop(String destination) {
|
||||
InstanceAction action = actions.remove(destination);
|
||||
try {
|
||||
action.stop(destination);
|
||||
} catch (Throwable e) {
|
||||
logger.error(String.format("scan delete found[%s] but stop failed", destination), e);
|
||||
actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除
|
||||
if (action != null) {
|
||||
try {
|
||||
action.stop(destination);
|
||||
configs.remove(destination);
|
||||
} catch (Throwable e) {
|
||||
logger.error(String.format("scan delete found[%s] but stop failed", destination), e);
|
||||
actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,6 +158,18 @@ public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle impleme
|
||||
}
|
||||
}
|
||||
|
||||
public void release(String destination) {
|
||||
InstanceAction action = actions.remove(destination);
|
||||
if (action != null) {
|
||||
try {
|
||||
configs.remove(destination);
|
||||
} catch (Throwable e) {
|
||||
logger.error(String.format("scan delete found[%s] but stop failed", destination), e);
|
||||
actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setDefaultAction(InstanceAction defaultAction) {
|
||||
this.defaultAction = defaultAction;
|
||||
}
|
||||
@@ -172,14 +182,6 @@ public class ManagerInstanceConfigMonitor extends AbstractCanalLifeCycle impleme
|
||||
this.configClient = configClient;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Map<String, InstanceAction> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
@@ -193,12 +193,14 @@ public class SpringInstanceConfigMonitor extends AbstractCanalLifeCycle implemen
|
||||
|
||||
private void notifyStop(String destination) {
|
||||
InstanceAction action = actions.remove(destination);
|
||||
try {
|
||||
action.stop(destination);
|
||||
lastFiles.remove(destination);
|
||||
} catch (Throwable e) {
|
||||
logger.error(String.format("scan delete found[%s] but stop failed", destination), e);
|
||||
actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除
|
||||
if (action != null) {
|
||||
try {
|
||||
action.stop(destination);
|
||||
lastFiles.remove(destination);
|
||||
} catch (Throwable e) {
|
||||
logger.error(String.format("scan delete found[%s] but stop failed", destination), e);
|
||||
actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,12 @@ canal.metrics.pull.port = 11112
|
||||
# canal.user = canal
|
||||
# canal.passwd = E3619321C1A937C46A0D8BD1DAC39F93B27D4458
|
||||
|
||||
# canal admin config
|
||||
#canal.admin.manager = 127.0.0.1:8089
|
||||
canal.admin.port = 11110
|
||||
canal.admin.user = admin
|
||||
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
|
||||
|
||||
canal.zkServers =
|
||||
# flush data to zk
|
||||
canal.zookeeper.flush.period = 1000
|
||||
|
||||
@@ -8,4 +8,4 @@ canal.admin.user = admin
|
||||
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
|
||||
# admin auto register
|
||||
canal.admin.register.auto = true
|
||||
canal.admin.register.cluster =
|
||||
canal.admin.register.cluster =
|
||||
Reference in New Issue
Block a user