Skip to content

Commit

Permalink
自动注册时可自定义node server名称,canal.properties#canal.admin.register.name,为空则…
Browse files Browse the repository at this point in the history
…使用canal.register.ip (alibaba#3459)
  • Loading branch information
zoemak authored Apr 19, 2021
1 parent f041750 commit 17f4ee1
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ public class PollingConfigController {
public BaseModel<CanalConfig> canalConfigPoll(@RequestHeader String user, @RequestHeader String passwd,
@RequestParam String ip, @RequestParam Integer port,
@RequestParam String md5, @RequestParam boolean register,
@RequestParam String cluster, @PathVariable String env) {
@RequestParam String cluster, @RequestParam String name,
@PathVariable String env) {
if (!auth(user, passwd)) {
throw new RuntimeException("auth :" + user + " is failed");
}

if (StringUtils.isEmpty(md5) && register) {
// do something
pollingConfigService.autoRegister(ip, port, cluster);
pollingConfigService.autoRegister(ip, port, cluster, StringUtils.trimToNull(name));
}

CanalConfig canalConfig = pollingConfigService.getChangedConfig(ip, port, md5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public interface PollingConfigService {

public boolean autoRegister(String ip, Integer adminPort, String cluster);
public boolean autoRegister(String ip, Integer adminPort, String cluster, String name);

CanalConfig getChangedConfig(String ip, Integer port, String md5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
Expand All @@ -28,11 +29,11 @@ public class PollingConfigServiceImpl implements PollingConfigService {
@Autowired
CanalClusterService canalClusterService;

public boolean autoRegister(String ip, Integer adminPort, String cluster) {
public boolean autoRegister(String ip, Integer adminPort, String cluster, String name) {
NodeServer server = NodeServer.find.query().where().eq("ip", ip).eq("adminPort", adminPort).findOne();
if (server == null) {
server = new NodeServer();
server.setName(ip);
server.setName(Optional.ofNullable(name).orElse(ip));
server.setIp(ip);
server.setAdminPort(adminPort);
server.setTcpPort(adminPort + 1);
Expand Down
4 changes: 4 additions & 0 deletions admin/admin-web/src/main/resources/canal-template.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ canal.metrics.pull.port = 11112
canal.admin.port = 11110
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# admin auto register
#canal.admin.register.auto = true
#canal.admin.register.cluster =
#canal.admin.register.name =

canal.zkServers =
# flush data to zk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CanalConstants {
public static final String CANAL_ADMIN_PASSWD = ROOT + "." + "admin.passwd";
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_ADMIN_REGISTER_NAME = ROOT + "." + "admin.register.name";
public static final String CANAL_ZKSERVERS = ROOT + "." + "zkServers";
public static final String CANAL_WITHOUT_NETTY = ROOT + "." + "withoutNetty";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void main(String[] args) {
boolean autoRegister = BooleanUtils.toBoolean(CanalController.getProperty(properties,
CanalConstants.CANAL_ADMIN_AUTO_REGISTER));
String autoCluster = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_AUTO_CLUSTER);
String name = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_REGISTER_NAME);
String registerIp = CanalController.getProperty(properties, CanalConstants.CANAL_REGISTER_IP);
if (StringUtils.isEmpty(registerIp)) {
registerIp = AddressUtils.getHostIp();
Expand All @@ -65,7 +66,8 @@ public static void main(String[] args) {
registerIp,
Integer.parseInt(adminPort),
autoRegister,
autoCluster);
autoCluster,
name);
PlainCanal canalConfig = configClient.findServer(null);
if (canalConfig == null) {
throw new IllegalArgumentException("managerAddress:" + managerAddress
Expand Down
3 changes: 2 additions & 1 deletion deployer/src/main/resources/canal_local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# admin auto register
canal.admin.register.auto = true
canal.admin.register.cluster =
canal.admin.register.cluster =
canal.admin.register.name =
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public class PlainCanalConfigClient extends AbstractCanalLifeCycle implements Ca
private int adminPort;
private boolean autoRegister;
private String autoCluster;
private String name;

public PlainCanalConfigClient(String configURL, String user, String passwd, String localIp, int adminPort,
boolean autoRegister, String autoCluster){
boolean autoRegister, String autoCluster, String name){
this(configURL, user, passwd, localIp, adminPort);
this.autoCluster = autoCluster;
this.autoRegister = autoRegister;
this.name = name;
}

public PlainCanalConfigClient(String configURL, String user, String passwd, String localIp, int adminPort){
Expand Down Expand Up @@ -71,7 +73,7 @@ public PlainCanal findServer(String md5) {
md5 = "";
}
String url = configURL + "/api/v1/config/server_polling?ip=" + localIp + "&port=" + adminPort + "&md5=" + md5
+ "&register=" + (autoRegister ? 1 : 0) + "&cluster=" + autoCluster;
+ "&register=" + (autoRegister ? 1 : 0) + "&cluster=" + autoCluster + "&name=" + name;
return queryConfig(url);
}

Expand Down

0 comments on commit 17f4ee1

Please sign in to comment.