服务注册与发现---ZooKeeper

本文详细介绍了如何使用ZooKeeper进行服务注册与发现,包括依赖引入、配置修改、注解使用及服务启动步骤。通过具体实例展示了微服务如何在ZooKeeper中注册并被发现。

1. 引入zookeeper的依赖

这里要注意自己在linux上安装的ZooKeeper的版本,因为我使用的是3.4.6,所以先排除spring-cloud-starter-zookeeper-discoveryzookeeper 在引入与注册中心一样的版本。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <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.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. 修改application.yml

server:
  port: 8004

# 服务别名,zookeeper配置
spring:
  application:
    name: cloud-payment-service
  cloud:
    zookeeper:
      # zookeeper的地址
      connect-string: 192.168.110.128:2181

3. 主类上加@EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentZKApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentZKApplication.class, args);
    }
}

4. 服务启动

  • 启动zookeeper
[root@yp-centos bin]# ./zkServer.sh start
JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
  • 启动微服务
  • 查看服务注册
[root@yp-centos bin]# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 3] ls /
[dubbo, services, zookeeper]
[zk: localhost:2181(CONNECTED) 4] ls /services
[cloud-payment-service]
[zk: localhost:2181(CONNECTED) 5] ls /services/cloud-payment-service
[dee02869-19b0-4d85-808d-fc274e1ecf2d]
[zk: localhost:2181(CONNECTED) 6] ls /services/cloud-payment-service/dee02869-19b0-4d85-808d-fc274e1ecf2d
[]
[zk: localhost:2181(CONNECTED) 7] get /services/cloud-payment-service/dee02869-19b0-4d85-808d-fc274e1ecf2d
{"name":"cloud-payment-service","id":"dee02869-19b0-4d85-808d-fc274e1ecf2d","address":"localhost","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-payment-service","metadata":{}},"registrationTimeUTC":1588153010388,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
cZxid = 0x563
ctime = Mon Apr 13 14:17:09 CST 2020
mZxid = 0x563
mtime = Mon Apr 13 14:17:09 CST 2020
pZxid = 0x563
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x17171ee8c9c0001
dataLength = 528
numChildren = 0
  • 在启动一个服务查看服务注册
[zk: localhost:2181(CONNECTED) 12] ls /services
[cloud-payment-service, cloud-order-service]

这样cloud-order-service 就可以通过zookeeper调用cloud-payment-service

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值