dubbo使用方法。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐),可以参见:
http://code.alibabatech.com/wiki/display/dubbo/User+Guide-zh#UserGuide-zh-API%E9%85%8D%E7%BD%AE
下面我们就来看看spring配置方式的写法。
服务提供者:
1. 下载zookeeper注册中心,下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下载后解压即可,进入D:\Tools\zookeeper\zookeeper-3.4.5\bin,
双击zkServer.cmd启动注册中心服务。
定义服务接口:
- package com.liangbinny.dubbotest.provider;
- import java.util.List;
- import com.liangbinny.dubbotest.provider.impl.User;
- public interface DemoService {
- String sayHello(String name);
- public List<User> getUsers();
- }
在服务提供方实现接口:(对服务消费方隐藏实现)
- package com.liangbinny.dubbotest.provider.impl;
- import java.util.ArrayList;
- import java.util.List;
- import com.liangbinny.dubbotest.provider.DemoService;
- public class DemoServiceImpl implements DemoService {
- public String sayHello(String name) {
- return "Hello " + name;
- }
- public List<User> getUsers() {
- List<User> list = new ArrayList<User>();
- User user = new User();
- user.setAge(28);
- user.setName("liangbinny");
- list.add(user);
- user = new User();
- user.setAge(27);
- user.setName("binny");
- list.add(user);
- return list;
- }
- }
用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.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- ">
- <!-- 具体的实现bean -->
- <bean id="demoService" class="com.liangbinny.dubbotest.provider.impl.DemoServiceImpl" />
- <!-- 提供方应用信息,用于计算依赖关系 -->
- <dubbo:application name="dubbo_provider" />
- <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234"
- /> -->
- <!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="zookeeper://10.102.30.211:2181" />
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 ,对应的具体实现类是自定义bean:demoService -->
- <dubbo:service interface="com.liangbinny.dubbotest.provider.DemoService" ref="demoService" />
- </beans>
加载Spring配置,启动服务:
- package com.liangbinny.dubbotest.provider.impl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Provider {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
- context.start();
- // 为保证服务一直开着,一直循环等待
- synchronized (Provider.class) {
- while (true) {
- try {
- Provider.class.wait();
- } catch (Throwable e) {
- }
- }
- }
- }
- }
整个服务端的项目图如下:
服务端开发好后,提供对应的jar包给消费端,提供对应的接口。
导出服务端项目为jar包即可。
新建消费端项目,依赖的jar包有:
其中包括刚刚导出的dubbo-server.jar包
服务消费者:
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.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- ">
- <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
- <dubbo:application name="dubbo_consumer" />
- <!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="zookeeper://10.102.30.211:2181" />
- <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
- <dubbo:reference id="demoService" interface="com.liangbinny.dubbotest.provider.DemoService" />
- </beans>
2.加载Spring配置,并调用远程服务:
- package com.liangbinny.dubbotest.consumer;
- import java.util.List;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.liangbinny.dubbotest.provider.DemoService;
- import com.liangbinny.dubbotest.provider.impl.User;
- public class Consumer {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
- context.start();
- DemoService demoService = (DemoService) context.getBean("demoService");
- String hello = demoService.sayHello("tom");
- System.out.println(hello);
- List<User> list = demoService.getUsers();
- System.out.println(list);
- }
- }
调用结果
1、下载dubbo控制台管理war包
http://download.csdn.net/detail/liweifengwf/7784901
官方地址:http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-admin/2.4.1/dubbo-admin-2.4.1.war(这个地址我一直下不了,wget也不通)
下载绿色版tomcat,并解压,删掉apache-tomcat-6.0.37\webapps\ROOT文件夹下的内容,并将dubbo-admin-2.4.1.war解压后,解压出的文件放在ROOT文件夹下
修改apache-tomcat-6.0.37\webapps\ROOT\WEB-INF下的dubbo.properties文件的zookeeper地址
dubbo.registry.address=zookeeper://10.102.30.211:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
2、启动
先启动zookeeper,再启动tomcat
访问:
http://localhost:8080
登录密码默认是:root/root
可以看到控制台管理界面,
这时候可以启动上篇文章中的提供者和消费者,再在dubbo管理控制台界面可以看到
提供者
消费者
Zookeeper单机配置:
<
dubbo:registry
address
=
"zookeeper://10.20.153.10:2181"
/>
Or:
<
dubbo:registry
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181"
/>
Zookeeper集群配置:
<
dubbo:registry
address
=
"zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181"
/>
Or:
< dubbo:registry protocol = "zookeeper" address = "10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />
|
同一Zookeeper,分成多组注册中心:
<
dubbo:registry
id
=
"chinaRegistry"
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181"
group
=
"china"
/>
<
dubbo:registry
id
=
"intlRegistry"
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181"
group
=
"intl"
/>