Skip to content

Commit

Permalink
feat: support zookeeper registry
Browse files Browse the repository at this point in the history
# 270
  • Loading branch information
huyuanxin authored and zilongTong committed Nov 8, 2022
1 parent d483644 commit 5e26547
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
59 changes: 59 additions & 0 deletions femas-registry-impl/femas-registry-zookeeper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.tencent.tsf</groupId>
<artifactId>femas-registry-impl</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>femas-registry-zookeeper</artifactId>
<name>femas-registry-zookeeper</name>

<dependencies>

<dependency>
<groupId>com.tencent.tsf</groupId>
<artifactId>femas-registry</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.8.0</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.tencent.tsf.femas.registry.impl.zookeeper.serviceregistry;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.tsf.femas.common.RegistryConstants;
import com.tencent.tsf.femas.common.entity.EndpointStatus;
import com.tencent.tsf.femas.common.entity.ServiceInstance;
import com.tencent.tsf.femas.common.serviceregistry.AbstractServiceRegistry;
import org.apache.zookeeper.*;

import java.util.Map;

/**
* @author huyuanxin
*/
public class ZookeeperServiceRegistry extends AbstractServiceRegistry {

private ZooKeeper zooKeeper;
private final ObjectMapper objectMapper;

public ZookeeperServiceRegistry(Map<String, String> configMap) {
try {
String connectString = configMap.get(RegistryConstants.REGISTRY_HOST) + ":" + configMap.get(RegistryConstants.REGISTRY_PORT);
this.zooKeeper = new ZooKeeper(connectString, 3000, watchedEvent -> {
if (Watcher.Event.KeeperState.SyncConnected == watchedEvent.getState() && Watcher.Event.EventType.None == watchedEvent.getType()) {
logger.info("zookeeper server connect success!");
}
});
if (zooKeeper.exists("/femas", false) == null) {
// init
zooKeeper.create("/femas", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} catch (Exception e) {
logger.error("Error create zookeeper registry with:{0}", e);
}
objectMapper = new ObjectMapper();
}

@Override
protected void doRegister(ServiceInstance serviceInstance) {
try {
String namespace = serviceInstance.getService().getNamespace();
buildNameSpace(namespace);
String name = serviceInstance.getService().getName();
buildServiceName(namespace, name);
zooKeeper.create(
buildPath(namespace, name, serviceInstance.getId()),
objectMapper.writeValueAsBytes(serviceInstance),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL
);
logger.info("Service {} registered.", serviceInstance);
} catch (Exception e) {
logger.error("Error deregisterInstance service with zookeeper:{} ", serviceInstance, e);
}
}

@Override
protected void doDeregister(ServiceInstance serviceInstance) {
try {
String namespace = serviceInstance.getService().getNamespace();
String name = serviceInstance.getService().getName();
zooKeeper.delete(buildPath(namespace, name, serviceInstance.getId()), -1);
logger.info("Deregister service with zookeeper: {} success.", serviceInstance);
} catch (Exception e) {
logger.error("Error registering service with zookeeper: " + serviceInstance, e);
}
}

@Override
public void setStatus(ServiceInstance serviceInstance, EndpointStatus status) {
// do nothing
}

@Override
public EndpointStatus getStatus(ServiceInstance serviceInstance) {
return null;
}

private void buildPath(String path) throws InterruptedException, KeeperException {
if (zooKeeper.exists(path, false) == null) {
zooKeeper.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}

private void buildNameSpace(String nameSpace) throws InterruptedException, KeeperException {
String path = "/femas/" + nameSpace;
buildPath(path);
}

private void buildServiceName(String nameSpace, String serviceName) throws InterruptedException, KeeperException {
String path = "/femas/" + nameSpace + "/" + serviceName;
buildPath(path);
}

private String buildPath(String nameSpace, String serviceName, String id) {
return "/femas/" + nameSpace + "/" + serviceName + "/" + id;
}

}
1 change: 1 addition & 0 deletions femas-registry-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<module>femas-registry-k8s</module>
<module>femas-registry-polaris</module>
<module>femas-registry-etcd</module>
<module>femas-registry-zookeeper</module>
</modules>

<dependencies>
Expand Down

0 comments on commit 5e26547

Please sign in to comment.