2012-02-14 8 views
38

कॉलिंग org.apache.zookeeper.server.quorum.QuorumPeerMain.main() काम नहीं कर रहा है।क्या यूनिट परीक्षणों के लिए कहें, प्रक्रिया में एक ज़ूकीपर सर्वर उदाहरण शुरू करना संभव है?

+4

इसके अलावा, इकाई में इस्तेमाल के लिए एक स्मृति में चिड़ियाघर संचालक कार्यान्वयन के साथ क्यूरेटर ढांचे जहाजों – manku

+0

क्यूरेटर एक परीक्षण चिड़ियाघर संचालक के साथ जहाज है परीक्षण, लेकिन यह स्मृति में _not_ है। यह एक temp निर्देशिका में डिस्क पर डेटा स्टोर करता है। –

उत्तर

39

फोन ZooKeeper आप ZooKeeperServerMain वर्ग पर अमल करने के लिए है शुरू करने के लिए।

आप एम्बेडेड मोड में ZooKeeper शुरू करने के लिए निम्न कोड का उपयोग कर सकते हैं।

Properties startupProperties = ... 

QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig(); 
try { 
    quorumConfiguration.parseProperties(startupProperties); 
} catch(Exception e) { 
    throw new RuntimeException(e); 
} 

zooKeeperServer = new ZooKeeperServerMain(); 
final ServerConfig configuration = new ServerConfig(); 
configuration.readFrom(quorumConfiguration); 

new Thread() { 
    public void run() { 
     try { 
      zooKeeperServer.runFromConfig(configuration); 
     } catch (IOException e) { 
      log.error("ZooKeeper Failed", e); 
     } 
    } 
}.start(); 
+2

यूनिट परीक्षणों के लिए जाने का यह तरीका है। QuorumPeerMain का उपयोग केवल मल्टी सर्वर (क्लस्टर) जुकीपर के लिए है। – sourcedelica

+1

स्वीकार्य उत्तर से यह एक बहुत साफ समाधान है। –

+0

यह पहले क्लास के संदर्भ में उपयोगी है, लेकिन परीक्षणों के बीच इसे बंद करना (नोड्स को साफ करने के लिए, उदाहरण के लिए) गैर-तुच्छ है - मैंने क्यूरेटर-आधारित समाधान को छोड़ दिया और स्विच किया। (thread.stop/interrupt में JUnitRunner को रोकने के बाद) –

11

आप इस तरह कुछ उपयोग कर सकते हैं।

int clientPort = 21818; // none-standard 
int numConnections = 5000; 
int tickTime = 2000; 
String dataDirectory = System.getProperty("java.io.tmpdir"); 

File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile(); 

ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime); 
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections); 

standaloneServerFactory.startup(server); // start the server. 

और यह बंद करने के लिए सिर्फ standaloneServerFactory.shutdown()

2
ServerConfig config = new ServerConfig(); 
config.parse(new String[] {port, dir}); 
ZooKeeperServerMain zk = new ZooKeeperServerMain(); 
zk.runFromConfig(config); 
+1

आप इस तरह से ज़ूकीपर सर्वर नहीं चला सकते हैं क्योंकि यह zk.runFromConfig (config) के बाद निम्न कोड i.e. कोड को अवरुद्ध करेगा; निष्पादित होने से। –

3

एक अल्पकालिक बंदरगाह (zkPort द्वारा दिखाया गया है) के उपयोग को जोड़कर बिल्डिंग 1 के जवाब पर और नवीनतम ZK एपीआई के लिए अद्यतन:

int tickTime = 2000; 
int numConnections = 5000; 
String dataDirectory = System.getProperty("java.io.tmpdir"); 

File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile(); 

ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime); 
standaloneServerFactory = ServerCnxnFactory.createFactory(0, numConnections); 
int zkPort = standaloneServerFactory.getLocalPort(); 

standaloneServerFactory.startup(server); 
+0

आप शायद इस तरह चिड़ियाघर रखरखाव सर्वर शुरू नहीं कर सकते क्योंकि यह सभी बाद के कोड को स्टैंडअलोन ServerFactory.startup (सर्वर) के बाद निष्पादित करने से रोक देगा; –

50

Netfix Curator एक रूपरेखा opensourced बनाने के लिए जुकीपर का उपयोग और भी सुविधाजनक है। यह परीक्षण सर्वर वर्ग में बनाया गया है। आप Maven का उपयोग करते हैं तो बस अपने प्रोजेक्ट की pom.xml से जोड़ें:

<dependency> 
    <groupId>org.apache.curator</groupId> 
    <artifactId>curator-test</artifactId> 
    <version>2.2.0-incubating</version> 
    <scope>test</scope> 
</dependency> 

और यहाँ परीक्षण अनिवार्य कर रहे हैं।

TestingServer zkTestServer; 

@Before 
public void startZookeeper() throws Exception { 
    zkTestServer = new TestingServer(2181); 
    cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000)); 
} 

@After 
public void stopZookeeper() throws IOException { 
    cli.close(); 
    zkTestServer.stop(); 
} 

cli किसी भी परीक्षण डेटा को बनाना बहुत आसान है।

cli.create().forPath("/a1", "testvalue".getBytes()); 
+2

मेरे लिए यह curi.start() क्यूरेटर संस्करण 2.10.0 –

+0

@ माइकलबॉकिंग धन्यवाद का उपयोग कर काम नहीं किया! मुझे पहले 'cli.start()' को कॉल करने की भी आवश्यकता थी, फिर उसने काम करना शुरू कर दिया –

0

जेफबॉर्न के उत्तर का एक अद्यतन संस्करण।

int clientPort = 2199; // not standard 
    int numConnections = 5000; 
    int tickTime = 2000; 
    String dataDirectory = System.getProperty("java.io.tmpdir"); 

    File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile(); 

    ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime); 
    ServerCnxnFactory factory = new NIOServerCnxnFactory(); 
    factory.configure(new InetSocketAddress(clientPort), numConnections); 

    factory.startup(server); // start the server. 

    // ...shutdown some time later 
    factory.shutdown(); 
संबंधित मुद्दे