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目录

Mac 下ZooKeeper安装和使_第1张图片

你可以使用

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所在目录。

Mac 下ZooKeeper安装和使_第2张图片

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

zkServer.sh start

5. ZooKeeper在Spring中注册

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

  1. pom.xml添加如下配置

    <packaging>warpackaging>
    <properties>
            <dubbo.version>2.7.4.1dubbo.version>
            <zookeeper.version>4.0.0zookeeper.version>
    properties>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-frameworkartifactId>
                <version>${zookeeper.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>${zookeeper.version}version>
            dependency>
    

    **注意:**注册zookeeper服务,Service模块要打包方式要改为war包

  2. applicationContext.xml添加如下配置

    
    <beans xmlns="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-scan base-package="com.example.service" />
    
    	
    	
    	<dubbo:application name="dubbo-service">
    		<dubbo:parameter key="qos.port" value="20282"/>
    	dubbo:application>
    
    	<dubbo:protocol port="20281"/>
    	
    	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    	<dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
    	
    	<dubbo:annotation package="com.example.service.impl" />
    beans>
    
  3. 在Service的类上面改用dubbo下的@Service注解

    import org.apache.dubbo.config.annotation.Service;
    
    //@Service
    @Service
    public class UserServiceImpl implements UserService {
    
        public String sayHello() {
            return "Hello DubboAndZookeeper";
        }
    
        public User getUser() {
            User user = new User(1, "zhangsan", "123456");
            return user;
        }
    }
    

    **注意:**如果使用自定义对象,那么这个对象要实现Serializable接口

6. ZooKeeper在Spring中使用

  1. 在SpringWeb项目的pom.xml文件中添加如下依赖

    <packaging>warpackaging>
    <properties>
            <dubbo.version>2.7.4.1dubbo.version>
            <zookeeper.version>4.0.0zookeeper.version>
    properties>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-frameworkartifactId>
                <version>${zookeeper.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>${zookeeper.version}version>
            dependency>
    
  2. applicationContext.xml添加如下配置

    
    <beans xmlns="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-scan base-package="com.example.controller"/>
        
        
        <dubbo:application name="dubbo-web" >
            <dubbo:parameter key="qos.port" value="33333"/>
        dubbo:application>
        
        <dubbo:protocol port="20282"/>
        
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        
        <dubbo:annotation package="com.example.controller" />
    beans>
    
  3. Controller类上面把@Autowired注入改为@Reference注入

    @RestController
    @RequestMapping("/user")
    public class UserController {
    //    @Autowired
        /*
            1. 从zookeeper注册中心获取userService的访问url
            2. 进行远程调用RPC
            3. 将结果封装为一个代理对象。给变量赋值
    
         */
        @Reference//远程注入
        public UserService userService;
        @RequestMapping("/sayHello")
        public String sayHello(){
            return userService.sayHello();
        }
    
        @RequestMapping("/getUserById")
        public String getUserById(){
            User user = userService.getUser();
            System.out.println(user.toString());
            return user.toString();
        }
    }
    

你可能感兴趣的:(Java语法,java-zookeeper,macos,zookeeper)