Dubbo分布式服务框架入门实战(附源码)
2016-08-05 17:49
477 查看
Dubbo分布式服务框架入门实战
首先,有必要清楚Dubbo是什么。官方文档的定义如下:
DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
其实,总结起来就是:
Dubbo是一个解决大规模服务治理的高性能分布式服务框架。
dubbo的架构如下:
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Dubbo入门实战
要完成一个dubbo的demo,其实只需要上面的Provider、Consumer和Registry。其中Registry使用Zookeeper来承担,当提供者提供服务后,需要向Zookeeper(注册中心)暴露其发布的服务,消费者通过Zookeeper订阅其需要消费的服务,然后就可以像调用本地服务一样调用远程服务了。
服务提供者:
接口定义:
接口实现类:
Spring配置文件:
pom.xml文件:
下面就需要编写测试类来验证服务提供者是否发布成功,由于使用了Zookeeper作为服务注册中心,所以在运行测试代码之前,需要首先启动Zookeeper。具体Zookeeper的使用与配置请参考Zookeeper入门实战。
测试代码:
如果服务发布成功,则会在控制台打印出“服务提供者已注册成功!”的提示。
服务消费者:
pom.xml配置文件,在服务提供者的pom.xml基础添加如下如下依赖:
Spring配置文件:
使用服务提供者发布的服务:
以上就是使用dubbo的入门实例,我们发现使用dubbo调用远程服务非常方便,感觉调用本地接口没什么很大不同。其实,这就是dubbo的高明之处了,有兴趣可以阅读以下dubbo的源码。
附:dubbo入门实战源码
首先,有必要清楚Dubbo是什么。官方文档的定义如下:
DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
其实,总结起来就是:
Dubbo是一个解决大规模服务治理的高性能分布式服务框架。
dubbo的架构如下:
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Dubbo入门实战
要完成一个dubbo的demo,其实只需要上面的Provider、Consumer和Registry。其中Registry使用Zookeeper来承担,当提供者提供服务后,需要向Zookeeper(注册中心)暴露其发布的服务,消费者通过Zookeeper订阅其需要消费的服务,然后就可以像调用本地服务一样调用远程服务了。
服务提供者:
接口定义:
package com.rhwayfun.service; /** * * @ClassName: HelloService * @Description: TODO * @author ZhongCB * @date 2016年8月1日 下午4:40:09 * */ public interface HelloService { String getName(); }
接口实现类:
package com.rhwayfun.service.impl; import com.rhwayfun.service.HelloService; /** * * @ClassName: HelloServiceImpl * @Description: TODO * @author ZhongCB * @date 2016年8月5日 下午5:12:00 * */ public class HelloServiceImpl implements HelloService { public String getName() { return "rhwayfun"; } }
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="helloService" class="com.rhwayfun.service.impl.HelloServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="provider-dubbo" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="29014" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.rhwayfun.service.HelloService" ref="helloService" /> </beans>
pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rhwayfun</groupId> <artifactId>provider-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- spring相关 --> <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.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
下面就需要编写测试类来验证服务提供者是否发布成功,由于使用了Zookeeper作为服务注册中心,所以在运行测试代码之前,需要首先启动Zookeeper。具体Zookeeper的使用与配置请参考Zookeeper入门实战。
测试代码:
package com.rhwayfun.test; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @ClassName: HelloServiceTest * @Description: TODO * @author ZhongCB * @date 2016年8月5日 下午5:17:52 * */ public class HelloServiceTest { public static void main(String[] args) throws IOException{ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"application.xml"}); ctx.start(); System.out.println("服务提供者已注册成功!"); System.in.read(); } }
如果服务发布成功,则会在控制台打印出“服务提供者已注册成功!”的提示。
服务消费者:
pom.xml配置文件,在服务提供者的pom.xml基础添加如下如下依赖:
<dependency> <groupId>com.rhwayfun</groupId> <artifactId>provider-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
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="consumer-dubbo" /> <!-- 使用zookeeper注册中心订阅服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用HelloService --> <dubbo:reference id="helloService" interface="com.rhwayfun.service.HelloService" /> </beans>
使用服务提供者发布的服务:
package com.rhwayfun.consumer.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.rhwayfun.service.HelloService; /** * * @ClassName: DubboConsumerDemo * @Description: TODO * @author ZhongCB * @date 2016年8月5日 下午4:14:58 * */ public class DubboConsumerDemo { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:application.xml"}); ctx.start(); // 获取远程服务代理 HelloService helloservice = (HelloService)ctx.getBean("helloService"); System.out.println(helloservice.getName()); } }
以上就是使用dubbo的入门实例,我们发现使用dubbo调用远程服务非常方便,感觉调用本地接口没什么很大不同。其实,这就是dubbo的高明之处了,有兴趣可以阅读以下dubbo的源码。
附:dubbo入门实战源码
相关文章推荐
- 学习Spark的入门教程——《Spark核心源码分析与开发实战》
- Google Protocol Buffer序列化入门实战(附源码)
- 云星数据---Scala实战系列(精品版)】:Scala入门教程018-Scala实战源码-Scala内部类
- 云星数据---Scala实战系列(精品版)】:Scala入门教程023-Scala实战源码-Scala 多态02
- 云星数据---Scala实战系列(精品版)】:Scala入门教程022-Scala实战源码-Scala 多态01
- 云星数据---Scala实战系列(精品版)】:Scala入门教程028-Scala实战源码-Scala 的特质 (接口)04
- 云星数据---Scala实战系列(精品版)】:Scala入门教程016-Scala实战源码-Scala 判断语句 、循环与java的比较
- 云星数据---Scala实战系列(精品版)】:Scala入门教程015-Scala实战源码-Scala函数
- 云星数据---Scala实战系列(精品版)】:Scala入门教程025-Scala实战源码-Scala 的特质 (接口)01
- Ehcache缓存入门实战(附源码)
- android从入门到精通-项目实战(心得及源码)
- Spark 2.0从入门到精通:Scala编程、大数据开发、上百个实战案例、内核源码深度剖析(278讲全)
- 云星数据---Scala实战系列(精品版)】:Scala入门教程017-Scala实战源码-Scala 创建对象 以及类的介绍
- 云星数据---Scala实战系列(精品版)】:Scala入门教程012-Scala实战源码-Scala操作符
- Redis入门实战(附源码)
- 小兔博客新增源码下载模块,JavaWeb项目实战,JavaScript入门教程 ,JavaSE案例等
- 云星数据---Scala实战系列(精品版)】:Scala入门教程010-Scala实战源码-object(类)
- 云星数据---Scala实战系列(精品版)】:Scala入门教程020-Scala实战源码-Scala 继承关系
- 云星数据---Scala实战系列(精品版)】:Scala入门教程027-Scala实战源码-Scala 的特质 (接口)03