@ -50,11 +50,15 @@ public class ZookeeperOperator implements InitializingBean {
private final Logger logger = LoggerFactory . getLogger ( ZookeeperOperator . class ) ;
private final Logger logger = LoggerFactory . getLogger ( ZookeeperOperator . class ) ;
@Autowired
@Autowired
private CuratorZookeeperClient zookeeperClient ;
private ZookeeperConfig zookeeperConfig ;
protected CuratorFramework zkClient ;
@Override
@Override
public void afterPropertiesSet ( ) throws Exception {
public void afterPropertiesSet ( ) throws Exception {
registerListener ( ) ;
this . zkClient = buildClient ( ) ;
initStateLister ( ) ;
treeCacheStart ( ) ;
}
}
/ * *
/ * *
@ -62,9 +66,62 @@ public class ZookeeperOperator implements InitializingBean {
* /
* /
protected void registerListener ( ) { }
protected void registerListener ( ) { }
protected void treeCacheStart ( ) { }
public void initStateLister ( ) {
checkNotNull ( zkClient ) ;
zkClient . getConnectionStateListenable ( ) . addListener ( ( client , newState ) - > {
if ( newState = = ConnectionState . LOST ) {
logger . error ( "connection lost from zookeeper" ) ;
} else if ( newState = = ConnectionState . RECONNECTED ) {
logger . info ( "reconnected to zookeeper" ) ;
} else if ( newState = = ConnectionState . SUSPENDED ) {
logger . warn ( "connection SUSPENDED to zookeeper" ) ;
}
} ) ;
}
private CuratorFramework buildClient ( ) {
logger . info ( "zookeeper registry center init, server lists is: {}." , zookeeperConfig . getServerList ( ) ) ;
CuratorFrameworkFactory . Builder builder = CuratorFrameworkFactory . builder ( ) . ensembleProvider ( new DefaultEnsembleProvider ( checkNotNull ( zookeeperConfig . getServerList ( ) , "zookeeper quorum can't be null" ) ) )
. retryPolicy ( new ExponentialBackoffRetry ( zookeeperConfig . getBaseSleepTimeMs ( ) , zookeeperConfig . getMaxRetries ( ) , zookeeperConfig . getMaxSleepMs ( ) ) ) ;
//these has default value
if ( 0 ! = zookeeperConfig . getSessionTimeoutMs ( ) ) {
builder . sessionTimeoutMs ( zookeeperConfig . getSessionTimeoutMs ( ) ) ;
}
if ( 0 ! = zookeeperConfig . getConnectionTimeoutMs ( ) ) {
builder . connectionTimeoutMs ( zookeeperConfig . getConnectionTimeoutMs ( ) ) ;
}
if ( StringUtils . isNotBlank ( zookeeperConfig . getDigest ( ) ) ) {
builder . authorization ( "digest" , zookeeperConfig . getDigest ( ) . getBytes ( StandardCharsets . UTF_8 ) ) . aclProvider ( new ACLProvider ( ) {
@Override
public List < ACL > getDefaultAcl ( ) {
return ZooDefs . Ids . CREATOR_ALL_ACL ;
}
@Override
public List < ACL > getAclForPath ( final String path ) {
return ZooDefs . Ids . CREATOR_ALL_ACL ;
}
} ) ;
}
zkClient = builder . build ( ) ;
zkClient . start ( ) ;
try {
zkClient . blockUntilConnected ( ) ;
} catch ( final Exception ex ) {
throw new RuntimeException ( ex ) ;
}
return zkClient ;
}
public String get ( final String key ) {
public String get ( final String key ) {
try {
try {
return new String ( zookeeperClient . getZkClient ( ) . getData ( ) . forPath ( key ) , StandardCharsets . UTF_8 ) ;
return new String ( zkClient . getData ( ) . forPath ( key ) , StandardCharsets . UTF_8 ) ;
} catch ( Exception ex ) {
} catch ( Exception ex ) {
logger . error ( "get key : {}" , key , ex ) ;
logger . error ( "get key : {}" , key , ex ) ;
}
}
@ -74,7 +131,7 @@ public class ZookeeperOperator implements InitializingBean {
public List < String > getChildrenKeys ( final String key ) {
public List < String > getChildrenKeys ( final String key ) {
List < String > values ;
List < String > values ;
try {
try {
values = zookeeperClient . getZ kClient ( ) . getChildren ( ) . forPath ( key ) ;
values = zkClient . getChildren ( ) . forPath ( key ) ;
return values ;
return values ;
} catch ( InterruptedException ex ) {
} catch ( InterruptedException ex ) {
logger . error ( "getChildrenKeys key : {} InterruptedException" , key ) ;
logger . error ( "getChildrenKeys key : {} InterruptedException" , key ) ;
@ -88,7 +145,7 @@ public class ZookeeperOperator implements InitializingBean {
public boolean hasChildren ( final String key ) {
public boolean hasChildren ( final String key ) {
Stat stat ;
Stat stat ;
try {
try {
stat = zookeeperClient . getZ kClient ( ) . checkExists ( ) . forPath ( key ) ;
stat = zkClient . checkExists ( ) . forPath ( key ) ;
return stat . getNumChildren ( ) > = 1 ;
return stat . getNumChildren ( ) > = 1 ;
} catch ( Exception ex ) {
} catch ( Exception ex ) {
throw new IllegalStateException ( ex ) ;
throw new IllegalStateException ( ex ) ;
@ -97,7 +154,7 @@ public class ZookeeperOperator implements InitializingBean {
public boolean isExisted ( final String key ) {
public boolean isExisted ( final String key ) {
try {
try {
return zookeeperClient . getZ kClient ( ) . checkExists ( ) . forPath ( key ) ! = null ;
return zkClient . checkExists ( ) . forPath ( key ) ! = null ;
} catch ( Exception ex ) {
} catch ( Exception ex ) {
logger . error ( "isExisted key : {}" , key , ex ) ;
logger . error ( "isExisted key : {}" , key , ex ) ;
}
}
@ -107,7 +164,7 @@ public class ZookeeperOperator implements InitializingBean {
public void persist ( final String key , final String value ) {
public void persist ( final String key , final String value ) {
try {
try {
if ( ! isExisted ( key ) ) {
if ( ! isExisted ( key ) ) {
zookeeperClient . getZ kClient ( ) . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . PERSISTENT ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
zkClient . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . PERSISTENT ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
} else {
} else {
update ( key , value ) ;
update ( key , value ) ;
}
}
@ -118,11 +175,7 @@ public class ZookeeperOperator implements InitializingBean {
public void update ( final String key , final String value ) {
public void update ( final String key , final String value ) {
try {
try {
zkClient . inTransaction ( ) . check ( ) . forPath ( key ) . and ( ) . setData ( ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) . and ( ) . commit ( ) ;
CuratorOp check = zookeeperClient . getZkClient ( ) . transactionOp ( ) . check ( ) . forPath ( key ) ;
CuratorOp setData = zookeeperClient . getZkClient ( ) . transactionOp ( ) . setData ( ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
zookeeperClient . getZkClient ( ) . transaction ( ) . forOperations ( check , setData ) ;
} catch ( Exception ex ) {
} catch ( Exception ex ) {
logger . error ( "update key : {} , value : {}" , key , value , ex ) ;
logger . error ( "update key : {} , value : {}" , key , value , ex ) ;
}
}
@ -132,12 +185,12 @@ public class ZookeeperOperator implements InitializingBean {
try {
try {
if ( isExisted ( key ) ) {
if ( isExisted ( key ) ) {
try {
try {
zookeeperClient . getZ kClient ( ) . delete ( ) . deletingChildrenIfNeeded ( ) . forPath ( key ) ;
zkClient . delete ( ) . deletingChildrenIfNeeded ( ) . forPath ( key ) ;
} catch ( KeeperException . NoNodeException ignore ) {
} catch ( KeeperException . NoNodeException ignore ) {
//NOP
//NOP
}
}
}
}
zookeeperClient . getZ kClient ( ) . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
zkClient . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
} catch ( final Exception ex ) {
} catch ( final Exception ex ) {
logger . error ( "persistEphemeral key : {} , value : {}" , key , value , ex ) ;
logger . error ( "persistEphemeral key : {} , value : {}" , key , value , ex ) ;
}
}
@ -149,7 +202,7 @@ public class ZookeeperOperator implements InitializingBean {
persistEphemeral ( key , value ) ;
persistEphemeral ( key , value ) ;
} else {
} else {
if ( ! isExisted ( key ) ) {
if ( ! isExisted ( key ) ) {
zookeeperClient . getZ kClient ( ) . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
zkClient . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
}
}
}
}
} catch ( final Exception ex ) {
} catch ( final Exception ex ) {
@ -159,7 +212,7 @@ public class ZookeeperOperator implements InitializingBean {
public void persistEphemeralSequential ( final String key , String value ) {
public void persistEphemeralSequential ( final String key , String value ) {
try {
try {
zookeeperClient . getZ kClient ( ) . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL_SEQUENTIAL ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
zkClient . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL_SEQUENTIAL ) . forPath ( key , value . getBytes ( StandardCharsets . UTF_8 ) ) ;
} catch ( final Exception ex ) {
} catch ( final Exception ex ) {
logger . error ( "persistEphemeralSequential key : {}" , key , ex ) ;
logger . error ( "persistEphemeralSequential key : {}" , key , ex ) ;
}
}
@ -168,7 +221,7 @@ public class ZookeeperOperator implements InitializingBean {
public void remove ( final String key ) {
public void remove ( final String key ) {
try {
try {
if ( isExisted ( key ) ) {
if ( isExisted ( key ) ) {
zookeeperClient . getZ kClient ( ) . delete ( ) . deletingChildrenIfNeeded ( ) . forPath ( key ) ;
zkClient . delete ( ) . deletingChildrenIfNeeded ( ) . forPath ( key ) ;
}
}
} catch ( KeeperException . NoNodeException ignore ) {
} catch ( KeeperException . NoNodeException ignore ) {
//NOP
//NOP
@ -178,14 +231,14 @@ public class ZookeeperOperator implements InitializingBean {
}
}
public CuratorFramework getZkClient ( ) {
public CuratorFramework getZkClient ( ) {
return zookeeperClient . getZ kClient ( ) ;
return zkClient ;
}
}
public ZookeeperConfig getZookeeperConfig ( ) {
public ZookeeperConfig getZookeeperConfig ( ) {
return zookeeperClient . getZookeeperC onfig ( ) ;
return zookeeperConfig ;
}
}
public void close ( ) {
public void close ( ) {
CloseableUtils . closeQuietly ( zookeeperClient . getZ kClient ( ) ) ;
CloseableUtils . closeQuietly ( zkClient ) ;
}
}
}
}