@@ -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 }
0 commit comments