call org.apache.zookeeper.server.quorum.QuorumPeerMain.main() çalışmıyor.Süreç testi için söz konusu süreçte bir zookeeper sunucu örneği başlatılabilir mi?
cevap
ç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();
Birim testleri için bu yoldur. QuorumPeerMain kullanımı sadece Çoklu Sunucu (Kümelenmiş) Zookeeper içindir. – sourcedelica
Bu, kabul edilen yanıttan çok daha temiz bir çözümdür. –
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) –
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()
ServerConfig config = new ServerConfig();
config.parse(new String[] {port, dir});
ZooKeeperServerMain zk = new ZooKeeperServerMain();
zk.runFromConfig(config);
Aşağıdaki kodu engelleyeceği şekilde zookeeper sunucusunu çalıştıramazsınız, zk.runFromConfig (config); idam edilmek üzere. –
(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);
Muhtemelen hayvan koruyucusu sunucusunu, standaloneServerFactory.startup (server) 'dan sonra çalıştırılacak tüm sonraki kodları engelleyecek şekilde başlatamazsınız; –
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());
Benim için bu Curator sürümü 2.10.0 –
@ MichaelBöckling kullanarak cli.start() olmadan işe yaramadı! İlk önce 'cli.start()' i çağırmam gerekiyordu. –
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();
Ayrıca ünitesinde kullanılmak üzere bir hafızada Yaupon uygulaması ile küratör çerçeve gemileri test zookeeper ile gemi yok – manku
Küratörü test eder, ancak bellekte _not_ olduğunu. verileri geçici bir dizinde diskte saklar. –