0


Mac 下ZooKeeper安装和使

Mac 下ZooKeeper安装和使用

Apache ZooKeeper分布式协调系统是构建分布式应用程序的高性能服务。

1.下载ZooKeeper

环境要求:ZooKeeper服务器是用Java创建的,它运行在JVM之上。需要安装JDK 7或更高版本。

https://zookeeper.apache.org/releases.html

或者直接去资源包目录下载

https://archive.apache.org/dist/zookeeper/

2. 配置ZooKeeper

Tomcat

一样,下载后,解压到你指定的目录即可。

进入conf文件夹中,copy一份配置文件,并指定你的存储数据

data

目录

你可以使用

dataDir=../data

这样就可以在你的conf的上级目录创建data目录,用于存储数据

3. 启动ZooKeeper

进入bin目录

启动命令

./zkServer.sh start

查看状态

./zkServer.sh status

停止命令

./zkServer.sh stop

启动客户端

./zkCli.sh

4. 配置环境变量(可选)

打开终端输入以下命令,打开

.bash_profile

文件

sudo vi ~/.bash_profile

.bash_profile

添加你的

zkServer

所在目录。

这样你打开终端可以在任意目录下启动

zkServer
zkServer.sh start

5. ZooKeeper在Spring中注册

新建Spring项目注册zookeeper服务,配合dubbo使用

  1. pom.xml添加如下配置<packaging>war</packaging><properties><dubbo.version>2.7.4.1</dubbo.version><zookeeper.version>4.0.0</zookeeper.version></properties><!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version></dependency><!--ZooKeeper客户端实现 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>${zookeeper.version}</version></dependency><!--ZooKeeper客户端实现 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>${zookeeper.version}</version></dependency>注意:注册zookeeper服务,Service模块要打包方式要改为war包
  2. applicationContext.xml添加如下配置<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scanbase-package="com.example.service"/><!--dubbo的配置--><!--1.配置项目的名称,唯一--><dubbo:applicationname="dubbo-service"><dubbo:parameterkey="qos.port"value="20282"/></dubbo:application><!-- 修改端口号--><dubbo:protocolport="20281"/><!--2.配置注册中心的地址--><dubbo:registryaddress="zookeeper://127.0.0.1:2181"/><dubbo:metadata-reportaddress="zookeeper://127.0.0.1:2181"/><!--3.配置dubbo包扫描--><dubbo:annotationpackage="com.example.service.impl"/></beans>
  3. 在Service的类上面改用dubbo下的@Service注解importorg.apache.dubbo.config.annotation.Service;//@Service@ServicepublicclassUserServiceImplimplementsUserService{publicStringsayHello(){return"Hello DubboAndZookeeper";}publicUsergetUser(){User user =newUser(1,"zhangsan","123456");return user;}}注意:如果使用自定义对象,那么这个对象要实现Serializable接口

6. ZooKeeper在Spring中使用

  1. 在SpringWeb项目的pom.xml文件中添加如下依赖<packaging>war</packaging><properties><dubbo.version>2.7.4.1</dubbo.version><zookeeper.version>4.0.0</zookeeper.version></properties><!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version></dependency><!--ZooKeeper客户端实现 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>${zookeeper.version}</version></dependency><!--ZooKeeper客户端实现 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>${zookeeper.version}</version></dependency>
  2. applicationContext.xml添加如下配置<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><mvc:annotation-driven/><context:component-scanbase-package="com.example.controller"/><!--dubbo的配置--><!--1.配置项目的名称,唯一--><dubbo:applicationname="dubbo-web"><dubbo:parameterkey="qos.port"value="33333"/></dubbo:application><!-- 修改端口号--><dubbo:protocolport="20282"/><!--2.配置注册中心的地址--><dubbo:registryaddress="zookeeper://127.0.0.1:2181"/><!--3.配置dubbo包扫描--><dubbo:annotationpackage="com.example.controller"/></beans>
  3. Controller类上面把@Autowired注入改为@Reference注入@RestController@RequestMapping("/user")publicclassUserController{// @Autowired/* 1. 从zookeeper注册中心获取userService的访问url 2. 进行远程调用RPC 3. 将结果封装为一个代理对象。给变量赋值 */@Reference//远程注入publicUserService userService;@RequestMapping("/sayHello")publicStringsayHello(){return userService.sayHello();}@RequestMapping("/getUserById")publicStringgetUserById(){User user = userService.getUser();System.out.println(user.toString());return user.toString();}}

本文转载自: https://blog.csdn.net/u010495996/article/details/126512001
版权归原作者 倾锋落颖花 所有, 如有侵权,请联系我们删除。

“Mac 下ZooKeeper安装和使”的评论:

还没有评论