一、Linux配置ZooKeeper服务

ZooKeeper单点配置

1.下载ZooKeeper:

ZooKeeper官网下载:http://mirror.bit.edu.cn/apache/zookeeper/
在这里插入图片描述

$cd /usr/local
$mkdir zookeeper
$cd zookeeper
$wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz
$tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz

2.配置ZooKeeper:

$cd apache-zookeeper-3.5.5-bin/conf
$ls

在这里插入图片描述
复制一份zoo_sample.cfg文件命名为zoo.cfg并修改内容如下。

cp zoo_sample.cfg zoo.cfg

在这里插入图片描述

3.配置环境变量

$vi /etc/profile

在文件最后加上如下配置

export ZOOKEEPER=/usr/local/zookeeper/apache-zookeeper-3.5.5-bin
export PATH=$PATH:$ZOOKEEPER/bin

在这里插入图片描述
然后执行如下命令使环境生效

$source /etc/profile

4.启动Zookeeper

启动服务器端

zkServer.sh start //启动
zkServer.sh status  //查看状态

启动客户端

zkCli.sh   //启动客户端

在这里插入图片描述

ZooKeeper集群配置

1.修改zoo.cfg配置文件

在原来的服务器和新增的两台服务器的zoo.cfg配置文件后面加上如下配置(记住本机的ip用0.0.0.0代替如图)

server.1=123.207.231.159:2888:3888
server.2=120.77.222.219:2888:3888
server.3=47.106.128.4:2888:3888

123.207.231.159服务器配置:
在这里插入图片描述
120.77.222.219服务器配置:
在这里插入图片描述
最后一台服务器同理。
2181端口:服务连接zookeeper时使用
2888端口:在zookeeper服务器之间及和leader连接时使用,
3888端口:在进行leader选举时使用

2.新建myid文件

在/tmp/zookeeper/下新建myid文件

$cd /tmp/zookeeper/
$vi myid

如第二台服务120.77.222.219直接输入的 2。
在这里插入图片描述
在123.207.231.159的myid下输入1,120.77.222.219输入2,47.106.128.4输入3。与zoo.cfg配置文件是对应的。

3.重启三个zookeeper服务

zkServer.sh restart //重启

我第一个启动的第二台120.77.222.219服务器。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、创建ZooKeepre客户端应用

GitHub项目地址:https://github.com/ZiXinZhu/zookeeper

1.提供者

  • 添加maven依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--用于服务注册发现-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--连接zk服务的客户端工具包-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.3-beta</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
  • 配置application.properties配置文件

    server.port=8762
    spring.application.name=zk-provider
    spring.cloud.zookeeper.connect-string=120.77.222.219:2181
    spring.cloud.zookeeper.discovery.instance-id=1
    spring.cloud.zookeeper.discovery.enabled=true
    spring.cloud.zookeeper.discovery.register=true
    spring.cloud.zookeeper.discovery.root=/server
    
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://123.207.123.159:3306/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
    spring.datasource.username=root
    spring.datasource.password=password
    mybatis.configuration.map-underscore-to-camel-case=true
    
    spring.datasource.hikari.minimum-idle=5
    spring.datasource.hikari.maximum-pool-size=15
    spring.datasource.hikari.auto-commit=true
    spring.datasource.hikari.idle-timeout=30000
    spring.datasource.hikari.pool-name=DatebookHikariCP
    spring.datasource.hikari.max-lifetime=1800000
    spring.datasource.hikari.connection-timeout=30000
    spring.datasource.hikari.connection-test-query=SELECT 1
    

    参数的简单说明:
    connect-string:连接zk服务的套接字
    register: 是否启动服务注册
    instance-id: zk唯一id的标识
    root: zk根节点名称,默认/services

  • 启动类添加注解

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class, args);
        }
    }
    
  • DAO层UserDao

     import com.zzx.provider.po.UserPO;
     import org.apache.ibatis.annotations.Mapper;
     import org.apache.ibatis.annotations.Param;
     import org.apache.ibatis.annotations.Select;
     import org.springframework.stereotype.Repository;
     
     @Mapper
     @Repository
     public interface UserDao {
     
         @Select("SELECT * from girl where id=#{id}")
         UserPO findById(@Param("id")int id);
     }
    
  • 服务层UserServer

     import com.zzx.provider.dao.UserDao;
     import com.zzx.provider.po.UserPO;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.stereotype.Service;
     
     @Service
     public class UserServer {
     
         @Autowired
         UserDao userDao;
         public UserPO findById(){
             return userDao.findById(1);
         }
     }
    
  • 控制层UserController

     import com.zzx.provider.po.UserPO;
     import com.zzx.provider.server.UserServer;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.web.bind.annotation.GetMapping;
     import org.springframework.web.bind.annotation.RestController;
     import java.util.ArrayList;
     import java.util.List;
     
     @RestController
     public class UserController {
     
         @Autowired
         UserServer userServer;
     
         @GetMapping("/find")
         public UserPO findById(){
             return userServer.findById();
         }
         
         @GetMapping("/list")
         public List<String > getList() {
             return new ArrayList<String>() {
                 {
                     add("add");
                 }
             };
         }
     }
    

2.消费者

  • 添加maven依赖
     	<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.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.3-beta</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
  • 配置application.properties配置文件
    server.port=8763
    spring.application.name=zk-consumer
    
    spring.cloud.zookeeper.connect-string=120.77.222.219:2181
    spring.cloud.zookeeper.discovery.instance-id=1
    spring.cloud.zookeeper.discovery.enabled=true
    spring.cloud.zookeeper.discovery.register=true
    spring.cloud.zookeeper.discovery.root=/server
    
  • 启动类添加注解
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
     import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
     
     @SpringBootApplication
     @EnableDiscoveryClient
     public class ProviderApplication {
     
         public static void main(String[] args) {
             SpringApplication.run(ProviderApplication.class, args);
         }
     
     }
    
  • 服务器层IUserServer
     import org.springframework.cloud.openfeign.FeignClient;
     import org.springframework.stereotype.Service;
     import org.springframework.web.bind.annotation.GetMapping;
     import java.util.List;
     
     @Service
     @FeignClient("zk-provider")
     public interface IUserServer {
     
     
         @GetMapping("/find")
         String findById() ;
     
         @GetMapping("/list")
         List<String> getList();
     }
    
  • 控制层UserController
     import com.zzx.consumer.server.IUserServer;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.web.bind.annotation.GetMapping;
     import org.springframework.web.bind.annotation.RestController;
     import java.util.List;
     
     @RestController
     public class UserController {
     
     
         @Autowired
         protected IUserServer server;
     
         @GetMapping("/get")
         public String getUser() {
             return server.findById();
         }
     
         @GetMapping("/add")
         public List<String> getList() {
             return server.getList();
         }
     }
    

三、测试

1.启动项目

分别启动提供者和消费者,然后打开ZooInspector界面管理工具如下
在这里插入图片描述

2.访问接口

在这里插入图片描述
在这里插入图片描述
GitHub项目地址:https://github.com/ZiXinZhu/zookeeper

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐