简单的Dubbo框架搭建过程,包括Dubbo所依赖的zookeeper安装。
下载地址为:https://mirrors.cnnic.cn/apache/zookeeper/
可从链接内选择一个合适的版本下载,我所使用的版本是zookeeper-3.4.10
修改目录下zookeeper-3.4.10\conf\zoo_sample.cfg文件内容,并将文件名修改为zoo.cfg。
实际上只需要修改dataDir和dataLogDir两个地方即可,dataDir为Zookeeper 保存数据的目录,dataLogDir为Zookeeper 保存日志文件的目录。
以下为我的配置文件,可以参考。
# 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=W:/Dubbo/zookeeper/data
dataLogDir=W:/Dubbo/zookeeper/log
# 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
找到目录\zookeeper-3.4.10\bin,双击zkServer.cmd,直接运行;这种方法遇到问题会闪退。
打开cmd,进入到\zookeeper-3.4.10\bin目录下,运行zkServer.cmd。
若双击zkServer.cmd出现闪退,可以使用第二种启动方法,或者在编辑zkServer.cmd在文末添加pause,来查找问题。
1.报错,缺失zoo.cfg
这个错是因为没有将conf/zoo_sample.cfg改名为zoo.cfg,改名之后即可解决。
2.JAVA_HOME没有设置
需要在环境变量的用户变量里添加JAVA_HOME,添加方法就不赘述了。
1.一共分三个模块,每个模块都有独立的pom文件。
稍微说明一下三个模块的作用。api内为公用接口,consumer调用远程服务,provider提供远程服务。
2.在consumer和provider的pom中添加依赖
com.alibaba
dubbo
2.6.0
com.alibaba
dubbo-demo-api
2.6.1
com.101tec
zkclient
0.9
3.在dubbo-demo-api内添加接口
public interface DemoService {
String sayHello(String name);
}
4..在dubbo-demo-provider内实现接口
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
修改配置文件
配置文件的内容为:
启动服务,注意要先启动zookeeper
public class Provider {
public static void main(String[] args) throws Exception {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
// System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.out.println("----------------服务已启动,按任意键结束···········--------------------");
System.in.read(); // press any key to exit
}
}
5.在dubbo-demo-consumer内调用服务
public class Consumer {
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("world"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
修改配置文件
下面是配置文件内容,注意registry的地址,按照需求修改。
先启动zookeeper,启动成功后启动provider,最后启动consumer
运行结果部分截图如下:
我只是写了一下我的搭建过程,如果想要更详细的了解请去看大神写的博文。
参考:http://blog.csdn.net/noaman_wgs/article/details/70214612/