SpringCloud之Zookeeper作为注册中心

Spring Cloud Zookeeper通过自动配置和绑定到Spring环境,为Spring Boot应用程序提供Apache Zookeepper集成。通过一些简单的注释,可以快速启用和配置应用程序中的常见模式,并使用Zookeeper构建大型分布式系统。提供的模式包括服务发现和分布式配置。

 

特征

  • 服务发现:可以向Zookeeper注册实例,客户端可以使用Spring托管bean发现实例
  • 支持Spring Cloud LoadBalancer-客户端负载平衡解决方案
  • 支持Spring Cloud OpenFeign
  • 分布式配置:使用Zookeeper作为数据存储

要使用zookeeper作为注册中心只需要加入依赖:

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>

并在application.properties配置:

spring.cloud.zookeeper.connect-string=localhost:2181

启动zookeeper后,启动GatewayServer和Producer。访问http://localhost:8500/producer/hello,看到hello,null,8002。同时再启动端口是8004的Producer,访问http://localhost:8500/producer/hello,看到hello,null,8002和hello,null,8004交替出现,说明负载均衡生效。

 

连接上zookeeper后,运行命令ls /,看到services。在运行ls /services,看到:

 

在运行ls /services/producer,看到:

 

将zookeeper服务关闭后,在访问http://localhost:8500/producer/hello出现404。将zookeeper换成Eureka作为注册中心,关闭Eureka后依然能访问。zookeeper提供的是一致性(CP),Eureka提供的是可用性(AP)

posted @ 2023-05-11 20:15  shigp1  阅读(297)  评论(0)    收藏  举报