Difference between revisions of "Linux zookeeper"
(→Spring Cloud ZooKeeper) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | ZooKeeper is a cluster operator. | + | ZooKeeper is a cluster operator that stores values to be shared across multiply clients. |
== Shell Commands == | == Shell Commands == | ||
Line 15: | Line 15: | ||
== Spring Cloud ZooKeeper == | == Spring Cloud ZooKeeper == | ||
+ | This framework helps manage the clients that connect to zookeeper. You can also use this for sharing values amongst a cluster. | ||
+ | |||
+ | To start, I followed the spring guide and created an empty application with @EnableDiscoveryClient | ||
+ | |||
+ | package com.example.zookeeper.zkexample; | ||
+ | |||
+ | import org.springframework.boot.SpringApplication; | ||
+ | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
+ | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | @SpringBootApplication | ||
+ | @EnableDiscoveryClient | ||
+ | @RestController | ||
+ | public class ZkExampleApplication { | ||
+ | |||
+ | @RequestMapping("/") | ||
+ | public String hello() { | ||
+ | return "hello"; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | SpringApplication.run(ZkExampleApplication.class, args); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | After I create this, I ran it with the following command | ||
+ | mvn spring-boot:run -Dserver.port=8081 | ||
+ | |||
+ | Then I check zookeeper cli and looked for the znode | ||
+ | ls /services/application | ||
+ | |||
+ | and what was returned was | ||
+ | [959427f8-5629-4dcb-a606-2ed310134f8f] | ||
+ | |||
+ | When I started up another server | ||
+ | mvn spring-boot:run -Dserver.port=8082 | ||
+ | |||
+ | Then I ls the same location I found another znode. | ||
+ | [959427f8-5629-4dcb-a606-2ed310134f8f, 106e739e-7422-414d-8cca-8d57fb443290] | ||
+ | |||
+ | |||
+ | I then created a method in class that returned all the URI of all the clients connected. | ||
+ | |||
+ | @Autowired | ||
+ | private DiscoveryClient discoveryClient; | ||
+ | |||
+ | public String serviceUrl() { | ||
+ | |||
+ | List<ServiceInstance> list = discoveryClient.getInstances("application"); | ||
+ | |||
+ | String result = list.stream().map(instance -> { return instance.getUri().toString(); }).collect(Collectors.joining(",")); | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | When I hooked this up to a rest end point I was able to display all connected clients to zookeeper. | ||
+ | |||
+ | @RequestMapping("/") | ||
+ | public String hello() { | ||
+ | |||
+ | System.out.println("appName:" + appName); | ||
+ | System.out.println("services:" + discoveryClient.getServices()); | ||
+ | |||
+ | return serviceUrl(); | ||
+ | } | ||
+ | |||
+ | What I found out is that when spring-boot starts up it creates an instance of ServerInstance in ZooKeeper and this is retrievable. When I shutdown a client the znode was removed. | ||
+ | |||
+ | This is an easy but limited frame work for quick clustering of nodes. For any more in detailed work or customizing I would go with just implementing ZooKeeper client directly in java. | ||
+ | |||
+ | == Resources == | ||
+ | https://zookeeper.apache.org/doc/r3.3.3/zookeeperStarted.html | ||
+ | https://cloud.spring.io/spring-cloud-zookeeper/ |
Latest revision as of 11:33, 18 July 2017
ZooKeeper is a cluster operator that stores values to be shared across multiply clients.
Shell Commands
The server works by using things called znodes, kind of like a file system /
This shell is very helpful for debugging issue and understand what Zoo Keeper does under the hood.
To Start the shell
./zkCli.sh -server localhost:2181
ls - list out the directory create /test - creates the znode test get /test - gets the data for the znode
Spring Cloud ZooKeeper
This framework helps manage the clients that connect to zookeeper. You can also use this for sharing values amongst a cluster.
To start, I followed the spring guide and created an empty application with @EnableDiscoveryClient
package com.example.zookeeper.zkexample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ZkExampleApplication { @RequestMapping("/") public String hello() { return "hello"; } public static void main(String[] args) { SpringApplication.run(ZkExampleApplication.class, args); } }
After I create this, I ran it with the following command
mvn spring-boot:run -Dserver.port=8081
Then I check zookeeper cli and looked for the znode
ls /services/application
and what was returned was
[959427f8-5629-4dcb-a606-2ed310134f8f]
When I started up another server
mvn spring-boot:run -Dserver.port=8082
Then I ls the same location I found another znode.
[959427f8-5629-4dcb-a606-2ed310134f8f, 106e739e-7422-414d-8cca-8d57fb443290]
I then created a method in class that returned all the URI of all the clients connected.
@Autowired private DiscoveryClient discoveryClient; public String serviceUrl() { List<ServiceInstance> list = discoveryClient.getInstances("application"); String result = list.stream().map(instance -> { return instance.getUri().toString(); }).collect(Collectors.joining(",")); return result; }
When I hooked this up to a rest end point I was able to display all connected clients to zookeeper.
@RequestMapping("/") public String hello() { System.out.println("appName:" + appName); System.out.println("services:" + discoveryClient.getServices()); return serviceUrl(); }
What I found out is that when spring-boot starts up it creates an instance of ServerInstance in ZooKeeper and this is retrievable. When I shutdown a client the znode was removed.
This is an easy but limited frame work for quick clustering of nodes. For any more in detailed work or customizing I would go with just implementing ZooKeeper client directly in java.
Resources
https://zookeeper.apache.org/doc/r3.3.3/zookeeperStarted.html https://cloud.spring.io/spring-cloud-zookeeper/