一、dubbo简介
dubbo是阿里巴巴于2011年开源的分布式RPC服务框架,采用java编写。
二、dubbo角色
dubbo框架从角色分,有以下几种:
1、provider,服务的提供方;
2、consumer,服务的消费方;
3、registey,服务注册与订阅的注册中心;
4、monitor,统计服务调用次数和调用时间的监控中心;
5、container,服务运行的容器;
三、dubbo配置
dubbo配置主要有几下几项。
1、配置应用信息
<dubbo:application />,配置当前应用信息,名称为必填,如:
<dubbo:application name="dubboProvider"/>
2、向注册中用注册
这里使用zookeeper当注册中心,包括协议、地址(包含端口)、超时时间等,如:
3、配置dubbo协议<dubbo:registry id="dubboStudyRegistry" protocol="zookeeper" address="127.0.0.1:2181" timeout="5000"/>
这里使用dubbo作为协议,包括协议名和服务端口,如:
<dubbo:protocol name="dubbo" port="20880" />
4、定义服务
提供service服务,包括服务接口、接口实现引用、版本、超时时间、负载策略等,如
5、消费引用配置<dubbo:service interface="com.dragon.study.dubboService.service.HelloService" ref="helloService" version="1.0.0" timeout="3000" retries="0" loadbalance="random" registry="dubboStudyRegistry"/>
使用dubbo提供的服务,包括引用id、服务接口、注册中心、超时时间、是否检测提供者存在等,如
<dubbo:reference id="helloService" interface="com.dragon.study.dubboService.service.HelloService" registry="dubboStudyRegistry" version="1.0.0" timeout="3000" check="true"/>
四、dubbo服务在zookeeper存储格式
1、/dubbo
这是dubbo在zookeeper上的根节点
2、/dubbo/com.dragon.study.dubboService.service.HelloService
这是服务节点,代表一个服务,这里即是自定义的HelloService服务
3、/dubbo/com.dragon.study.dubboService.service.HelloService/providers
服务提供者的节点,子节点代码服务真正的提供者
4、/dubbo/com.dragon.study.dubboService.service.HelloService/consumers
服务消费者的节点,子节点代码服务真正的消费者
五、服务端开发流程及代码实例1、添加maven依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency>2、服务实现
public interface HelloService { String hello(String name); }
@Service("helloService") public class HelloServiceImpl implements HelloService { @Override public String hello(String name) { return "hello "+name; } }3、服务实现的dubbo配置(文件spring-dubbo.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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <context:component-scan base-package="com.dragon.study.dubboServiceImpl.serviceImpl"/> <!--定义服务应用名称--> <dubbo:application name="dubboProvider"/> <!--向注册中心注册(这里使用zookeeper当注册中心)--> <dubbo:registry id="dubboStudyRegistry" protocol="zookeeper" address="127.0.0.1:2181" timeout="5000"/> <!--定义协议及端口--> <dubbo:protocol name="dubbo" port="20880" /> <!--定义服务--> <dubbo:service interface="com.dragon.study.dubboService.service.HelloService" ref="helloService" version="1.0.0" timeout="3000" retries="0" loadbalance="random" registry="dubboStudyRegistry"/> </beans>4、启动zookeeper服务(具体使用请参考本博客关于zookeeper的内容)
5、启动dubbo服务(provider)
public class DubboProviderMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-dubbo.xml"); ac.start(); System.out.println("dubbo start success"); System.in.read(); } }六、服务调用端开发流程及代码实例
1、添加maven依赖,特别是需要引进服务端的服务接口包(即是服务端自定义的服务接口)
<dependency> <groupId>com.dragon.study</groupId> <artifactId>dubboService</artifactId> <version>1.0-SNAPSHOT</version> </dependency>2、调用服务的配置(文件spring-dubbo-consumer.xml)
3、调用服务实例<?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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!--定义服务应用名称--> <dubbo:application name="dubboConsumer"/> <!--向注册中心注册(这里使用zookeeper当注册中心)--> <dubbo:registry id="dubboStudyRegistry" protocol="zookeeper" address="127.0.0.1:2181" timeout="5000"/> <!--定义调用服务--> <dubbo:reference id="helloService" interface="com.dragon.study.dubboService.service.HelloService" registry="dubboStudyRegistry" version="1.0.0" timeout="3000" check="true"/> </beans>
public class DubboConsumerMain { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml"); HelloService helloService = (HelloService) ac.getBean("helloService"); String result = helloService.hello("apple"); System.out.println(result); } }输出:
hello apple