标签:bsp blog ice 分享图片 depend properly core ... sed
前言:在集群环境下使用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文件:
④.修改每个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注册中心部署并测试成功 !
标签:bsp blog ice 分享图片 depend properly core ... sed
原文地址:https://www.cnblogs.com/Json1208/p/8824755.html