4.motan之集群調用
前言:在集群環境下使用Motan需要依賴外部服務發現組件,目前支持consul或zookeeper,但是工作中最常用的是zookeeper,所以這裏以zookeeper作為註冊中心 !
一、zookeeper的安裝和啟動
說明:這裏以單機集群的方式來作為註冊中心
①.首先下載zk的tar.gz包,放到服務器上;
②.新建zookeeper-cluster目錄,將zk壓縮包解壓到該目錄,並copy兩份:
接著,配置環境變量:
vim /etc/profile
export PATH=$JAVA_HOME/bin:$PATH:$ZOOKEEPER_HOME1:$ZOOKEEPER_HOME2:$ZOOKEEPER_HOME3 export ZOOKEEPER_HOME1=/home/env/zookeeper-cluster/zookeeper1 export ZOOKEEPER_HOME2=/home/env/zookeeper-cluster/zookeeper2 export ZOOKEEPER_HOME3=/home/env/zookeeper-cluster/zookeeper3
註意:這裏幾個zookeeper目錄,就配置幾個路徑
最後,使配置文件生效:
source /etc/profile
③.在每個目錄下新建一個data目錄,並且新建個myid文件:
- 如果是zookeeper1,那麽在myid內寫1,保存退出;
- 如果是zookeeper2,那麽在myid內寫2,保存退出;
- 如果是zookeeper3,那麽在myid內寫3,保存退出;
④.修改每個zookeeper目錄的配置文件
進入zookeeper1--->conf目錄,將zk配置文件zoo_sample.cfg重命名為zoo.cfg;
打開配置文件zoo.cfg進行配置:
dataDir=/home/env/zookeeper-cluster/zookeeper1/data 這裏修改為第③步新建的data目錄路徑
clientPort=2181
修改端口
server.1=172.xx.xx.2:2881:3881
server.2=172.xx.xx.2:2882:3882
server.3=172.xx.xx.2:2883:3883
配置集群節點,這裏的ip地址可以使用ifconfig eth0來查看內網地址
註意:zookeeper2和zookeeper3除了端口修改為2882和2883,以及dataDir目錄修改為各自目錄下的data目錄除外,其它都跟這裏一致。
⑤.分別啟動3臺zk
執行:./zkServer.sh或者zkServer.sh start(環境變量已配置,所以可以這麽啟動)(註意:分別進入3臺機器的該目錄下,3臺機器都要執行此命令啟動) 狀態:zkServer.sh status(在3個節點上校驗zookeeper的mode,一個leader和兩個follower)
zk集群搭建成功 !
二、motan-zookeeper配置
1.以異步調用例子為例:http://www.cnblogs.com/Json1208/p/8799370.html,調整service實現,不再有休眠效果:
package com.motan.service; public class HelloWorldServiceImpl implements HelloWorldService{ @Override public String hello(String name) { System.out.println(name); return "Hello " + name + "!"; } }
2.由於我們使父子工程,所以原本需要在motan-client和motan-server中添加的zookeeper依賴,我們可以直接添加到motan-parent中:
<dependency> <groupId>com.weibo</groupId> <artifactId>motan-registry-zookeeper</artifactId> <version>1.0.0</version> </dependency>
3.在motan-server和motan-client的配置文件中分別增加zookeeper registry定義:
<!-- zookeeper多節點集群 --> <motan:registry regProtocol="zookeeper" name="my_zookeeper" address="xxx.xx.xx.212:2182,xxx.xx.xx.212:2181,xxx.xx.xx.212:2183"/> <!-- zookeeper單節點配置 --> <!-- <motan:registry regProtocol="zookeeper" name="my_zookeeper" address="xxx.xx.xx.212:2182"/> -->
註意:這裏的地址配置的是服務器的外網地址!
註意:這裏的zk配置在client和server端是一樣的 !
4.在motan-client及motan-server配置改為通過registry服務發現:
5.server程序啟動後,需要顯式調用心跳開關,註冊到zookeeper,調整motan-server啟動程序:
package com.motan.server; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.weibo.api.motan.common.MotanConstants; import com.weibo.api.motan.util.MotanSwitcherUtil; public class Server { @SuppressWarnings({ "unused", "resource" }) public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan-server.xml"); // server程序啟動後,需要顯式調用心跳開關,註冊到zookeeper MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); System.out.println("server start..."); } }
6.測試,啟動上方的server
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. server start...
7.啟動client
package com.motan.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.motan.service.HelloWorldServiceAsync; import com.weibo.api.motan.rpc.Future; import com.weibo.api.motan.rpc.FutureListener; import com.weibo.api.motan.rpc.ResponseFuture; public class Client { @SuppressWarnings("resource") public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan-client.xml"}); HelloWorldServiceAsync async = (HelloWorldServiceAsync) ctx.getBean("helloWorldReferer"); System.out.println(async.hello("motan")); } } log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. Hello motan!
zk集群做為motan註冊中心部署並測試成功 !
4.motan之集群調用