1. 程式人生 > >使用Spring Cloud Zookeeper實現服務的註冊和發現

使用Spring Cloud Zookeeper實現服務的註冊和發現

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.properties
server.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 系統會返回一個可用的服務,預設使用輪詢的方法返回一個服務