自行百度,下载解压
(由于机器有限,本文使用一台机器不同端口来模拟实现集群模式)
1、创建zookeepere文件夹
2、zookeeper文件夹下新建server1、server2、server3三个文件夹
3、复制解压后的zookeeper文件夹到server1、server2、server3这三个文件夹下
4、再分别在这三个文件夹下创建data、dataLog两个目录
完成后如下图
进入zookeeper-3.4.14/conf文件夹,重命名zoo-sample.cfg为zoo.cfg,并修改配置如下:
# 心跳检测时间间隔,单位ms
tickTime = 2000
#zookeeper集群中包含多台server,其中一台为leader,其余为follower,initLimit参数配置初始化连接时,follower和leader之间的最长心跳时间,设置为5,则表示时间限制为5倍tickTime
initLimit = 5
#syncLimit配置leader和follower之间发送消息、请求和应答的最大时间长度,设置为2,则说明时间限制为2倍tickTime
syncLimit = 2
dataDir=/Library/zookeeper/server1/data
dataLogDir=/Library/zookeeper/server1/dataLog
clientPort=2181
server.1 = 127.0.0.1:2222:2225
server.2 = 127.0.0.1:3333:3335
server.3 = 127.0.0.1:4444:4445
其中dataDir为data文件夹所在的位置, dataLogDir为dataLog文件夹所在的位置
server1里的clientPort=2181, server2里的clientPort=2182, server3里的clientPort=2183
在data文件夹里新增myid文件,文件内容为serverID, 如server1的myid文件的内容为1,server2的myid文件内容为2,以此类推
终端进入zookeeper3.4.14/bin目录下,执行./zkServer.sh start启动zookeeper服务,执行./zkServer.sh status查看服务状态,当状态为一个leader, 2个follower时,则启动成功
项目结构如下
dubbotestapi项目下创建User对象
public class User implements Serializable{
private static final long serialVersionUID = 5264854701181085862L;
private Long id;
private String username;
private String password;
private Integer age;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建userAPI接口
public interface UserAPI {
public List getUsers();
String sayHello(String name);
}
将dubbotestapi打包生成jar引入dubbotestservice项目,并引入其他依赖的jar包
io.netty
netty-all
4.1.34.Final
com.alibaba
dubbo
2.6.6
spring-context
org.springframework
org.apache.zookeeper
zookeeper
3.4.14
com.github.sgroschupf
zkclient
0.1
zookeeper
org.apache.zookeeper
log4j
log4j
org.apache.curator
curator-framework
4.0.1
zookeeper
org.apache.zookeeper
slf4j-api
org.slf4j
配置dubbo-provider.xml文件
添加userAPI接口的实现类userAPIImpl文件
public class UserAPIImpl implements UserAPI{
public List getUsers() {
List list = new ArrayList();
for (int i=0;i<10;i++){
User user = new User();
user.setUsername("username_" + i);
user.setId(Long.valueOf(i+1));
user.setAge(10 + i);
user.setPassword("123456");
list.add(user);
}
return list;
}
public String sayHello(String name) {
return "11111";
}
引入依赖的jar包
com.qjj
dubbotest.api
1.0-SNAPSHOT
org.springframework
spring-context
4.3.12.RELEASE
org.springframework
spring-core
4.3.12.RELEASE
org.springframework
spring-web
4.3.12.RELEASE
org.springframework
spring-beans
4.3.12.RELEASE
org.springframework
spring-jdbc
4.3.12.RELEASE
org.springframework
spring-oxm
4.3.12.RELEASE
org.springframework
spring-tx
4.3.12.RELEASE
org.springframework
spring-webmvc
4.3.12.RELEASE
org.springframework
spring-aop
4.3.12.RELEASE
org.springframework
spring-test
4.3.12.RELEASE
io.netty
netty-all
4.1.34.Final
com.alibaba
dubbo
2.6.6
spring-context
org.springframework
org.apache.zookeeper
zookeeper
3.4.14
com.github.sgroschupf
zkclient
0.1
log4j
log4j
zookeeper
org.apache.zookeeper
org.apache.curator
curator-framework
4.2.0
zookeeper
org.apache.zookeeper
创建dubbo-consumer.xml文件
创建测试类
public class consumer {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "dubbo-consumer.xml" });
context.start();
UserAPI userAPI = context.getBean(UserAPI.class);
String hello = userAPI.sayHello("qijiaojiao");
System.out.println(hello);
List list = userAPI.getUsers();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
}
运行测试类,执行结果如下: