Zookeeper在linux下的安装
去官网下载Zookeeper压缩包,解压到服务器的任意目录下。进入conf文件夹,有一个zoo_sample.cfg的文件,打开编辑进行编辑:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=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 connect
clientPort=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-bin
export PATH=$PATH:$ZOOKEEPER_HOME/bin
保存退出。
启动Zookeeper
进入bin目录,执行./zkServer.sh start启动服务,同时也有stop、restart等其他命令。
使用自带客户端连接Zookeeper
进入bin目录,执行./zkCli.sh,即可连接到服务器,使用help命令可以查看指令,可进行创建节点等操作。
Springboot整合Dubbo
创建一个SpringBoot项目作为provider,在pom中加入如下依赖:
org.apache.dubbo
dubbo-spring-boot-starter
2.7.8
org.apache.curator
curator-recipes
5.2.0
org.apache.curator
curator-framework
5.2.0
目前这个版本的Dubbo需要curator的依赖。
在application.properties中配置:
server.port=8080
spring.application.name=dubbo-provider-demo
#包含dubbo注解希望被dubbo扫描的包
dubbo.scan.base-packages=com.guomz.springbootdubbo.dubboService
#用于直连dubbo服务,不通过zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
## 配置zookeeper信息
##如果采用直连方式不使用zookeeper则需要将address配置为N/A,不配置protocal
#dubbo.registry.address=N/A
dubbo.registry.protocol=zookeeper
dubbo.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;
@RestController
public 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中配置的一致。