1.首先我们要知道Dubbo是什么?
Dubbo是阿里巴巴开源的基于 Java 的高性能 RPC 分布式服务框架,现已成为 Apache 基金会孵化项目。(简单的说:是个远程服务调用的分布式框架)
2.dubbo的架构
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
0:服务容器负责启动,加载,运行服务提供者。
1: 服务提供者在启动时,向注册中心注册自己提供的服务。
2:服务消费者在启动时,向注册中心订阅自己所需的服务。
3: 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4: 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5: 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
3.dubbo使用方法
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
服务提供者:
- 下载zookeeper注册中心,下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/
(我下的是zookeeper-3.4.10)解压后进入conf文件夹下,修改zoo_simple.cfg为zoo.cfg
修改完后,回到根目录进入到bin文件夹下面双击zkServer.cmd启动注册中心服务。
有人会疑问为什么要改zoo.cfg呢?不改双击zkServer.cmd,Dos是会闪退的。
定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
下面是个案例:
首先加入jar
<!-- 添加 dubbo 依赖包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- 添加 zookeeper 依赖包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
<!-- 添加 zkclient 依赖包 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
定义一个接口
package dubbotest.provider;
import java.util.List;
public interface DemoService {
String sayHello(String name);
public List getUsers();
}
在服务提供方实现接口:(对服务消费方隐藏实现)
package dubbotest.provider;
import dubbotest.provider.model.User;
import java.util.ArrayList;
import java.util.List;
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello,"+name;
}
@Override
public List getUsers() {
List list=new ArrayList();
User user1=new User();
user1.setName("xiexie");
user1.setAge(23);
user1.setSex("女");
User user2=new User();
user2.setName("谢谢");
user2.setAge(20);
user2.setSex("女");
User user3=new User();
user3.setName("bigboss");
user3.setAge(20);
user3.setSex("男");
list.add(user1);
list.add(user2);
list.add(user3);
return list;
}
}
用Spring配置声明暴露服务(dubbo-provider.xml):
<?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="dubbotest.provider.DemoServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="xs_provider" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper注册中心暴露服务地址,即zookeeper的所在服务器ip地址和端口号 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="dubbotest.provider.DemoService" ref="demoService" />
</beans>
加载Spring配置,启动服务:
package dubbotest.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
context.start();
System.in.read();// 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
服务消费者:
通过Spring配置引用远程服务【消费者】(dubbo-consumer.xml):
<?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="hehe_consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="dubbotest.provider.DemoService" />
</beans>
加载Spring配置,并调用远程服务【消费者】:
package dubbotest.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Consumer {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService");
String hello = demoService.sayHello("旧时光");
System.out.println("----------------------"+hello);
List list = demoService.getUsers();
if(list!=null&&list.size()>0){
for (int i=0;i<list.size();i++){
System.out.println("*****************"+list.get(i));
}
}
System.in.read();
}
}
这样相当于服务提供者搭建建好了,记得打开zookeeper的服务,不然会报错的。
先启动服务器再远程调用服务。结果如下: