链接:https://pan.baidu.com/s/1SIRI5IPEiRz0TVQJ0vzLLQ
提取码:r4xg
使用下面这个软件上传到linux
链接:https://pan.baidu.com/s/1u21hJwmzmvIDUdsvrMDhCw
提取码:ykqc
1.先解压
tar -zxvf zookeeper-3.4.9.tar.gf
2.改名字
mv zookeeper-3.4.9 zookeeper
3.进入zookeeper
cd zookeeper
4.切换到zookeeper目录下的conf目录下,重新复制一份zoo_sample.cfg文件并命名为zoo.cfg:
[root@localhost zookeeper]# cd conf //切换到目录下 [root@localhost conf]# ll //显示目录下的信息 总用量 12 -rw-r--r--. 1 root root 535 5月 8 18:17 configuration.xsl -rw-r--r--. 1 root root 2161 5月 8 18:17 log4j.properties -rw-r--r--. 1 root root 922 5月 8 18:17 zoo_sample.cfg [root@localhost conf]# cp zoo_sample.cfg zoo.cfg //copy一份到当前目录下,并命名为zoo.cfg [root@localhost conf]# ll 总用量 16 -rw-r--r--. 1 root root 535 5月 8 18:17 configuration.xsl -rw-r--r--. 1 root root 2161 5月 8 18:17 log4j.properties -rw-r--r--. 1 root root 922 5月 8 18:29 zoo.cfg -rw-r--r--. 1 root root 922 5月 8 18:17 zoo_sample.cfg [root@localhost conf]#
5.修改zoo.cfg文件如下:
vi zoo.cfg
7加入下面
dataDir= /usr/local/zookeeper/data
# 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/zookeeper/data # 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 ~
8.最后一步配置环境变量:
[root@localhost zookeeper]# vi /etc/profile //编辑文件 [root@localhost zookeeper]# source /etc/profile //使生效
9添加如下内容:
export ZOOKEEPER=/usr/local/zookeeper export PATH=$PATH:$ZOOKEEPER/bin
10.启动Zookeeper
先关闭防火墙(如果未关闭会报错Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect)
systemctl stop firewalld systemctl status firewalld
因为配置了环境变量,所以在任意目录下都可以运行以下启动命令启动Zookeeper。
[root@localhost ~]# zkServer.sh start //启动 [root@localhost ~]# zkServer.sh status //查看运行状态编写模块cloud-provider-payment8004
改pom
org.springframework.cloud spring-cloud-starter-zookeeper-discovery2.2.0.RELEASE org.apache.zookeeper zookeeperorg.apache.zookeeper zookeeper3.4.9 org.slf4j slf4j-log4j12com.chen cloud-api-commons${project.version} org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-devtoolsorg.springframework.boot spring-boot-starter-test
写yaml
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.184.30:2181 #ip为linux的ip
主启动
@SpringBootApplication
@EnableDiscoveryClient //该注解使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
controller
@RestController
public class PaymentConroller {
@Value("${server.port}")
private String serverport;
@GetMapping("/payment/zk")
public String paymentZk(){
return "springcloud with zookeeper: "+serverport+"t"+ UUID.randomUUID().toString();
}
}
查看本机ip
C:UsersDELL>ipconfig
ping一下
linux和Windows都ping一下
启动8004
http://localhost:8004/payment/zk
启动linux的客户端
zkCli.sh
然后返回linux查看注册进的服务
ls /services
查看流水号
ls /services/cloud-provider-payment
get /services/cloud-provider-payment/1cdfba21-1d5f-4799-889b-8bdfdfac2fa1思考:
服务节点是临时节点还是持久节点
是临时节点
写消费模块cloud-consumerZK-order80
改pom
org.springframework.cloud spring-cloud-starter-zookeeper-discovery2.2.0.RELEASE org.apache.zookeeper zookeeperorg.apache.zookeeper zookeeper3.4.9 org.slf4j slf4j-log4j12com.chen cloud-api-commons${project.version} org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-devtoolsorg.springframework.boot spring-boot-starter-test
写yaml
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.184.30:2181
主启动
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKmain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKmain80.class,args);
}
}
测试
http://localhost/consumer/payment/zk
zkCli.sh ls /services



