Zookeeper在linux下的装置

去官网下载Zookeeper压缩包,解压到服务器的任意目录下。进入conf文件夹,有一个zoo_sample.cfg的文件,关上编辑进行编辑:

# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sakes.dataDir=/tmp/zookeeper# the port at which the clients will connectclientPort=2181# the maximum number of client connections.# increase this if you need to handle more clients#maxClientCnxns=60## Be sure to read the maintenance section of the # administrator guide before turning on autopurge.## http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance## The number of snapshots to retain in dataDir#autopurge.snapRetainCount=3# Purge task interval in hours# Set to "0" to disable auto purge feature#autopurge.purgeInterval=1## Metrics Providers## https://prometheus.io Metrics Exporter#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider#metricsProvider.httpPort=7000#metricsProvider.exportJvmInfo=true#开启四字命令4lw.commands.whitelist=*

其中dataDir能够设置为本人制订的目录,用来寄存快照信息,同时能够增加dataLogDir来配置日志寄存的门路,默认与dataDir同一地位。最初记得开启四字命令配置,不便查看服务器状态。
最初一步,将编辑好的文件改名或复制一份叫zoo.cfg的文件作为配置文件。
(留神这是单机版的配置,非集群)

Zookeeper环境变量配置

关上/etc/profile文件编辑,加上如下语句:

export ZOOKEEPER_HOME=/home/admin/apache-zookeeper-3.6.3-binexport PATH=$PATH:$ZOOKEEPER_HOME/bin

保留退出。

启动Zookeeper

进入bin目录,执行./zkServer.sh start启动服务,同时也有stop、restart等其余命令。

应用自带客户端连贯Zookeeper

进入bin目录,执行./zkCli.sh,即可连贯到服务器,应用help命令能够查看指令,可进行创立节点等操作。

Springboot整合Dubbo

创立一个SpringBoot我的项目作为provider,在pom中退出如下依赖:

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo-spring-boot-starter</artifactId>            <version>2.7.8</version>        </dependency>                <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->        <dependency>            <groupId>org.apache.curator</groupId>            <artifactId>curator-recipes</artifactId>            <version>5.2.0</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->        <dependency>            <groupId>org.apache.curator</groupId>            <artifactId>curator-framework</artifactId>            <version>5.2.0</version>        </dependency>

目前这个版本的Dubbo须要curator的依赖。
在application.properties中配置:

server.port=8080spring.application.name=dubbo-provider-demo#蕴含dubbo注解心愿被dubbo扫描的包dubbo.scan.base-packages=com.guomz.springbootdubbo.dubboService#用于直连dubbo服务,不通过zookeeperdubbo.protocol.name=dubbodubbo.protocol.port=20880## 配置zookeeper信息##如果采纳直连形式不应用zookeeper则须要将address配置为N/A,不配置protocal#dubbo.registry.address=N/Adubbo.registry.protocol=zookeeperdubbo.registry.address=zookeeper://39.105.145.179:2181

之后新创建一个maven工程,并编写服务接口,例如:

public interface MyService {    String sayHello();    Student getStudent();}

之后应用maven install命令将其打入本地maven仓库(也能够是本人的近程仓库),不便后续应用。
接口创立好之后在springboot provider中退出这个依赖,创立服务类实现这个接口并退出Dubbo注解:

import org.apache.dubbo.config.annotation.DubboService;import org.guomz.entity.Student;import org.guomz.service.MyService;@DubboService(version = "1.0.0")public class MyServiceImpl implements MyService {    @Override    public String sayHello() {        System.out.println("被调用了");        return "hello";    }    @Override    public Student getStudent() {        Student student = new Student();        student.setId(1L);        student.setName("guomz");        System.out.println("被调用了");        return student;    }}

其中@DubboService注解用于将以后类标识为dubbo服务,能够应用参数version与group来辨别不同的实现类。
再创立一个SpringBoot我的项目作为consumer消费者,依赖与provider统一。
application.properties配置同样与provider雷同,留神区别端口与服务名。
创立controller援用服务:

import org.apache.dubbo.config.annotation.DubboReference;import org.guomz.entity.Student;import org.guomz.service.MyService;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MyController {    @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:20880")    private MyService myService;    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String hello(){        return "hello";    }    @RequestMapping(value = "/sayhello", method = RequestMethod.GET)    public String callMyServiceHello(){        String result = myService.sayHello();        return result;    }    @RequestMapping(value = "/getstudent", method = RequestMethod.GET)    public Student getStudent(){        return myService.getStudent();    }}

其中@DubboReference用来援用dubbo服务,如果采纳直连provider的形式,须要给url参数赋值,赋值内容为provider配置文件中dubbo.protocal中配置的协定名与端口;如果应用zookeeper,则不须要配置这个参数。version参数须要与provider中配置的统一。