博主有一台阿里的服务器,所以就写一下在服务器上搭建Dubbo+Zookeeper的过程和遇到的坑。
服务器:CentOS 7.4
JDK版本: 1.7
tomcat : apache-tomcat-8.5.30
dubbo版本:2.6.0 war包,可以去github上面下载项目自己打war包,后续我会上传并提供下载地址。
zookeeper版本: 3.5.3 - beta 可以去官网去下载。 后续我会上传并提供下载地址。
需要先配置好JDK。具体操作百度。
好了,开始搭建吧,博主直接输入命令,可能会有些命令不对,如果发现了就提醒一下,
在linux中创建zookeeper文件夹
mkdir /usr/local/services/zookeeper
解压 zookeeper到这个文件夹
tar -zxvf zookeeper-3.5.3-beta.tar
将解压的文件移动到新建的zookeeper里
mv zookeeper-3.5.3-beta.tar /usr/local/services/zookeeper
进入 zookeeper的conf里
cd /usr/local/services/zookeeper/zookeeper-3.5.3-beta/conf
复制zoo_sample.cfg文件重命名zoo.cfg
cp zoo_sample.cfg zoo.cfg
编辑zoo.cfg , 看具体代码和注释。
admin.serverPor 这是博主(服务器上有很多tomcat)遇到的一个坑 因为tomcat内置的端口和zk的占用 所以修改了它的默认端口。
# 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
#数据文件夹 这里data需要注意 加集群信息的话 myid文件需要在data文件下创建
dataDir=/usr/local/services/zookeeper/zookeeper-3.5.3-beta/data
#日志文件夹
dataLogDir=/usr/local/services/zookeeper/zookeeper-3.5.3-beta/logs
# the port at which the clients will connect 2181是需要调用的一个端口号,需要在防火墙中放行
clientPort=2181
admin.serverPort=8099
# 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
#集群信息 没有就不用写下面的 ip对应的是服务器的ip
server.1=ip1:2888:3888
#server.2=ip2:2888:3888
#server.3=ip3:2888:3888
:wq 保存并退出。
配置zookeeper环境.
vim /etc/profile
加入以下code
对应自己的zookeeper安装地址。
# idea - zookeeper-3.5.3 config start - 2018-04-17
export ZOOKEEPER_HOME=/usr/local/services/zookeeper/zookeeper-3.5.3-beta/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
# idea - zookeeper-3.5.5 config start - 2018-04-17
:wq 保存退出 并使文件生效。
source /etc/profile
启动zookeeper
cd usr/local/services/zookeeper/zookeeper-3.5.3-beta/bin
./zkServer.sh start 启动
./zkServer.sh status 查看zookeeper运行状态
./zkServer.sh stop 停止
启动成功。 如果启动遇到什么可以问我哦。qq群:8028492。
接下来搭建dubbo。
创建tomcat文件夹并解压
mkdir /usr/local/tomcat 创建文件夹
tar -zxvf apache-tomcat-8.5.30.tar.gz 解压
mv apache-tomcat-8.5.30 /usr/local/tomcat
进入tomcat的webapps的ROOT目录下,删除所有东西,并把dubbo的war包移动过来并解压。
cd /usr/ /usr/local/tomcat/apache-tomcat-8.5.30/webapps/ROOT
在 ROOT 目录下执行删除ROOT下的所有东西 千万别搞错 哈哈哈 rm rf *
在war包目录执行 mv dubbo-admin-2.6.0.war /usr/local/tomcat/apache-tomcat-8.5.30/webapps/ROOT
tar -xvf dubbo-admin-2.6.0.war
接下来启动tomcat
cd /usr/local/tomcat/apache-tomcat-8.5.30/bin
./startup.sh
在浏览器访问tomcat 。 http://你的ip地址:tomcat端口号
就可以看到 dubbo-admin页面了
dubbo的密码账号配置在/usr/local/tomcat/apache-tomcat-8.5.30/webapps/ROOT/WEB-INF的dubbo.properties 里配置
默认用户名是root 密码 root。
接下来我们用IDEA创建一个项目来测试。 博主参考的dubbo官方的demo 在github上面可以找到
创建一个maven项目
在创建一个空的mavne项目 提供者
同上流程在创建另一个,消费者的
提供者代码如下:
DemoService.java
package com.demo.server;
/**
* @Author: Laban
* @Description:
* @Date: Created in 9:51 2018/4/19
* @Modfied:
*/
public interface DemoService{
/**
* dubbo服务提供者 server接口
*/
String sayHello(String name);
}
DemoServiceImpl.java
package com.demo.server.impl;
import com.demo.server.DemoService;
/**
* @Author: Laban
* @Description:
* @Date: Created in 9:53 2018/4/19
* @Modfied:
*/
public class DemoServiceImpl implements DemoService{
public String sayHello(String name) {
return "dubbo+zookeeper测试,名字是"+name;
}
}
pom.xml
demo
com.demo
1.0-SNAPSHOT
4.0.0
demo-provider
com.demo
demo
1.0-SNAPSHOT
com.alibaba
dubbo-config-spring
com.alibaba
dubbo-registry-zookeeper
com.alibaba
dubbo-registry-multicast
com.alibaba
dubbo-rpc-dubbo
com.alibaba
dubbo-remoting-netty
com.alibaba
dubbo-serialization-hessian2
com.alibaba
dubbo
2.6.1
org.apache.zookeeper
zookeeper
3.5.3-beta
org.apache.curator
curator-framework
4.0.1
dubbo-demo-provider.xml
log4j.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
测试类
package com.demo.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* @Author: Laban
* @Description:
* @Date: Created in 10:01 2018/4/19
* @Modfied:
*/
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"});
System.out.println("注册服务成功");
context.start();
try {
System.in.read(); // press any key to exit
} catch (IOException e) {
e.printStackTrace();
}
context.close();
}
}
下面是消费者
Consumer.java
package com.demo.test;
import com.demo.server.DemoServer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author: Laban
* @Description:
* @Date: Created in 11:37 2018/4/19
* @Modfied:
*/
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"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();
}
}
}
}
dubbo-demo-consumer.xml
log配置和上面的一样配置
pom.xml
demo
com.demo
1.0-SNAPSHOT
4.0.0
demo-consumer
com.demo
demo
1.0-SNAPSHOT
com.alibaba
dubbo-config-spring
com.alibaba
dubbo-registry-zookeeper
com.alibaba
dubbo-registry-multicast
com.alibaba
dubbo-rpc-dubbo
com.alibaba
dubbo-remoting-netty
com.alibaba
dubbo-serialization-hessian2
com.alibaba
dubbo
2.6.1
org.apache.zookeeper
zookeeper
3.5.3-beta
org.apache.curator
curator-framework
4.0.1
com.demo
demo-provider
1.0-SNAPSHOT
运行测试:
项目目录
到此结束。一些下载的连接后续补上,有什么问题可以找我。群:8028492