forked from docs/doc-exports
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com> Co-authored-by: Yang, Tong <yangtong2@huawei.com> Co-committed-by: Yang, Tong <yangtong2@huawei.com>
56 lines
8.3 KiB
HTML
56 lines
8.3 KiB
HTML
<a name="mrs_01_2102"></a><a name="mrs_01_2102"></a>
|
|
|
|
<h1 class="topictitle1">Performing Special Configuration on ZooKeeper Clients in the Same JVM</h1>
|
|
<div id="body8662426"><div class="section" id="mrs_01_2102__en-us_topic_0000001219029643_s44dfef643bd54329867a86059f61df38"><h4 class="sectiontitle">Scenarios</h4><p id="mrs_01_2102__en-us_topic_0000001219029643_ae3c3cca0bda94d2a9b7bd9baaf27f014">Currently, ZooKeeper client properties can be configured only through Java system properties. Therefore, all clients in the same JVM have the same configuration. In some cases, the ZooKeeper client needs to connect to different clusters and this requires different client configurations. For example, if users want to connect two different ZooKeeper clusters from the same JVM, one cluster has the security mode enabled and the other does not. The configuration of each client varies. Different configuration methods are required for different clients.</p>
|
|
<div class="p" id="mrs_01_2102__en-us_topic_0000001219029643_add15901323ac43bc89b0609bd2574607">This solution allows each ZooKeeper client to use different configurations. As shown in <a href="#mrs_01_2102__en-us_topic_0000001219029643_fa44c1a23b67d492da21c48348fcd9fc5">Figure 1</a>, each client uses different configurations, which are initialized in the property file.<div class="fignone" id="mrs_01_2102__en-us_topic_0000001219029643_fa44c1a23b67d492da21c48348fcd9fc5"><a name="mrs_01_2102__en-us_topic_0000001219029643_fa44c1a23b67d492da21c48348fcd9fc5"></a><a name="en-us_topic_0000001219029643_fa44c1a23b67d492da21c48348fcd9fc5"></a><span class="figcap"><b>Figure 1 </b>Performing special configuration on ZooKeeper clients in the same JVM</span><br><span><img id="mrs_01_2102__en-us_topic_0000001219029643_ie8b24cf3be854ac49acb7943c2345391" src="en-us_image_0000001349139437.png"></span></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mrs_01_2102__en-us_topic_0000001219029643_s1c550c122f1b467ca6314f773d803d5c"><h4 class="sectiontitle">Configuration Description</h4><ol id="mrs_01_2102__en-us_topic_0000001219029643_o33b48bd75b7041909806dece99902320"><li id="mrs_01_2102__en-us_topic_0000001219029643_lee22274798394c5687d4a94f6726fa59"><span>Create the ZKClientConfig instance and transfer the ZKClientConfig instance to ZooKeeper when creating the ZooKeeper instance.</span><p><p id="mrs_01_2102__en-us_topic_0000001219029643_a610e69ba866e4e2aaf2a19a6dfc4dbf1">The methods for creating the ZKClientConfig instance are as follows:</p>
|
|
<ul id="mrs_01_2102__en-us_topic_0000001219029643_u933661bab83844279d4ea8d18a55ae20"><li id="mrs_01_2102__en-us_topic_0000001219029643_l79da0f40e654425d87d71c59ae175031">Method 1:<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_s77780b49eaaf4412b8b64133a9d9b90e">ZKClientConfig clientConfig = new ZKClientConfig();</pre>
|
|
<p id="mrs_01_2102__en-us_topic_0000001219029643_a67ec2e7ff97b4f949b4e842729223995">ZKClientConfig will have all properties of the ZooKeeper client configured in Java.</p>
|
|
</li><li id="mrs_01_2102__en-us_topic_0000001219029643_ld0a40d56eaf548989a19321f4b14fdca">Method 2:<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_s8ec05ce172404d1db5332f891f887265">ZKClientConfig clientConfig = new ZKClientConfig();
|
|
clientConfig.addConfiguration("/somepath/zoo-client.cfg");</pre>
|
|
<p id="mrs_01_2102__en-us_topic_0000001219029643_abb92ed6764c94b2f9fabc3096462237b">ZKClientConfig is initialized in the <span class="filepath" id="mrs_01_2102__en-us_topic_0000001219029643_fedb0000305c74ef7b8ceb01d61ba2fc4"><b>zoo-client.cfg</b></span> file. The properties of the <span class="filepath" id="mrs_01_2102__en-us_topic_0000001219029643_f3b047b158fd14a00a9885b4a31fb6682"><b>zoo-client.cfg</b></span> file will overwrite the same Java system properties.</p>
|
|
</li><li id="mrs_01_2102__en-us_topic_0000001219029643_le9a06fea06d049e3b7744363fa0b39c0">Method 3:<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_sa3ef3ea47a0e4fee8d507030a98f9f38">ZKClientConfig clientConfig = new ZKClientConfig();
|
|
clientConfig.setProperty(ZKClientConfig.SECURE_CLIENT, "true");</pre>
|
|
<p id="mrs_01_2102__en-us_topic_0000001219029643_a4e79b2ce119c479188dad2991df425aa">After initialization, the clientConfig has the Java system properties. In addition, the additional property <strong id="mrs_01_2102__en-us_topic_0000001219029643_b100753854241924">ZKClientConfig.SECURE_CLIENT</strong> is added through the code <strong id="mrs_01_2102__en-us_topic_0000001219029643_b103194873641924">clientConfig.setProperty(ZKClientConfig.SECURE_CLIENT, "true</strong>").</p>
|
|
</li></ul>
|
|
</p></li><li id="mrs_01_2102__en-us_topic_0000001219029643_l81148716ba0244ee8f1c4289261ead0a"><span>After the configuration is complete, use any of the following ZooKeeper APIs to create a client.</span><p><pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_s1eb586a8a8a84e04991ce80d1cf19706">org.apache.zookeeper.ZooKeeper.ZooKeeper(String, int, Watcher, ZKClientConfig)
|
|
org.apache.zookeeper.ZooKeeper.ZooKeeper(String, int, Watcher, boolean, ZKClientConfig)</pre>
|
|
</p></li></ol>
|
|
</div>
|
|
<div class="section" id="mrs_01_2102__en-us_topic_0000001219029643_s155a1eb394da464cbfeb6c96eb5db30d"><h4 class="sectiontitle">Example Code</h4><p id="mrs_01_2102__en-us_topic_0000001219029643_a58364b6f8d634a399fd5a53a95144fd7">Use the following code to connect Client1 and Client2 from the same JVM to a cluster with security mode enabled and a cluster in general mode, respectively.</p>
|
|
<ul id="mrs_01_2102__en-us_topic_0000001219029643_u835cc4cbcc8b4720ba36cc7d32cc005e"><li id="mrs_01_2102__en-us_topic_0000001219029643_l73511995d21841de95069c97ef9ba566">Connect to a cluster with security mode enabled:<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_sf3402bf22e0045928809f5d37cb497cb">String secureClientConfigPath = "/somepath/zoo-secure-client.cfg";
|
|
ZKClientConfig secureClientConfig = new ZKClientConfig(secureClientConfigPath);
|
|
ZooKeeper secureZooKeeperClient = new ZooKeeper(connectString, sessionTimeout, watcher, secureClientConfig);</pre>
|
|
</li><li id="mrs_01_2102__en-us_topic_0000001219029643_l237b4062e3454cdc91011c38e91a9187">Connect to a cluster in common mode:<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_s06c13cc491084ac8853f3030215a86b7">String nonSecureClientConfigPath = "/somepath/zoo-non-secure-client.cfg";
|
|
|
|
File nonSecureConfigFile = new File(nonSecureClientConfigPath);
|
|
ZKClientConfig nonSecureClientConfig = new ZKClientConfig(nonSecureConfigFile);
|
|
ZooKeeper nonSecureZooKeeperClient = new ZooKeeper(connectString, sessionTimeout, watcher, nonSecureClientConfig);</pre>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="mrs_01_2102__en-us_topic_0000001219029643_s661bc3dac82745ddb2d475e734e0fe86"><h4 class="sectiontitle">Constraints</h4><p id="mrs_01_2102__en-us_topic_0000001219029643_a7becbb73ad5e4225b7d2ff4d13413577">When Kerberos realms are different, we can map to KDC by the realm. Therefore, authentication can be performed based on the KDC of the realm of each client.</p>
|
|
<p id="mrs_01_2102__en-us_topic_0000001219029643_aedd2d334e993406ea9ad4e1e4ceb758e">For example, two KDCs run on 192.168.1.2 and 192.168.1.3. The two KDCs correspond to the realms of HADOOP.COM and EXAMPLE.COM, respectively.</p>
|
|
<p id="mrs_01_2102__en-us_topic_0000001219029643_abdb4f9426c844d1dab01ff67d362e27a">You can map KDC in the <strong id="mrs_01_2102__en-us_topic_0000001219029643_b106304249741924">krb5.conf</strong> file based on the realm as follows:</p>
|
|
<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_s1c78405f68514ec49a172ded814293ff">[realms]
|
|
HADOOP.COM = {
|
|
kdc=192.168.1.2
|
|
admin_server=192.168.1.2
|
|
other attributes ...
|
|
}</pre>
|
|
<pre class="screen" id="mrs_01_2102__en-us_topic_0000001219029643_sd29e88232c6e4c30b8afe9faf999db63">EXAMPLE.COM = {
|
|
kdc=192.168.1.3
|
|
admin_server=192.168.1.3
|
|
other attributes ...
|
|
} </pre>
|
|
<p id="mrs_01_2102__en-us_topic_0000001219029643_af330f12d088f48f79b89ffa56586803f">However, when different KDCs have the same realm, the realm cannot map to the corresponding KDC. This is the configuration constraint of multiple ZooKeeper clients in the same JVM. Therefore, multiple ZooKeeper clients in the same JVM, cannot use multiple KDCs with the same realm.</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_2092.html">Using ZooKeeper</a></div>
|
|
</div>
|
|
</div>
|
|
|