Spring Cloud Zookeeper provides Apache Zookeeper integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations you can quickly enable and configure the common patterns inside your application and build large distributed systems with Zookeeper. The patterns provided include Service Discovery and Distributed Configuration.
首先要安装zookeeper,我这里安装的是:zookeeper-3.4.6
本实例使用2个服务端,1个客户端
项目依赖:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-zookeeper-dependencies</artifactId> <version>1.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-all</artifactId> </dependency> </dependencies>
package com.pp.zk.server1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class AppServer { public static void main(String[] args) { SpringApplication.run(AppServer.class, args); } }
server.port=8822 spring.application.name=tomcat
spring.cloud.zookeeper.connectString=192.168.1.100:2181 spring.cloud.zookeeper.discovery.instanceHost=192.168.2.10 spring.cloud.zookeeper.discovery.instancePort=${server.port}
application.properties
server.port=8833 spring.application.name=tomcat其余的代码、配置和上面的一样
分别启动这2个main方法
去zookeeper去查看节点信息
[zk: localhost:2181(CONNECTED) 70] ls /services/tomcat
[68e73968-9c1e-4362-a20c-ed505f772837, eeb02f9b-d115-4e18-ad31-cafc66093aa2]
这里可以看到,有2个临时节点,即有两个服务
客户端:
package com.pp.zk.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class AppClient { @Autowired private LoadBalancerClient loadBalancer; @Autowired private DiscoveryClient discovery; @RequestMapping("/discovery") public Object discovery() { System.out.println(loadBalancer.choose("tomcat")); return "discovery"; } @RequestMapping("/all") public Object all() { System.out.println(discovery.getServices()); return "all"; } public static void main(String[] args) { SpringApplication.run(AppClient.class, args); } }application.properties
server.port=8844 spring.application.name=tomcat-client
spring.cloud.zookeeper.connectString=192.168.1.100:2181 spring.cloud.zookeeper.discovery.register=false
启动main方法,
访问http://127.0.0.1:8844/discovery 系统会返回一个可用的服务,默认使用轮询的方法返回一个服务