ZooKeeper for Java/Spring Config?

前端 未结 6 844
梦毁少年i
梦毁少年i 2021-01-30 23:36

Are there any well documented use cases of Apache ZooKeeper being used to distribute configuration of Java applications, and in particular Spring services?

Like many use

6条回答
  •  野的像风
    2021-01-31 00:08

    Zookeeper can be very nicely leveraged with higher abstraction using Curator APIs for configuration management in distributed applications. To get started just follow these two steps.

    STEP 1 : Start zookeper server and then start zookeeper cli and create some znodes. Znodes are nothing but UNIX like files which contain values, and name of files depict property name.
    To create/fetch/update properties use these commands on zookeeper cli.
    
    create /system/dev/example/port 9091
    get /system/dev/example/port
    set /system/dev/example/port 9092
    

    To fetch these properties in java program refer this code snippet.

    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.curator.framework.CuratorFramework; 
    import org.apache.curator.framework.CuratorFrameworkFactory; 
    import org.apache.curator.retry.ExponentialBackoffRetry;
    public class App 
    {
        public static void main( String[] args ) throws Exception
        {
             final String ZK = "localhost:2181"; 
    
             final Map data = new HashMap();         
             CuratorFramework client = CuratorFrameworkFactory.newClient(ZK, new ExponentialBackoffRetry(100, 3)); 
             client.start();
             System.out.println(new String(client.getData().forPath("/system/dev/example/port")));
           }  
    }
    

提交回复
热议问题