Skip to content

Commit 3a74f61

Browse files
committed
fixed issue #1940 #2088 , add canal.register.ip support docker
1 parent bd974e3 commit 3a74f61

File tree

4 files changed

+99
-83
lines changed

4 files changed

+99
-83
lines changed

deployer/src/main/java/com/alibaba/otter/canal/deployer/CanalConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class CanalConstants {
1414
public static final String ROOT = "canal";
1515
public static final String CANAL_ID = ROOT + "." + "id";
1616
public static final String CANAL_IP = ROOT + "." + "ip";
17+
public static final String CANAL_REGISTER_IP = ROOT + "." + "register.ip";
1718
public static final String CANAL_PORT = ROOT + "." + "port";
1819
public static final String CANAL_METRICS_PULL_PORT = ROOT + "." + "metrics.pull.port";
1920
public static final String CANAL_ADMIN_JMX_PORT = ROOT + "." + "admin.jmx.port";

deployer/src/main/java/com/alibaba/otter/canal/deployer/CanalController.java

Lines changed: 85 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class CanalController {
5454
private static final Logger logger = LoggerFactory.getLogger(CanalController.class);
5555
private Long cid;
5656
private String ip;
57+
private String registerIp;
5758
private int port;
5859
// 默认使用spring的方式载入
5960
private Map<String, InstanceConfig> instanceConfigs;
@@ -108,6 +109,7 @@ public CanalConfigClient apply(String managerAddress) {
108109
// 准备canal server
109110
cid = Long.valueOf(getProperty(properties, CanalConstants.CANAL_ID));
110111
ip = getProperty(properties, CanalConstants.CANAL_IP);
112+
registerIp = getProperty(properties, CanalConstants.CANAL_REGISTER_IP);
111113
port = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_PORT));
112114
embededCanalServer = CanalServerWithEmbedded.instance();
113115
embededCanalServer.setCanalInstanceGenerator(instanceGenerator);// 设置自定义的instanceGenerator
@@ -127,9 +129,17 @@ public CanalConfigClient apply(String managerAddress) {
127129
}
128130

129131
// 处理下ip为空,默认使用hostIp暴露到zk中
132+
if (StringUtils.isEmpty(ip) && StringUtils.isEmpty(registerIp)) {
133+
ip = registerIp = AddressUtils.getHostIp();
134+
}
135+
130136
if (StringUtils.isEmpty(ip)) {
131137
ip = AddressUtils.getHostIp();
132138
}
139+
140+
if (StringUtils.isEmpty(registerIp)) {
141+
registerIp = ip; // 兼容以前配置
142+
}
133143
final String zkServers = getProperty(properties, CanalConstants.CANAL_ZKSERVERS);
134144
if (StringUtils.isNotEmpty(zkServers)) {
135145
zkclientx = ZkClientx.getZkClient(zkServers);
@@ -138,89 +148,88 @@ public CanalConfigClient apply(String managerAddress) {
138148
zkclientx.createPersistent(ZookeeperPathUtils.CANAL_CLUSTER_ROOT_NODE, true);
139149
}
140150

141-
final ServerRunningData serverData = new ServerRunningData(cid, ip + ":" + port);
151+
final ServerRunningData serverData = new ServerRunningData(cid, registerIp + ":" + port);
142152
ServerRunningMonitors.setServerData(serverData);
143-
ServerRunningMonitors
144-
.setRunningMonitors(MigrateMap.makeComputingMap(new Function<String, ServerRunningMonitor>() {
145-
146-
public ServerRunningMonitor apply(final String destination) {
147-
ServerRunningMonitor runningMonitor = new ServerRunningMonitor(serverData);
148-
runningMonitor.setDestination(destination);
149-
runningMonitor.setListener(new ServerRunningListener() {
150-
151-
public void processActiveEnter() {
152-
try {
153-
MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination));
154-
embededCanalServer.start(destination);
155-
if (canalMQStarter != null) {
156-
canalMQStarter.startDestination(destination);
157-
}
158-
} finally {
159-
MDC.remove(CanalConstants.MDC_DESTINATION);
153+
ServerRunningMonitors.setRunningMonitors(MigrateMap.makeComputingMap(new Function<String, ServerRunningMonitor>() {
154+
155+
public ServerRunningMonitor apply(final String destination) {
156+
ServerRunningMonitor runningMonitor = new ServerRunningMonitor(serverData);
157+
runningMonitor.setDestination(destination);
158+
runningMonitor.setListener(new ServerRunningListener() {
159+
160+
public void processActiveEnter() {
161+
try {
162+
MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination));
163+
embededCanalServer.start(destination);
164+
if (canalMQStarter != null) {
165+
canalMQStarter.startDestination(destination);
160166
}
167+
} finally {
168+
MDC.remove(CanalConstants.MDC_DESTINATION);
161169
}
170+
}
162171

163-
public void processActiveExit() {
164-
try {
165-
MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination));
166-
if (canalMQStarter != null) {
167-
canalMQStarter.stopDestination(destination);
168-
}
169-
embededCanalServer.stop(destination);
170-
} finally {
171-
MDC.remove(CanalConstants.MDC_DESTINATION);
172+
public void processActiveExit() {
173+
try {
174+
MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination));
175+
if (canalMQStarter != null) {
176+
canalMQStarter.stopDestination(destination);
172177
}
178+
embededCanalServer.stop(destination);
179+
} finally {
180+
MDC.remove(CanalConstants.MDC_DESTINATION);
173181
}
182+
}
174183

175-
public void processStart() {
176-
try {
177-
if (zkclientx != null) {
178-
final String path = ZookeeperPathUtils.getDestinationClusterNode(destination,
179-
ip + ":" + port);
180-
initCid(path);
181-
zkclientx.subscribeStateChanges(new IZkStateListener() {
182-
183-
public void handleStateChanged(KeeperState state) throws Exception {
184-
185-
}
186-
187-
public void handleNewSession() throws Exception {
188-
initCid(path);
189-
}
190-
191-
@Override
192-
public void handleSessionEstablishmentError(Throwable error) throws Exception {
193-
logger.error("failed to connect to zookeeper", error);
194-
}
195-
});
196-
}
197-
} finally {
198-
MDC.remove(CanalConstants.MDC_DESTINATION);
184+
public void processStart() {
185+
try {
186+
if (zkclientx != null) {
187+
final String path = ZookeeperPathUtils.getDestinationClusterNode(destination,
188+
registerIp + ":" + port);
189+
initCid(path);
190+
zkclientx.subscribeStateChanges(new IZkStateListener() {
191+
192+
public void handleStateChanged(KeeperState state) throws Exception {
193+
194+
}
195+
196+
public void handleNewSession() throws Exception {
197+
initCid(path);
198+
}
199+
200+
@Override
201+
public void handleSessionEstablishmentError(Throwable error) throws Exception {
202+
logger.error("failed to connect to zookeeper", error);
203+
}
204+
});
199205
}
206+
} finally {
207+
MDC.remove(CanalConstants.MDC_DESTINATION);
200208
}
209+
}
201210

202-
public void processStop() {
203-
try {
204-
MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination));
205-
if (zkclientx != null) {
206-
final String path = ZookeeperPathUtils.getDestinationClusterNode(destination,
207-
ip + ":" + port);
208-
releaseCid(path);
209-
}
210-
} finally {
211-
MDC.remove(CanalConstants.MDC_DESTINATION);
211+
public void processStop() {
212+
try {
213+
MDC.put(CanalConstants.MDC_DESTINATION, String.valueOf(destination));
214+
if (zkclientx != null) {
215+
final String path = ZookeeperPathUtils.getDestinationClusterNode(destination,
216+
registerIp + ":" + port);
217+
releaseCid(path);
212218
}
219+
} finally {
220+
MDC.remove(CanalConstants.MDC_DESTINATION);
213221
}
214-
215-
});
216-
if (zkclientx != null) {
217-
runningMonitor.setZkClient(zkclientx);
218222
}
219-
// 触发创建一下cid节点
220-
runningMonitor.init();
221-
return runningMonitor;
223+
224+
});
225+
if (zkclientx != null) {
226+
runningMonitor.setZkClient(zkclientx);
222227
}
223-
}));
228+
// 触发创建一下cid节点
229+
runningMonitor.init();
230+
return runningMonitor;
231+
}
232+
}));
224233

225234
// 初始化monitor机制
226235
autoScan = BooleanUtils.toBoolean(getProperty(properties, CanalConstants.CANAL_AUTO_SCAN));
@@ -266,8 +275,7 @@ public void reload(String destination) {
266275
instanceConfigMonitors = MigrateMap.makeComputingMap(new Function<InstanceMode, InstanceConfigMonitor>() {
267276

268277
public InstanceConfigMonitor apply(InstanceMode mode) {
269-
int scanInterval = Integer
270-
.valueOf(getProperty(properties, CanalConstants.CANAL_AUTO_SCAN_INTERVAL));
278+
int scanInterval = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_AUTO_SCAN_INTERVAL));
271279

272280
if (mode.isSpring()) {
273281
SpringInstanceConfigMonitor monitor = new SpringInstanceConfigMonitor();
@@ -375,8 +383,7 @@ private void initInstanceConfig(Properties properties) {
375383
InstanceConfig oldConfig = instanceConfigs.put(destination, config);
376384

377385
if (oldConfig != null) {
378-
logger
379-
.warn("destination:{} old config:{} has replace by new config:{}", destination, oldConfig, config);
386+
logger.warn("destination:{} old config:{} has replace by new config:{}", destination, oldConfig, config);
380387
}
381388
}
382389
}
@@ -424,9 +431,9 @@ public static String getProperty(Properties properties, String key) {
424431
}
425432

426433
public void start() throws Throwable {
427-
logger.info("## start the canal server[{}:{}]", ip, port);
434+
logger.info("## start the canal server[{}({}):{}]", ip, registerIp, port);
428435
// 创建整个canal的工作节点
429-
final String path = ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port);
436+
final String path = ZookeeperPathUtils.getCanalClusterNode(registerIp + ":" + port);
430437
initCid(path);
431438
if (zkclientx != null) {
432439
this.zkclientx.subscribeStateChanges(new IZkStateListener() {
@@ -501,14 +508,14 @@ public void stop() throws Throwable {
501508
}
502509

503510
// 释放canal的工作节点
504-
releaseCid(ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port));
505-
logger.info("## stop the canal server[{}:{}]", ip, port);
511+
releaseCid(ZookeeperPathUtils.getCanalClusterNode(registerIp + ":" + port));
512+
logger.info("## stop the canal server[{}({}):{}]", ip, registerIp, port);
506513

507514
if (zkclientx != null) {
508515
zkclientx.close();
509516
}
510517

511-
//关闭时清理缓存
518+
// 关闭时清理缓存
512519
if (instanceConfigs != null) {
513520
instanceConfigs.clear();
514521
}

deployer/src/main/java/com/alibaba/otter/canal/deployer/CanalLauncher.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import java.util.Properties;
55
import java.util.concurrent.CountDownLatch;
66

7-
import com.alibaba.otter.canal.deployer.mbean.CanalServerAgent;
8-
import com.alibaba.otter.canal.deployer.mbean.CanalServerMXBean;
9-
import com.alibaba.otter.canal.deployer.mbean.CanalServerBean;
107
import org.apache.commons.lang.StringUtils;
118
import org.slf4j.Logger;
129
import org.slf4j.LoggerFactory;
1310

11+
import com.alibaba.otter.canal.deployer.mbean.CanalServerAgent;
12+
import com.alibaba.otter.canal.deployer.mbean.CanalServerBean;
13+
import com.alibaba.otter.canal.deployer.mbean.CanalServerMXBean;
14+
import com.alibaba.otter.canal.deployer.monitor.remote.RemoteCanalConfigMonitor;
1415
import com.alibaba.otter.canal.deployer.monitor.remote.RemoteConfigLoader;
1516
import com.alibaba.otter.canal.deployer.monitor.remote.RemoteConfigLoaderFactory;
16-
import com.alibaba.otter.canal.deployer.monitor.remote.RemoteCanalConfigMonitor;
1717

1818
/**
1919
* canal独立版本启动的入口类
@@ -80,8 +80,13 @@ public void onChange(Properties properties) {
8080
String jmxPort = properties.getProperty(CanalConstants.CANAL_ADMIN_JMX_PORT);
8181
if (StringUtils.isNotEmpty(jmxPort)) {
8282
String ip = properties.getProperty(CanalConstants.CANAL_IP);
83+
String registerIp = properties.getProperty(CanalConstants.CANAL_REGISTER_IP);
84+
if (StringUtils.isEmpty(registerIp)) {
85+
// 兼容老的配置
86+
registerIp = ip;
87+
}
8388
CanalServerMXBean canalServerMBean = new CanalServerBean(canalStater);
84-
canalServerAgent = new CanalServerAgent(ip, Integer.parseInt(jmxPort), canalServerMBean);
89+
canalServerAgent = new CanalServerAgent(registerIp, Integer.parseInt(jmxPort), canalServerMBean);
8590
Thread agentThread = new Thread(canalServerAgent::start);
8691
agentThread.start();
8792
}

deployer/src/main/resources/canal.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#canal.manager.jdbc.username=root
66
#canal.manager.jdbc.password=121212
77
canal.id = 1
8+
# tcp bind ip
89
canal.ip =
10+
# register ip to zookeeper
11+
canal.register.ip =
912
canal.port = 11111
1013
canal.metrics.pull.port = 11112
1114
canal.admin.jmx.port = 11113

0 commit comments

Comments
 (0)