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);
}
}
application.properties
server.port=8822
spring.application.name=tomcat
bootstrap.properties
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.propertiesserver.port=8844
spring.application.name=tomcat-client
bootstrap.properties
spring.cloud.zookeeper.connectString=192.168.1.100:2181
spring.cloud.zookeeper.discovery.register=false
注意這裏的spring.cloud.zookeeper.discovery.register必須配置爲false,否則,應用啓動之後,也去zookeeper裏面註冊了服務
啓動main方法,
訪問http://127.0.0.1:8844/discovery 系統會返回一個可用的服務,默認使用輪詢的方法返回一個服務