1、工做原理图html
这是dubbo官网的关于dubbo工做原理,其中最核心的应该是Registry注册中心,Monitor,Consumer消费者和Provider服务提供者四个部分,注册中心关系这消费者和提供者的在zookeeper上的注册状态,Monitor控制着消费者,提供者启用和禁用。java
1、服务提供者git
dubbo的git源码有提供一个provider案例工程,dubbo-demo-provider。上面有一段spring配置文件,dubbo-demo-provider.xml,其内容以下:spring
<?xml version="1.0" encoding="UTF-8"?> <!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="demo-provider"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> --> <dubbo:registry address="zookeeper://192.168.43.33:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 和本地bean同样实现服务 --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> <!-- 声明须要暴露的服务接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> </beans>
其中,1)dubbo:application标签做用是给提供者备注一个名称,这个名称一旦被取了,其余提供者就不能再用这个名称。express
2)dubbo:registry标签的做用,就是讲应用注册到注册中心,原例子是在用多广播方式进行注册,能够改成注册到咱们安装的zookeeper上,个人zookeeper服务地址是192.168.43.33,端口默认2181。apache
3)dubbo:protocol标签是标注这个本次服务的协议,暴露的端口,如今是采用dubbo协议,20880端口。api
4)dubbo:service标签是声明提供者服务用的。app
具体实现服务代码比较简单,是一个helloworld程序,再也不作介绍。less
2、服务消费者eclipse
dubbo的git源码有提供一个consumer案例工程,dubbo-demo-consumer。上面有一段spring配置文件,dubbo-demo-consumer.xml,其内容以下:
<?xml version="1.0" encoding="UTF-8"?> <!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 --> <dubbo:application name="demo-consumer"/> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> --> <dubbo:registry address="zookeeper://192.168.43.33:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <!-- 生成远程服务代理,能够和本地bean同样使用demoService --> <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/> </beans>
上面的配置文件跟provider类似,比较特别就是他是消费者,没有服务提供一说,而是用dubbo:reference来标明本身要调用提供者的那个服务,响应名称又跟提供者对应。
3、运行结果
程序的调用顺序是要先把provider启动起来,而后运行consumer工程的,会发现eclipse的console中有以下日志:
说明服务已经成功调用
4、注意环节
消费者要跑起来,还要在该工程的pom.xml中引入一个
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.parent.version}</version> </dependency>
这段引用是对应dubbo开源项目中的dubbo-demo-api这个工程,主要是实现一个与provide对应服务相同的一个接口,给消费者调用,否则程序会报错,当你在eclipse用鼠标,点击
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
中的interface的内容,就会跳到相应的代码,相应代码以下:
package com.alibaba.dubbo.demo; public interface DemoService { String sayHello(String name); }