Skip to content

Commit

Permalink
fix commets
Browse files Browse the repository at this point in the history
  • Loading branch information
HxpSerein committed Oct 30, 2024
1 parent a7beaa5 commit 07af799
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public static void main(String[] args) throws Exception {
@PostConstruct
public void init() {
if (enableHA()) {
registryService.startListening();
registryService.registry();
registryService.doRegister();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
registryService.deRegistry();
registryService.unRegister();
log.info("RegistryService close success.");
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

public interface RegistryService {

/**
* Registry the service.
*/
void registry();

/**
* Start the registry service.
*/
void startListening();
void doRegister();

/**
* Close the registry service.
*/
void deRegistry();
void unRegister();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import java.net.InetAddress;
import java.util.HashSet;
import java.util.List;
Expand All @@ -52,7 +50,7 @@ public class RegistryServiceImpl implements RegistryService {
private static final int HEARTBEAT_INTERVAL = 10000;
private static final int HEARTBEAT_TIMEOUT = 60000;

private String zk_address;
private String zkAddress;
private ZooKeeper zk;
private String nodePath;
private Watcher watcher = event -> {
Expand All @@ -70,14 +68,10 @@ public class RegistryServiceImpl implements RegistryService {

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);

@PostConstruct
public void registry() {
if (!enableHA()) {
return;
}
try {
zk_address = SystemPropertyUtils.get("high-availability.zookeeper.quorum", "localhost:2181");
zk = new ZooKeeper(zk_address, HEARTBEAT_TIMEOUT, watcher);
zkAddress = SystemPropertyUtils.get("high-availability.zookeeper.quorum", "localhost:2181");
zk = new ZooKeeper(zkAddress, HEARTBEAT_TIMEOUT, watcher);

if (zk.exists(REGISTRY_PATH, false) == null) {
zk.create(REGISTRY_PATH, new byte[0], OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Expand All @@ -96,10 +90,7 @@ public void registry() {
}

@Override
public void startListening() {
if (!enableHA()) {
return;
}
public void doRegister() {
try {
distributedTaskService.init(currentNodes, nodePath);
startHeartbeat();
Expand Down Expand Up @@ -183,7 +174,7 @@ private void reconnectAndRegister() {
while (retries > 0) {
try {
zk.close();
zk = new ZooKeeper(zk_address, HEARTBEAT_TIMEOUT, watcher);
zk = new ZooKeeper(zkAddress, HEARTBEAT_TIMEOUT, watcher);
zk.create(nodePath, new byte[0], OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
return;
} catch (Exception e) {
Expand All @@ -200,10 +191,7 @@ private void reconnectAndRegister() {
}

@Override
public void deRegistry() {
if (!enableHA()) {
return;
}
public void unRegister() {
try {
zk.close();
scheduler.shutdown();
Expand All @@ -213,9 +201,4 @@ public void deRegistry() {
log.error("Failed to close ZooKeeper client", e);
}
}

public boolean enableHA() {
return SystemPropertyUtils.get("high-availability.enable", "false").equals("true");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ public class RegistryServiceTest {

@Test
public void testRegister() {
SystemPropertyUtils.set("high-availability.enable", "true");
registryService.registry();
Assertions.assertEquals(1, registryService.getCurrentNodes().size());
registryService.deRegistry();
if (enableHA()) {
registryService.registry();
Assertions.assertEquals(1, registryService.getCurrentNodes().size());
registryService.unRegister();
}
}

public boolean enableHA() {
return SystemPropertyUtils.get("high-availability.enable", "false").equals("true");
}

}

0 comments on commit 07af799

Please sign in to comment.