2012-02-14 20 views

cevap

39

çağrı kapatmaya.

Katıştırılmış modda ZooKeeper'u başlatmak için aşağıdaki kodu kullanabilirsiniz.

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

Birim testleri için bu yoldur. QuorumPeerMain kullanımı sadece Çoklu Sunucu (Kümelenmiş) Zookeeper içindir. – sourcedelica

+1

Bu, kabul edilen yanıttan çok daha temiz bir çözümdür. –

+0

Bu, BeforeClass bağlamında kullanışlıdır, ancak bunları (örneğin, düğümleri temizlemek için) testler arasında kapatılması önemsizdir - Ben vazgeçtim ve bir Küratör tabanlı çözüme geçtim. (thread.stop/Interrupt sonra JUnitRunner'ı durduracak) –

11

Böyle bir şeyi kullanabilirsiniz. Ve

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. 

sadece ZooKeeperServerMain sınıf yürütmek zorunda ZooKeeper başlatmak için standaloneServerFactory.shutdown()

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

Aşağıdaki kodu engelleyeceği şekilde zookeeper sunucusunu çalıştıramazsınız, zk.runFromConfig (config); idam edilmek üzere. –

3

(zkPort tarafından gösterilir) geçici bir port kullanımını ekleyerek 1 'ın cevabı Bina ve en son ZK API için güncelleme:

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

Muhtemelen hayvan koruyucusu sunucusunu, standaloneServerFactory.startup (server) 'dan sonra çalıştırılacak tüm sonraki kodları engelleyecek şekilde başlatamazsınız; –

50

Netfix yapmak Curator bir çerçeve opensourced Zookeeper kullanımı daha da kullanışlı. Test sunucusu sınıfında inşa etti.

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

Ve burada Test şartları şunlardır: Maven kullanıyorsanız sadece proje en pom.xml için bunu ekleyin. Herhangi bir test veri oluşturma cli ile

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(); 
} 

çok kolaydır.

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

Benim için bu Curator sürümü 2.10.0 –

+0

@ MichaelBöckling kullanarak cli.start() olmadan işe yaramadı! İlk önce 'cli.start()' i çağırmam gerekiyordu. –

0

GeoffBourne'in yanıtının güncelleştirilmiş bir sürümü.

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(); 
İlgili konular