Zookeeper Demo

package com.example.demo.resource;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class ZookeeperTest {

	static final String address = "192.168.1.24:2181,192.168.1.25:2181,192.168.1.26:2181";
	static final int session_timeout = 5000;
	static final CountDownLatch countDownLatch = new CountDownLatch(1);
	public static void main(String[] args) throws Exception {
		ZooKeeper zk = new ZooKeeper(address,session_timeout,new Watcher() {

			@Override
			public void process(WatchedEvent event) {
				KeeperState keeperState =  event.getState();
				EventType eventType = event.getType();
				if(KeeperState.SyncConnected == keeperState) {
					if(EventType.None == eventType) {
						countDownLatch.countDown();
						System.out.println("zk 建立连接");
					}
				}
			}
			
		});
		countDownLatch.await();
		//同步创建
		String returnStr = zk.create("/test", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println(returnStr);
		
		
		//查询数据
		System.out.println(zk.getData("/test", false, null).toString());
		
		//异步删除
		zk.delete("/test", -1,new AsyncCallback.VoidCallback() {
			
			@Override
			public void processResult(int rc, String path, Object ctx) {
				System.out.println(rc);
				System.out.println(path);
				System.out.println(ctx);
				
			}
		}, "a");
		
		Thread.sleep(5000);
		zk.close();
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Spring Boot整合ZookeeperDemo代码。 首先,需要在pom.xml文件中添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> ``` 然后,在application.properties文件中配置Zookeeper的连接地址和端口号: ``` spring.cloud.zookeeper.connect-string=localhost:2181 ``` 接下来,创建一个ZookeeperServiceDiscovery类,用于向Zookeeper注册服务: ```java import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.stereotype.Component; @Component public class ZookeeperServiceDiscovery implements ServiceRegistry<Registration> { @Override public void register(Registration registration) { // 获取服务的名称和地址 String serviceName = registration.getServiceId(); String serviceAddress = registration.getHost() + ":" + registration.getPort(); // 向Zookeeper注册服务 // 这里可以使用ZooKeeper客户端库,也可以使用Curator框架 // 这里使用Curator框架进行注册 CuratorFramework client = CuratorFrameworkFactory.newClient( "localhost:2181", new RetryNTimes(3, 1000) ); client.start(); try { String path = "/services/" + serviceName + "/" + serviceAddress; client.create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(path); } catch (Exception e) { e.printStackTrace(); } } @Override public void deregister(Registration registration) { // 获取服务的名称和地址 String serviceName = registration.getServiceId(); String serviceAddress = registration.getHost() + ":" + registration.getPort(); // 从Zookeeper注销服务 // 这里可以使用ZooKeeper客户端库,也可以使用Curator框架 // 这里使用Curator框架进行注销 CuratorFramework client = CuratorFrameworkFactory.newClient( "localhost:2181", new RetryNTimes(3, 1000) ); client.start(); try { String path = "/services/" + serviceName + "/" + serviceAddress; client.delete().forPath(path); } catch (Exception e) { e.printStackTrace(); } } @Override public void close() { } @Override public void setStatus(Registration registration, String status) { } @Override public <T> T getStatus(Registration registration) { return null; } @Override public Iterable<Registration> getRegistrations() { return null; } @Override public Registration getRegistration(String s) { return null; } } ``` 最后,在启动类中加入@EnableDiscoveryClient注解,启用服务注册和发现功能: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样就完成了一个简单的Spring Boot整合ZookeeperDemo。注意,这里只是演示了服务注册功能,如果需要使用服务发现功能,还需要在客户端中加入@EnableDiscovery注解,并使用@LoadBalanced注解来实现负载均衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值