服务注册进ZooKeeper
环境
先在Linux服务器上搭建好一zookeeper环境,关闭Linux服务器防火墙
[root@VM_0_16_centos ~]# systemctl stop firewalld
查看防火墙状态:
[root@VM_0_16_centos ~]# systemctl status firewalld
连通zookeeper服务器
查看zookeeper服务器ip:
[root@VM_0_16_centos ~]# ifconfig
互相连通:(windows和服务器)
[root@VM_0_16_centos ~]# ping xxx.xxx.xxx.xxx
新建微服务(省略部分xxx)
新建maven工程,导入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--先排除自带的zookeeper3.5.3-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.5.7版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
配置好yml文件:
server:
port: 8001
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 服务器IP:2181
再次确认连通
在windows命令窗口中输入
C:\Users\MACHENIKE>ping xxx.xxx.xxx.xxx
启动服务
启动微服务后,查看服务器上
[zk: localhost:2181(CONNECTED) 4] ls /
[zk: localhost:2181(CONNECTED) 5] ls /services