मैं जुकीपर पर नेतृत्व चुनाव करने के लिए अपाचे क्यूरेटर लाइब्रेरी का उपयोग कर रहा हूं। मेरे पास विभिन्न मशीनों में अपना आवेदन कोड तैनात किया गया है और मुझे केवल एक मशीन से अपना कोड निष्पादित करने की आवश्यकता है, इसलिए मैं ज़ूकीपर पर नेतृत्व चुनाव कर रहा हूं ताकि मैं जांच सकूं कि मैं नेता हूं या नहीं, फिर इस कोड को निष्पादित करें। , मैं उपयोग कर रहा हूँज़ूकीपर के लिए क्यूरेटर का उपयोग करके लीडर चयन नुस्खा का कुशलतापूर्वक उपयोग कैसे करें?
// can this class be improved in any ways?
public class ZookeeperClient {
private CuratorFramework client;
private String latchPath;
private String id;
private LeaderLatch leaderLatch;
public ZookeeperClient(String connString, String latchPath, String id) {
client = CuratorFrameworkFactory.newClient(connString, new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
this.id = id;
this.latchPath = latchPath;
}
public void start() throws Exception {
client.start();
leaderLatch = new LeaderLatch(client, latchPath, id);
leaderLatch.start();
}
public boolean isLeader() {
return leaderLatch.hasLeadership();
}
public Participant currentLeader() throws Exception {
return leaderLatch.getLeader();
}
public void close() throws IOException {
leaderLatch.close();
client.close();
}
public CuratorFramework getClient() {
return client;
}
public String getLatchPath() {
return latchPath;
}
public String getId() {
return id;
}
public LeaderLatch getLeaderLatch() {
return leaderLatch;
}
}
अब अपने आवेदन में
-
नीचे मेरी LeaderElectionExecutor
वर्ग जो यकीन है कि मैं आवेदन प्रति एक क्यूरेटर उदाहरण हो रहा है
public class LeaderElectionExecutor {
private ZookeeperClient zookClient;
private static final String LEADER_NODE = "/testleader";
private static class Holder {
static final LeaderElectionExecutor INSTANCE = new LeaderElectionExecutor();
}
public static LeaderElectionExecutor getInstance() {
return Holder.INSTANCE;
}
private LeaderElectionExecutor() {
try {
String hostname = Utils.getHostName();
String nodes = "host1:2181,host2:2181;
zookClient = new ZookeeperClient(nodes, LEADER_NODE, hostname);
zookClient.start();
// added sleep specifically for the leader to get selected
// since I cannot call isLeader method immediately after starting the latch
TimeUnit.MINUTES.sleep(1);
} catch (Exception ex) {
// logging error
System.exit(1);
}
}
public ZookeeperClient getZookClient() {
return zookClient;
}
}
और नीचे मेरी ZookeeperClient
कोड है बनाता है इस तरह कोड -
public void method01() {
ZookeeperClient zookClient = LeaderElectionExecutor.getInstance().getZookClient();
if (zookClient.isLeader()) {
// do something
}
}
public void method02() {
ZookeeperClient zookClient = LeaderElectionExecutor.getInstance().getZookClient();
if (zookClient.isLeader()) {
// do something
}
}
समस्या वक्तव्य: -
क्यूरेटर पुस्तकालय में - कुंडी काम नहीं करेगा शुरू करने के बाद तुरंत isLeader()
कॉलिंग। नेता को चुनने में समय लगता है। और इस कारण से, मैंने अपने LeaderElectionExecutor
कोड में 1 मिनट की नींद जोड़ दी है जो ठीक काम करता है लेकिन मुझे लगता है कि ऐसा करने का सही तरीका नहीं है।
क्या ऐसा करने का कोई बेहतर तरीका है? इसे ध्यान में रखते हुए, मुझे यह जांचने का एक तरीका चाहिए कि मैं नेता हूं या नहीं, फिर कोड के इस टुकड़े को निष्पादित करें। मैं एक ही विधि में सब कुछ नहीं कर सकता, इसलिए मुझे अलग-अलग वर्गों और विधियों से isLeader
विधि को कॉल करने की आवश्यकता है ताकि यह जांच सके कि मैं नेता हूं या नहीं, फिर केवल कोड के इस टुकड़े को निष्पादित करें।
मैं जुकीपर 3.4.5 और क्यूरेटर 1.7.1 संस्करण का उपयोग कर रहा हूं।