spring-cloud系列之——注册中心Zookeeper

一、为什么使用Zookeeper 作为注册中心

  1. 默认SpringCloud使用Eureka作为注册中心
  2. 因为Eureka闭源,所以使用Zookkeeper作为注册中心
  3. 3.Zookeeper分布式协调工具,可以实现注册中心,采用临时节点,和生命周期进行关联,如果服务断开连接,该节点会自动被删除

二、Docker 中安装zookeeper

  • docker-compose.yml 配置文件
    version: '3.1'
    services:
      zookeeper:
        image: zookeeper:3.5
        restart: always
        ports:
          - 2181:2181
        logging:
          driver: "json-file"
          options:
            max-size: "10k"
            max-file: "10"
    
  • 启动zookeeper
    docker-compose up
    

三、这里使用的spring-cloud版本

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.0.6.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>Finchley.SR1</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
   </dependencies>
</dependencyManagement>

四、创建spring-cloud-zookeeper-server工程

  • pom.xml文件
        <dependencies>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- SpringBoot整合zookeeper客户端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
  • bootstrap.yml文件
    server:
      port: 8080
    spring:
      application:
        name: spring-cloud-zookeeper-sever
      cloud:
        zookeeper:
          # zookeeper 的地址,集群可以用“,”分隔
          connect-string: 192.168.199.214:2181
          discovery:
          	# 以服务的方式启动
            enabled: true
    
  • 工程启动类
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ZookeeperServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ZookeeperServerApplication.class, args);
        }
    
    }
    
  • 对外提供接口的controller
    @RestController
    public class HelloWorldController {
    
    
        @GetMapping("/hi")
        public String HelloWorld() {
            return "Hello World!";
        }
    
    }
    
  • 访问/hi 接口
    curl -X GET http://localhost:8080/hi
    Hello World!
    

关于我

  • 关注不迷路,点赞走一波~ 转载请标注~
  • 公众号
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值