- 一、Linux配置ZooKeeper服务
- ZooKeeper单点配置
- 1.下载ZooKeeper:
- 2.配置ZooKeeper:
- 3.配置环境变量
- 4.启动Zookeeper
- ZooKeeper集群配置
- 1.修改zoo.cfg配置文件
- 2.新建myid文件
- 3.重启三个zookeeper服务
- 二、创建ZooKeepre客户端应用
- 1.提供者
- 2.消费者
- 三、测试
- 1.启动项目
- 2.访问接口
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.gz2.配置ZooKeeper:
$cd apache-zookeeper-3.5.5-bin/conf $ls
复制一份zoo_sample.cfg文件命名为zoo.cfg并修改内容如下。
cp zoo_sample.cfg zoo.cfg3.配置环境变量
$vi /etc/profile
在文件最后加上如下配置
export ZOOKEEPER=/usr/local/zookeeper/apache-zookeeper-3.5.5-bin export PATH=$PATH:$ZOOKEEPER/bin
然后执行如下命令使环境生效
$source /etc/profile4.启动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选举时使用
在/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配置文件是对应的。
zkServer.sh restart //重启
我第一个启动的第二台120.77.222.219服务器。
GitHub项目地址:https://github.com/ZiXinZhu/zookeeper
1.提供者-
添加maven依赖
org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-zookeeper-discovery mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.apache.zookeeper zookeeper 3.5.3-beta org.slf4j slf4j-log4j12 log4j log4j -
配置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 ListgetList() { return new ArrayList () { { add("add"); } }; } }
- 添加maven依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.springframework.cloud spring-cloud-starter-feign 1.4.6.RELEASE org.apache.zookeeper zookeeper 3.5.3-beta org.slf4j slf4j-log4j12 log4j log4j org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test - 配置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") ListgetList(); } - 控制层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 ListgetList() { return server.getList(); } }
分别启动提供者和消费者,然后打开ZooInspector界面管理工具如下
GitHub项目地址:https://github.com/ZiXinZhu/zookeeper



