-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
99 changes: 99 additions & 0 deletions
99
...m/tencent/tsf/femas/registry/impl/zookeeper/serviceregistry/ZookeeperServiceRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters