CentOS7搭建Zookeeper
准备一台CentOS7服务器,并连接到服务器,能输入命令即可
安装JDK8
yum install java-1.8.0-openjdk* -y
java -version
安装Zookeeper
下载
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
解压
tar -zxf apache-zookeeper-3.5.9-bin.tar.gz
修改配置
[root@localhost zookeeper]# cd apache-zookeeper-3.5.9-bin
[root@localhost apache-zookeeper-3.5.9-bin]# cd conf/
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
[root@localhost conf]# vi zoo.cfg
zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/services/zookeeper/apache-zookeeper-3.5.9-bin/data
dataLogDir=/usr/local/services/zookeeper/apache-zookeeper-3.5.9-bin/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
esc
:wq
[root@localhost conf]# cd ../bin/
[root@localhost bin]# vi /etc/profile
profile
最后三行添加
export ZOOKEEPER_HOME=/usr/local/services/zookeeper/apache-zookeeper-3.5.9-bin
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
esc
:wq
[root@localhost bin]# source /etc/profile
启动
[root@localhost bin]# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost bin]#
查询状态
[root@localhost bin]# ./zkServer.sh state
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Usage: ./zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|restart|status|print-cmd}
[root@localhost bin]#
关闭防火墙
[root@localhost bin]# systemctl stop firewalld
[root@localhost bin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 四 2021-07-01 18:36:32 CST; 7s ago
Docs: man:firewalld(1)
Main PID: 686 (code=exited, status=0/SUCCESS)
7月 01 17:17:35 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
7月 01 17:17:35 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
7月 01 17:17:35 localhost.localdomain firewalld[686]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
7月 01 17:17:35 localhost.localdomain firewalld[686]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
7月 01 17:17:35 localhost.localdomain firewalld[686]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
7月 01 17:17:35 localhost.localdomain firewalld[686]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
7月 01 17:17:35 localhost.localdomain firewalld[686]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
7月 01 17:17:35 localhost.localdomain firewalld[686]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
7月 01 18:36:32 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
7月 01 18:36:32 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@localhost bin]#
测试连接
查看Centos IP
[root@localhost bin]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:97:84:38 brd ff:ff:ff:ff:ff:ff
inet 192.168.117.128/24 brd 192.168.117.255 scope global dynamic ens33
valid_lft 1427sec preferred_lft 1427sec
inet6 fe80::ac6b:cf04:3429:3996/64 scope link
valid_lft forever preferred_lft forever
[root@localhost bin]#
主机测试
C:\Users\ext.zhangyugen1>ping 192.168.117.128
正在 Ping 192.168.117.128 具有 32 字节的数据:
来自 192.168.117.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.117.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.117.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.117.128 的回复: 字节=32 时间<1ms TTL=64
192.168.117.128 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
C:\Users\ext.zhangyugen1>
提供者注册服务到Zookeeper
新建8004工程
新建名为cloud-provider-payment8004的Maven工程
修改POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3 防止与3.4.9起冲突-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.9</version>
</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>
</dependencies>
</project>
新建yml配置
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
# 采用自己搭建的Centos的地址 也就是上面搭建的
connect-string: 192.168.117.128:2181 # 192.168.111.144:2181 #
新建主启动类
package com.lun.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
新建Controller
package com.lun.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/zk")
public String paymentzk() {
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
测试
- 启动ZK-> ./zkServer.sh start
- 连接ZK -> ./zkCli.sh
- 启动项目
- 在ZK客户端查看 ```javascript [zk: localhost:2181(CONNECTED) 0] ls / [services, zookeeper] [zk: localhost:2181(CONNECTED) 1] get /zookeeper
[zk: localhost:2181(CONNECTED) 2] ls / [services, zookeeper] [zk: localhost:2181(CONNECTED) 3] get /services
[zk: localhost:2181(CONNECTED) 4] ls /services/cloud-provider-payment [574b2d9e-2b1d-4f7f-9d6d-46e54446e3c2] [zk: localhost:2181(CONNECTED) 5] get /services/cloud-provider-payment/574b2d9e-2b1d-4f7f-9d6d-46e54446e3c2 {“name”:”cloud-provider-payment”,”id”:”574b2d9e-2b1d-4f7f-9d6d-46e54446e3c2”,”address”:”ZB-PF2PD2LJ.360buyAD.local”,”port”:8004,”sslPort”:null,”payload”:{“@class”:”org.springframework.cloud.zookeeper.discovery.ZookeeperInstance”,”id”:”application-1”,”name”:”cloud-provider-payment”,”metadata”:{}},”registrationTimeUTC”:1625193474010,”serviceType”:”DYNAMIC”,”uriSpec”:{“parts”:[{“value”:”scheme”,”variable”:true},{“value”:”://“,”variable”:false},{“value”:”address”,”variable”:true},{“value”:”:”,”variable”:false},{“value”:”port”,”variable”:true}]}} [zk: localhost:2181(CONNECTED) 6]
5. 浏览器测试
1. [http://localhost:8004/payment/zk](http://localhost:8004/payment/zk)
![image.png](https://cdn.nlark.com/yuque/0/2021/png/1603133/1625193940624-ab1015db-f043-4286-86c3-4eb731c10cf9.png#clientId=u0c2b9cf4-6189-4&from=paste&height=94&id=u391b8d73&margin=%5Bobject%20Object%5D&name=image.png&originHeight=94&originWidth=633&originalType=binary&ratio=1&size=8245&status=done&style=none&taskId=ud01851b0-82a8-4e0b-8530-63bcba148d8&width=633)<br />测试成功!
<a name="XMed8"></a>
# 服务节点是临时节点还是持久节点
ZooKeeper的服务节点是**临时节点**,没有Eureka的自我保护机制,在服务停止后,只要超过心跳时间就会移除服务
<a name="SyLx9"></a>
## 测试
1. 停止服务
1. ZK客户端查看
```javascript
[zk: localhost:2181(CONNECTED) 30] ls /services
[cloud-provider-payment]
[zk: localhost:2181(CONNECTED) 31] ls /services
[cloud-provider-payment]
[zk: localhost:2181(CONNECTED) 32] ls /services
[]
[zk: localhost:2181(CONNECTED) 33]
消费者注册服务到Zookeeper
新建80工程
修改POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerzk-order80</artifactId>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3 防止与3.4.9起冲突-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.9</version>
</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>
</dependencies>
</project>
新建yml配置
server:
port: 80
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
# 自己搭建的CentOs的地址
connect-string: 192.168.117.128:2181 # 192.168.111.144:2181 #
新建主启动类
package com.lun.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
新建配置类
package com.lun.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
新建Controller
package com.lun.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk")
public String paymentInfo() {
String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
return result;
}
}
测试
- 启动ZK服务器 -> ./zkServer.sh start
- 连接ZK服务器 -> ./zkCli.sh
- 启动提供者和消费者服务
客户端测试
[zk: localhost:2181(CONNECTED) 33] ls /services [cloud-consumer-order, cloud-provider-payment] [zk: localhost:2181(CONNECTED) 34]
浏览器测试
测试成功!
zookeeper完成了,一般市面上用这个的并不多,一般银行用的还可以,至于集群配置和Eureka差不多,也是多个地址用逗号分割,至于Zookeeper的集群搭建,等到学Zookeeper的时候再写吧~完结撒花