本文是在Linux系统下的的dubbo+zookeeper+Spring的单机配置步骤,不对配置过程中各种配置项进行解释。
环境背景:
- 操作系统及开发软件:CentOS 64位、eclipse 4.4.2
- JDK版本:1.7
- zookeeper版本:zookeeper-3.3.6
- dubbo版本:2.5.3
- 其他相关说明:项目基于maven管理,共两个,dubboProvider和dubboConsumer。
zookeeper-3.3.6: http://download.youkuaiyun.com/detail/session_l/9738858
dubbo-admin: http://download.youkuaiyun.com/detail/session_l/9738894 dubbo可视化管理工具,需要在web服务器中使用(可选项)
一、安装和配置zookeeper注册中心,将下载好的zookeeper-3.3.6.tar.gz放在CentOS系统的opt或usr/local文件夹下,使用tar -xzvf zookeeper-3.3.6.tar.gz命令解压,进入zookeeper目录下conf文件夹,修改zoo_sample.cfg为zoo.cfg(为什么官网不直接用zoo.cfg命名?(⊙o⊙)…),暂时先使用默认配置,监听2181端口。至此zookeeper安装已完成;进入zookeeper目录下bin文件夹,执行 ./zkServer.sh 命令,启动zookeeper注册中心服务,可使用 ps -aux | grep 'zookeeper' 命令查看zookeeper服务是否启动成功。确认服务启动成功后,使用命令 ./zkCli.sh –server 自己的ip地址:2181 连接到zookeeper服务,同样可使用ps -aux | grep 'zookeeper' 命令查看是否启动成功。
二、dubbo的使用(只提供部分代码,希望本文的读者可以自己动手实践一下)
dubboProvider项目(服务提供者):
1、创建服务接口及接口实现
public interface ProviderService {
String helloWorld();
String sayHello(String name);
}
public class ProviderServiceImpl implements ProviderService {
public String helloWorld() {
return "hello,World";
}
public String sayHello(String name) {
return "hello , " + name;
}
}
2、spring配置zookeeper及dubbo等相关配置
<?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-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- Dubbo基于Spring的Schema扩展加载 -->
<bean id="providerService" class="dubbotest.provider.ProviderServiceImpl" />
<dubbo:application name="soar_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.184.130:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="dubbotest.provider.ProviderService" ref="providerService" />
</beans>
3、加载spring配置,启动服务。至此提供者工作已完成。
public static void init(String path) {
// 加载spring
try (AbstractApplicationContext context = new ClassPathXmlApplicationContext(path);) {
synchronized (Start.class) {
try {
System.out.println("============start =================");
Start.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Start.init("applicationContext.xml" );
}
dubboConsumer项目(服务消费者):
1、通过Spring配置调用远程服务
<?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-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不与提供方一样 -->
<dubbo:application name="lxy_consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://192.168.184.130:2181" />
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="demoService"
interface="dubbotest.provider.DemoService" />
</beans>
2、调用服务进行测试
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
context.start();
ProviderService demoService = (ProviderService) context.getBean("demoService");
System.out.println(demoService.helloWorld());
System.out.println(demoService.sayHello("Soar"));
}
3、运行结果
hello,World
hello,Soar
三、注意事项
1、使用dubbo前记得添加xml的Schema扩展
2、记得添加相关jar包,本项目是基于maven管理,需添加pom引入jar包