> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/spring-cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/spring-cloud/fu-wu-de-zhu-ce-yu-fa-xian/zookeeper.md).

# Zookeeper

## Zookeeper简介&#x20;

Zookeeper是一个分布式协调工具，可以实现服务注册与发现、注册中心、消息中间件、分布式配置中心等。&#x20;

> Zookeeper 分布式协调工具，可以实现注册中心，采用Zookeeper节点类型？ 用的临时节点
>
> 临时节点和生命周期进行关联的。
>
> Zookeeper 没有像Ereka那样有自动保护机制，
>
> 如果服务断开连接之后，该节点会自动的被删除掉。

## 环境搭建&#x20;

[Docker下安装zookeeper（单机 & 集群）](https://www.cnblogs.com/LUA123/p/11428113.html)

```
docker run -d -p 2181:2181 --name some-zookeeper --restart always zookeeper
```

## 启动zk服务器端&#x20;

## Maven依赖信息

```markup

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
		</dependency>

	</dependencies>
	<!-- 注意： 这里必须要添加， 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
```

## application.yml

### 会员配置文件

```yaml
###订单服务的端口号
server:
  port: 8002
###服务别名----服务注册到注册中心名称 
spring:
  application:
    name: zk-member
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181
```

### 订单配置文件

```yaml
###订单服务的端口号
server:
  port: 8003
###服务别名----服务注册到注册中心名称 
spring:
  application:
    name: zk-order
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181
```

启动zk-member服务和zk-order服务，可以发现在Zk服务器端上有对应的节点信息

![](https://1025660399-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfnT317zNvbaZczKOwj%2F-MEHzWkqkBkigiFNC_tb%2F-MEI1kFra67nxq3ByvGD%2Fimage.png?alt=media\&token=e3bbbbf2-7f7a-4a10-ba48-d47a44004033)

别忘记加入注释

```
@EnableDiscoveryClient
```

## 处理错误

[ZookeeperProviderApplicationTests.contextLoads » IllegalState Failed to load A...](https://www.cnblogs.com/heian99/p/11972232.html)
