springcloud之组件zookeeper

Zookeeper服务注册与发现

1.下载安装linux版本的zookeeper

链接:https://pan.baidu.com/s/1SIRI5IPEiRz0TVQJ0vzLLQ 
提取码:r4xg

使用下面这个软件上传到linux

链接:https://pan.baidu.com/s/1u21hJwmzmvIDUdsvrMDhCw 
提取码:ykqc

2.安装步骤

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

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.chen</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

 写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:\Users\DELL>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

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.chen</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

写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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值