2.软文推荐
3.软文推荐
目录: 1、dubbo使用zookeeper连接,zookeeper宕机后怎么处理 2、如何使用Cruator监听zookeeper事件变化 3、2021-04-22 4、Curator简介 5、curator里面的NodeCache监听节点,怎么获取触发事件 6、dubbo使用zookeeper连接,zookeeper宕机后怎么处理? dubbo使用zookeeper连接,zookeeper宕机后怎么处理zookeeper宕机后,因为消费者会缓存提供者的信息,所以应用不会有问题。但是,此时提供者和消费者都无法重连zookeeper,因为dubbo貌似配置的zkclient不会重连zookeeper,所以一旦重启一台服务提供者,那么这台就从服务消费者的缓存中消失了,此时服务消费者又连不上zookeeper,所以如果同时重启,消费者就没有提供者可用了,所以只能重启一台提供者后,再重启一个消费者,交错重启。
如何使用Cruator监听zookeeper事件变化掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括:
1,配置文件同步
2,主从切换
3,分布式队列
4,分布式锁
5,其他
散仙,在以前的文章里面有写过使用zookeeper原生的api,监听zk节点变化,那么本篇我们就来看下,如何使用curator来完成监听,代码如下:
pre name="code" class="java"package com.qin.curator.zk;
import javax.sound.midi.Patch;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
2021-04-22Last login: Tue Apr 20 14:55:45 on ttys002
The default interactive shell is now zsh.
To update your account to use zsh, please run chsh -s /bin/zsh .
For more details, please visit .
foxconndeMac-mini:~ foxconn cd System/Library/L
LASecureIO/ LaunchDaemons/ LoginPlugins/
LaunchAgents/ LinguisticData/
LaunchAgentsIgnored/ LocationBundles/
foxconndeMac-mini:/ foxconn ls
bootps.plist
com.apple.AirPlayXPCHelper.plist
com.apple.AppleFileServer.plist
com.apple.AppleQEMUGuestAgent.plist
com.apple.AssetCache.builtin.plist
com.apple.AssetCacheLocatorService.plist
com.apple.AssetCacheManagerService.plist
com.apple.AssetCacheTetheratorService.plist
com.apple.CSCSupportd.plist
com.apple.CommCenterRootHelper.plist
com.apple.CoreAuthentication.daemon.plist
com.apple.CrashReporterSupportHelper.plist
com.apple.CryptoTokenKit.ahp.plist
com.apple.DataDetectorsSourceAccess.plist
com.apple.DesktopServicesHelper.plist
com.apple.DumpGPURestart.plist
com.apple.DumpPanic.plist
com.apple.FileCoordination.plist
com.apple.FontWorker.plist
com.apple.GSSCred.plist
com.apple.GameController.gamecontrollerd.plist
com.apple.IFCStart.plist
com.apple.IOAccelMemoryInfoCollector.plist
com.apple.IOBluetoothUSBDFU.plist
com.apple.InstallerDiagnostics.installerdiagd.plist
com.apple.InstallerDiagnostics.installerdiagwatcher.plist
com.apple.InstallerProgress.plist
com.apple.Kerberos.digest-service.plist
com.apple.Kerberos.kadmind.plist
com.apple.Kerberos.kcm.plist
com.apple.Kerberos.kdc.plist
com.apple.Kerberos.kpasswdd.plist
com.apple.KernelEventAgent.plist
com.apple.ManagedClient.cloudconfigurationd.plist
com.apple.ManagedClient.enroll.plist
com.apple.ManagedClient.plist
com.apple.ManagedClient.startup.plist
com.apple.MobileAccessoryUpdater.plist
com.apple.MobileFileIntegrity.plist
com.apple.NetworkLinkConditioner.plist
com.apple.NetworkSharing.plist
com.apple.ODSAgent.plist
com.apple.PasswordService.plist
com.apple.PerfPowerServices.plist
com.apple.PerfPowerServicesExtended.plist
com.apple.RFBEventHelper.plist
com.apple.RemoteDesktop.PrivilegeProxy.plist
com.apple.ReportCrash.Root.plist
com.apple.ReportMemoryException.plist
com.apple.SCHelper.plist
com.apple.SafeEjectGPUStartupDaemon.plist
com.apple.SubmitDiagInfo.plist
com.apple.TrustEvaluationAgent.system.plist
com.apple.UpdateSettings.plist
com.apple.UserEventAgent-System.plist
com.apple.UserNotificationCenter.plist
com.apple.WindowServer.plist
com.apple.WirelessRadioManager-osx.plist
com.apple.accessoryd.plist
com.apple.adid.plist
com.apple.afpfs_afpLoad.plist
com.apple.afpfs_checkafp.plist
com.apple.airport.wps.plist
com.apple.airportd.plist
com.apple.akd.plist
com.apple.alf.agent.plist
com.apple.analyticsd.plist
com.apple.apfsd.plist
com.apple.applefileutil.plist
com.apple.appleseed.fbahelperd.plist
com.apple.applessdstatistics.plist
com.apple.appstored.plist
com.apple.apsd.plist
com.apple.aslmanager.plist
com.apple.atrun.plist
com.apple.audio.AudioComponentRegistrar.daemon.plist
com.apple.audio.coreaudiod.plist
com.apple.audio.systemsoundserverd.plist
com.apple.auditd.plist
com.apple.autofsd.plist
com.apple.automountd.plist
com.apple.avbdeviced.plist
com.apple.awdd.plist
com.apple.backupd-helper.plist
com.apple.backupd.plist
com.apple.biokitaggdd.plist
com.apple.biometrickitd.plist
com.apple.bluetoothReporter.plist
com.apple.bluetoothaudiod.plist
com.apple.bluetoothd.plist
com.apple.bnepd.plist
com.apple.bootinstalld.plist
com.apple.bosreporter.plist
com.apple.boswatcher.plist
com.apple.bridgeOSUpdateProxy.plist
com.apple.bsd.dirhelper.plist
com.apple.captiveagent.plist
com.apple.cfnetwork.cfnetworkagent.plist
com.apple.cfprefsd.xpc.daemon.plist
com.apple.ckdiscretionaryd.plist
com.apple.cmio.AVCAssistant.plist
com.apple.cmio.AppleCameraAssistant.plist
com.apple.cmio.IIDCVideoAssistant.plist
com.apple.cmio.VDCAssistant.plist
com.apple.cmio.iOSScreenCaptureAssistant.plist
com.apple.colorsync.displayservices.plist
com.apple.colorsyncd.plist
com.apple.commerced.plist
com.apple.comsat.plist
com.apple.configd.plist
com.apple.configureLocalKDC.plist
com.apple.contextstored.plist
com.apple.corebrightnessd.plist
com.apple.corecaptured.plist
com.apple.coreduetd.osx.plist
com.apple.coreservices.appleevents.plist
com.apple.coreservices.launchservicesd.plist
com.apple.coreservices.sharedfilelistd.plist
com.apple.coreservicesd.plist
com.apple.corestorage.corestoraged.plist
com.apple.corestorage.corestoragehelperd.plist
com.apple.coresymbolicationd.plist
com.apple.csrutil.report.plist
com.apple.ctkd.plist
com.apple.cvmsServ.plist
com.apple.dasd-OSX.plist
com.apple.datastored.plist
com.apple.deleted_helper.plist
com.apple.dhcp6d.plist
com.apple.diagnosticd.plist
com.apple.diagnosticextensions.osx.getmobilityinfo.helper.plist
com.apple.diagnosticextensions.osx.spotlight.helper.plist
com.apple.diagnosticextensions.osx.timemachine.helper.plist
com.apple.diagnosticextensions.osx.wifi.helper.plist
com.apple.diskarbitrationd.plist
com.apple.diskmanagementd.plist
com.apple.diskmanagementstartup.plist
com.apple.displaypolicyd.plist
com.apple.distnoted.xpc.daemon.plist
com.apple.dpaudiothru.plist
com.apple.dpd.plist
com.apple.dprivacyd.plist
com.apple.driver.eficheck.plist
com.apple.driver.ethcheck.plist
com.apple.dspluginhelperd.plist
com.apple.dvdplayback.setregion.plist
com.apple.dynamic_pager.plist
com.apple.eapolcfg_auth.plist
com.apple.efilogin-helper.plist
com.apple.emlog.plist
com.apple.emond.aslmanager.plist
com.apple.emond.plist
com.apple.endpointsecurity.endpointsecurityd.plist
com.apple.eoshostd.plist
com.apple.eppc.plist
com.apple.familycontrols.plist
com.apple.findmymac.plist
com.apple.findmymacmessenger.plist
com.apple.firmwaresyncd.plist
com.apple.fontd.plist
com.apple.fontmover.plist
com.apple.fpsd.plist
com.apple.fseventsd.plist
com.apple.ftp-proxy.plist
com.apple.getty.plist
com.apple.gkreport.plist
com.apple.gssd.plist
com.apple.hdiejectd.plist
com.apple.hidd.plist
com.apple.icloud.findmydeviced.plist
com.apple.icloud.searchpartyd.plist
com.apple.iconservices.iconservicesagent.plist
com.apple.iconservices.iconservicesd.plist
com.apple.ifdreader.plist
com.apple.installandsetup.systemmigrationd.plist
com.apple.installd.plist
com.apple.ionodecache.plist
com.apple.jetsamproperties.Mac.plist
com.apple.kcproxy.plist
com.apple.kdumpd.plist
com.apple.kextd.plist
com.apple.kuncd.plist
com.apple.locate.plist
com.apple.locationd.plist
com.apple.lockd.plist
com.apple.logd.plist
com.apple.logind.plist
com.apple.loginwindow.plist
com.apple.logkextloadsd.plist
com.apple.lsd.plist
com.apple.lskdd.plist
com.apple.mDNSResponder.plist
com.apple.mDNSResponderHelper.plist
com.apple.managedconfiguration.teslad.plist
com.apple.mbsystemadministration.plist
com.apple.mbusertrampoline.plist
com.apple.mdmclient.daemon.plist
com.apple.mdmclient.daemon.runatboot.plist
com.apple.mediaremoted.plist
com.apple.metadata.mds.index.plist
com.apple.metadata.mds.plist
com.apple.metadata.mds.scan.plist
com.apple.metadata.mds.spindump.plist
com.apple.metrickitd.plist
com.apple.mobile.keybagd.plist
com.apple.mobileactivationd.plist
com.apple.mobileassetd.plist
com.apple.msrpc.echosvc.plist
com.apple.msrpc.lsarpc.plist
com.apple.msrpc.mdssvc.plist
com.apple.msrpc.netlogon.plist
com.apple.msrpc.srvsvc.plist
com.apple.msrpc.wkssvc.plist
com.apple.multiversed.plist
com.apple.nehelper.plist
com.apple.nesessionmanager.plist
com.apple.netauth.sys.auth.plist
com.apple.netauth.sys.gui.plist
com.apple.netbiosd.plist
com.apple.newsyslog.plist
com.apple.nfcd.plist
com.apple.nfrestore.plist
com.apple.nfsconf.plist
com.apple.nfsd.plist
com.apple.noticeboard.state.plist
com.apple.notifyd.plist
com.apple.nsurlsessiond.plist
com.apple.nsurlstoraged.plist
com.apple.ocspd.plist
com.apple.odproxyd.plist
com.apple.opendirectoryd.plist
com.apple.osanalytics.osanalyticshelper.plist
com.apple.periodic-daily.plist
com.apple.periodic-monthly.plist
com.apple.periodic-weekly.plist
com.apple.pfctl.plist
com.apple.pfd.plist
com.apple.postfix.master.plist
com.apple.postfix.newaliases.plist
com.apple.powerd.plist
com.apple.powerd.swd.plist
com.apple.preferences.timezone.admintool.plist
com.apple.printtool.daemon.plist
com.apple.racoon.plist
com.apple.rapportd.plist
com.apple.recoverylogd.plist
com.apple.remotepairtool.plist
com.apple.revisiond.plist
com.apple.rpcbind.plist
com.apple.rtcreportingd.plist
com.apple.runningboardd.plist
com.apple.sandboxd.plist
com.apple.screensharing.plist
com.apple.scsid.plist
com.apple.secinitd.plist
com.apple.securechanneld.plist
com.apple.security.FDERecoveryAgent.plist
com.apple.security.agent.login.plist
com.apple.security.authhost.plist
com.apple.security.authtrampoline.plist
com.apple.security.syspolicy.plist
com.apple.securityd.plist
com.apple.securityd_service.plist
com.apple.seld.plist
com.apple.sessionlogoutd.plist
com.apple.signpost.signpost_reporter.plist
com.apple.smb.preferences.plist
com.apple.smbd.plist
com.apple.softwareupdate_firstrun_tasks.plist
com.apple.softwareupdated.plist
com.apple.speech.speechsynthesisd.plist
com.apple.spindump.plist
com.apple.startupdiskhelper.plist
com.apple.statd.notify.plist
com.apple.storagekitd.plist
com.apple.storeaccountd.daemon.plist
com.apple.storeagent.daemon.plist
com.apple.storeassetd.daemon.plist
com.apple.storedownloadd.daemon.plist
com.apple.storereceiptinstaller.plist
com.apple.suhelperd.plist
com.apple.symptomsd.plist
com.apple.sysdiagnose.plist
com.apple.sysdiagnose_helper.plist
com.apple.sysextd.plist
com.apple.syslogd.plist
com.apple.sysmond.plist
com.apple.system_installd.plist
com.apple.systemkeychain.plist
com.apple.systemstats.analysis.plist
com.apple.systemstats.daily.plist
com.apple.systemstats.microstackshot_periodic.plist
com.apple.tailspind.plist
com.apple.taskgated-helper.plist
com.apple.taskgated.plist
com.apple.tccd.system.plist
com.apple.testmanagerd.plist
com.apple.thermald.plist
com.apple.timed.plist
com.apple.timezoneupdates.tzd.plist
com.apple.touchbarserver.plist
com.apple.trustd.plist
com.apple.tzlinkd.plist
com.apple.ucupdate.plist
com.apple.uninstalld.plist
com.apple.unmountassistant.sysagent.plist
com.apple.usbd.plist
com.apple.uucp.plist
com.apple.var-db-dslocal-backup.plist
com.apple.vsdbutil.plist
com.apple.warmd.plist
com.apple.watchdogd.plist
com.apple.wifiFirmwareLoader.plist
com.apple.wifip2pd.plist
com.apple.wifivelocityd.plist
com.apple.wwand.plist
com.apple.xartstorageremoted.plist
com.apple.xpc.roleaccountd.plist
com.apple.xpc.smd.plist
com.apple.xpc.uscwoap.plist
com.apple.xsan.plist
com.apple.xsandaily.plist
com.apple.xscertadmin.plist
com.apple.xscertd-helper.plist
com.apple.xscertd.plist
com.vix.cron.plist
ntalk.plist
org.apache.httpd.plist
org.cups.cups-lpd.plist
org.cups.cupsd.plist
org.net-snmp.snmpd.plist
org.openldap.slapd.plist
ssh.plist
t
foxconndeMac-mini:LaunchDaemons foxconn$ vi com.apple.smbd.plist
/dict
/plist
Curator简介一、Curator是NetFlix公司开源的一套Zookeeper客户端框架,其特点如下:
1、连接重连
2、反复注册Watcher
3、NodeExistsException处理
二、常用API
API文档对应地址:
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);//Zookeeper每1秒检查一次session状态,如果断掉的话,重新连接,如果连续3次均没连上,则session失效
CuratorFramework client = CuratorFrameworkFactory.newClient(connectingString,sessionTimeout,connectionTimeOut,retryPolicy);
//创建节点
client.create().forPath(path);
//创建节点并设置值
client.create().forPath(path,"init".getBytes());
//创建临时节点
client.create().withMode(CreateMode.EPHEMERAL).forPath(path);
//创建临时节点并递归创建父节点
client.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path);
//删除节点
client.delete().forPath(path);
//删除节点并递归删除其所有子节点
client.delete().deletingChildrenIfNeeded().forPath(path);
//删除节点,强制指定版本进行删除
client.delete().withVersion(version).forPath(path);
//强制保证删除
client.delete().guaranteed().forPath(path);
//获取某个节点数据
client.getData().forPath(path);
//读取一个节点的数据内容,同时按获取到该节点的stat
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath(path);
//更新一个节点的数据内容
client.setData().withVersion(version).forPath(path);
//异步
client.create().creatingParentsIfNeeded().inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
}
}).forPath(path,"init".getBytes());
三、监听器
1、NodeCache:监听节点变化
NodeCache cache = new NodeCache(client,path,false);
cache.start(true); //第一次启动的时候就会从Zookeeper上同步数据
cache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println(cache.getCurrentData().getData());
}
});
2、PathChildrenCache:监控某个节点子节点的变化【无法对父节点以及二级节点数据进行监控】
PathChildrenCache childrenCache = new PathChildrenCache(client,path,true);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("节点变更类型:"+pathChildrenCacheEvent.getType()+"对应节点:"+pathChildrenCacheEvent.getData().getPath());
}
});
四、LeaderLatch
LeaderLatch⽤于实现Leader的选举
– 触发方法isLeader(),表示成为leader
– 触发notLeader(),表示失去leader权限
– 场景:参考xxx-callback
public void afterPropertiesSet() throws Exception {
if (leaderLatch != null) {
return;
}
leaderLatch = new LeaderLatch(curatorClient,//zk客户端实例
PATH,//选举根节点路径
OSUtils.getServerIp() + "_" + UUID.randomUUID().toString()); //客户端ID,用来标记客户端,即客户端编号,名称
leaderLatch.addListener(new LeaderLatchListener() {
@Override
public void isLeader() { //抢主成功时触发
ContextHolder.setLeader(true);
LOGGER.info("im leader");
}
@Override
public void notLeader() { //抢主失败时触发
ContextHolder.setLeader(false);
LOGGER.info("im not leader");
}
});
leaderLatch.start();
}
五、分布式锁
1、模拟并发
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i=0;i10;i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await();
}catch (Exception e){
}
System.out.println(System.currentTimeMillis());
}
}).start();
}
countDownLatch.countDown();
2、分布式锁控制
client.start();
String lock_path = "/lock";
final InterProcessMutex lock = new InterProcessMutex(client,lock_path);
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i=0;i10;i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await();
lock.acquire();
}catch (Exception e){
}
System.out.println(System.currentTimeMillis());
try {
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
countDownLatch.countDown();
六、分布式计数器
DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger(client,path,new RetryNTimes(1000,3));
atomicInteger.add(8);
思考:
1、开关同步采用Zookeeper是否可以 ?
2、ABTest?
3、分批任务计算?
4、配置中心
curator里面的NodeCache监听节点,怎么获取触发事件判断节点是新增、修改还是删除应该通过PathChildrenCache来处理,PathChildrenCacheListener有event参数,
NodeCache和NodeCacheListener是监听节点内容变更的,直接通过cn.getCurrentData().data()可以获得节点最新内容
dubbo使用zookeeper连接,zookeeper宕机后怎么处理?1、配置文件同步
2、主从切换
3、分布式队列
4、分布式锁
Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储数据的,它的作用主要是用来维护和监控你存储的数据的状态变化。
通过监控这些数据状态的变化,从而可以达到基于数据的集群管理,后面将会详细介绍 Zookeeper 能够解决的一些典型问题,这里先介绍一下,Zookeeper 的操作接口和简单使用示例。
常用接口列表
客户端要连接 Zookeeper 服务器可以通过创建 org.apache.zookeeper. ZooKeeper 的一个实例对象,然后调用这个类提供的接口来和服务器交互。
前面说了 ZooKeeper 主要是用来维护和监控一个目录节点树中存储的数据的状态,所有我们能够操作 ZooKeeper 的也和操作目录节点树大体一样,如创建一个目录节点,给某个目录节点设置数据,获取某个目录节点的所有子目录节点,给某个目录节点设置权限和监控这个目录节点的状态变化。
1
目录:1、求一个简单又好记的域名!!!2、域名怎么起 才简单而好记????3、什么样的域名才是好域名,利于网站收录4、什么样的域名...