安装Dubbo,并利用zookeeper作为注册中心
环境准备:
了解dubbo相关信息http://dubbo.io/Home-zh.htm
dubbo安装:
Zookeeper下载 download
zookeeper Install 伪分布式
1.解压下载好的zookeeper,到服务器上,解压并重命名
wget http://www.apache.org/dist/zookeeper/stable/zookeeper-3.4.6.tar.gz tar zxvf zookeeper-3.4.6.tar.gz mv zookeeper-3.4.6 zookeeper-3.4.6-server-1
2.进入conf目录,复制配置文件,并修改参数
cp zoo_sample.cfg zoo.cfg vi zoo.cfg tickTime=2000 initLimit=5 syncLimit=2 dataDir=/zk/zookeeper-3.4.6-server-1/zookeeper-3.4.6/data dataLogDir=/zk/zookeeper-3.4.6-server-1/zookeeper-3.4.6/logs clientPort=2182 server.1=127.0.0.1:8880:7770 server.2=127.0.0.1:8881:7771 server.3=127.0.0.1:8882:7772
其中需要修改的 dataDir dataLogDir clientPort ,端口不能重复! 一般我喜欢在conf同级目录下 新建 data 和 logs 文件夹
引用 http://coolxing.iteye.com/blog/1871009
- initLimit: zookeeper集群中的包含多台server, 其中一台为leader, 集群中其余的server为follower. initLimit参数配置初始化连接时, follower和leader之间的最长心跳时间. 此时该参数设置为5, 说明时间限制为5倍tickTime, 即5*2000=10000ms=10s.
- syncLimit: 该参数配置leader和follower之间发送消息, 请求和应答的最大时间长度. 此时该参数设置为2, 说明时间限制为2倍tickTime, 即4000ms.
- server.X=A:B:C 其中X是一个数字, 表示这是第几号server. A是该server所在的IP地址. B配置该server和集群中的leader交换消息所使用的端口. C配置选举leader时所使用的端口. 由于配置的是伪集群模式, 所以各个server的B, C参数必须不同.
3.进入 conf/zoo.cfg 里指定的 data 文件夹,新建
myid
文件,用于标识此zk实例是集群中的哪一个,该数字必须和
zoo.cfg
文件中的
server.X
中的X相对应.
cd /home/web/xiaolong.yuanxl/zookeeper-3.4.6-server-1/data echo "1" > myid
4.同理 cp 出 zookerper-3.4.6-server-2 和 zookerper-3.4.6-server-3,注意修改 data 、 logs、clientPort 、myid 这4个地方,然后依次启动zk ( 关闭stop 重启restart 状态status ) 并看启动情况
我的server1,server2,server3的端口号分别为:2182,2183,2184;myid为1,2,3
启动这三个zookeeper
cd /zk/zookeeper-3.4.6-server-1/zookeeper-3.4.6/binsh zkServer.sh start less ./zookeeper.out
5.这时查看zk的进程,是否是预期的3个,以及状态(2个foller、1个leader)
[[email protected] bin]$ ps -ef | grep zoo web 553 1 0 10:22 ? 00:00:01 /usr/java/jdk1.6.0_20/bin/java -Dzookeepeweb 778 1 0 10:26 ? 00:00:01 /usr/java/jdk1.6.0_20/bin/web 1167 1 0 10:37 ? 00:00:01 /usr/java/jdk1.6.0_20/bin/leader信息
[[email protected]_Dev1 bin]# pwd
/zk/zookeeper-3.4.6-server-3/zookeeper-3.4.6/bin
[[email protected]_Dev1 bin]# sh zkServer.sh status
JMX enabled by default
Using config: /zk/zookeeper-3.4.6-server-3/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
[[email protected]_Dev1 bin]# cd /zk/zookeeper-3.4.6-server-2/zookeeper-3.4.6/bin
[[email protected]_Dev1 bin]# sh zkServer.sh status
JMX enabled by default
Using config: /zk/zookeeper-3.4.6-server-2/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader
[[email protected]_Dev1 bin]# cd /zk/zookeeper-3.4.6-server-1/zookeeper-3.4.6/bin
[[email protected]_Dev1 bin]# sh zkServer.sh status
JMX enabled by default
Using config: /zk/zookeeper-3.4.6-server-1/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
dubbo admin 安装(web管理工具)
由于dubbo官网已经打不开了,这里是我的云盘 共享
http://pan.baidu.com/s/1pJ7o9PT
官网给出的 zk安装
官网给出的 dubbo-admin安装
1.下载dubbo-admin-2.5.4.war包,放在tomcat/webapps下
更改dubbo.properties 文件
vi ../webapps/dubbo-admin-2.5.4/WEB-INF/dubbo.propertiesdubbo.registry.aaaddress=zookeeper://127.0.0.1:2183 dubbo.admin.root.password=root dubbo.admin.guest.password=guest
随意更改之前配置过的端口号;
启动tomcat,访问http://ip:8080/dubbo-admin-2.5.4/,输入默认的用户名root密码:root
dubbo Provider Consumer 示例
即官网 首页示例
Provider:接口
package com.fpx.dubbo; public interface DemoService { public void sayHello(); }
Provider:实现
package com.fpx.dubbo; public class DemoServiceImpl implements DemoService { public void sayHello() { System.out.println("hello 00!"); } }
P
rovider 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-world-app" /> <dubbo:registry protocol="zookeeper" address="zookeeper://172.16.30.206:2183?backup=172.16.30.206:2182,172.16.30.206:2184" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.fpx.dubbo.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.fpx.dubbo.DemoServiceImpl" /> </beans>
P
rovider启动类
package com.fpx.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProviderDemo { public static void main(String[] args) throws InterruptedException { new ClassPathXmlApplicationContext(new String[]{"provider.xml" }); while (true) { } } }
Consumer配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://172.16.30.206:2183?backup=172.16.30.206:2182,172.16.30.206:2184" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.fpx.dubbo.DemoService"/> </beans>
Consumer启动类
package com.fpx.dubbo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboComsumeDemo { public static void main(String[] args) throws InterruptedException { ApplicationContext factory = new ClassPathXmlApplicationContext( new String[] {"comsumer.xml"}); DemoService demoService =(DemoService)factory.getBean("demoService"); demoService.sayHello(); } }
先启动Provider,再启动Consumer
控制台也输出了"hello 00!";
碰到的问题:
[INFO ] 11:48:16 (ClientCnxn.java:975) - Opening socket connection to server 172.16.30.206/172.16.30.206:2182. Will not attempt to authenticate using SASL (unknown error) [INFO ] 11:48:26 (ClientCnxn.java:1096) - Client session timed out, have not heard from server in 10001ms for sessionid 0x0, closing socket connection and attempting reconnect [DEBUG] 11:48:26 (ClientCnxnSocketNIO.java:203) - Ignoring exception during shutdown input java.net.SocketException: Socket is not connected at sun.nio.ch.Net.translateToSocketException(Net.java:149) at sun.nio.ch.Net.translateException(Net.java:183) at sun.nio.ch.Net.translateException(Net.java:189) at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:428) at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:200) at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1185) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1110) Caused by: java.nio.channels.NotYetConnectedException at sun.nio.ch.SocketChannelImpl.shutdownInput(SocketChannelImpl.java:800) at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:426) ... 3 more [DEBUG] 11:48:26 (ClientCnxnSocketNIO.java:210) - Ignoring exception during shutdown output
启动服务时报连接失败,检查后原来是我ubuntu防火墙问题;
解决方法:
vi /etc/sysconfig/iptables
增加端口信息:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2182 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2183 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2184 -j ACCEPT
重启防火墙
service iptables restart