2018-08-23 11:07:25.0|分类: docker+node.js+zookeeper构建微服务|浏览量: 1334
服务实例开启后,需要将服务器的ip+端口等信息注册到zookeeper,zookeeper保存每个服务器的信息,当调用者请求zookeeper,获取服务实例列表,客户端再去访问服务实例。 zookeeper节点设计模型 /registry /registry/testService /registry/helloService 是持久节点。 /registry/testService/000000001 是临时持久节点,当zookeeper连接客户端关闭后,这个节点会自动删除。 创建mvn项目registration-service pom.xml配置信息 <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cookqq</groupId> <artifactId>registration-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>registration-service</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent>
<repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> <repository> <id>jcenter</id> <name>jcenter Repository</name> <url>http://jcenter.bintray.com/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> </dependencies> </project> 定义ServiceRegistry接口 package com.cookqq.registry; public interface ServiceRegistry { /** * @param name 服务名字 * @param adress 服务地址 */ void register(String name, String adress); } 注册实现类 package com.cookqq.registry; import java.io.IOException; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class ServiceRegistryImpl implements ServiceRegistry,Watcher { private static Logger logger = LoggerFactory.getLogger(ServiceRegistryImpl.class); private static CountDownLatch latch = new CountDownLatch(1); private ZooKeeper zk; public ServiceRegistryImpl(){ } public ServiceRegistryImpl(String zkAdress){ try { logger.error("zkAdress is "+zkAdress); zk = new ZooKeeper(zkAdress, SESSION_TIMEOUT, this); latch.await(); logger.debug("connected to zookeeper"); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } private static final String REGISTRY = "/registry"; private static final int SESSION_TIMEOUT = 5000; /** * @param name 服务名字 * @param adress 服务地址 */ @Override public void register(String name, String adress) { try { //创建根节点-持久节点 Stat stat = zk.exists(REGISTRY, null); if(stat==null){ //根节点不存在,则创建 zk.create(REGISTRY, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); logger.debug("creating registry node:{}"); }else{ logger.debug("registry node already exits "); } //创建服务节点 String servicePath = REGISTRY+"/"+name; stat = zk.exists(servicePath, null); if(stat==null){ //根节点不存在,则创建 zk.create(servicePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); logger.debug("creating "+servicePath+" node:{}"); }else{ logger.debug(servicePath+" node already exits "); } //创建地址节点-临时顺序节点 String addressPath = servicePath + "/address-"; String addressNode = zk.create(addressPath, adress.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); logger.debug("creating address node:"+addressNode); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public void process(WatchedEvent event) { // TODO Auto-generated method stub if(event.getState()==Event.KeeperState.SyncConnected){ latch.countDown(); } } } 启动类 package com.cookqq.registry; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" );
String zkAdress = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"; ServiceRegistry serviceRegistry = new ServiceRegistryImpl(zkAdress); serviceRegistry.register("testService", "127.0.0.1:80");
try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); }
} } 验证服务是否注册到zookeeper |