Dubbo分布式服务框架入门实战(附源码)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.****.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
接口实现类:
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"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
下面就需要编写测试类来验证服务提供者是否发布成功,由于使用了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(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
如果服务发布成功,则会在控制台打印出“服务提供者已注册成功!”的提示。
服务消费者:
pom.xml配置文件,在服务提供者的pom.xml基础添加如下如下依赖:
<dependency> <groupId>com.rhwayfun</groupId> <artifactId>provider-demo</artifactId> <version>0.0.1-SNAPSHOT</version></dependency>
- 1
- 2
- 3
- 4
- 5
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
使用服务提供者发布的服务:
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()); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
以上就是使用dubbo的入门实例,我们发现使用dubbo调用远程服务非常方便,感觉调用本地接口没什么很大不同。其实,这就是dubbo的高明之处了,有兴趣可以阅读以下dubbo的源码。
给我老师的人工智能教程打call!http://blog.****.net/jiangjunshow
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();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
接口实现类:
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"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
下面就需要编写测试类来验证服务提供者是否发布成功,由于使用了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(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
如果服务发布成功,则会在控制台打印出“服务提供者已注册成功!”的提示。
服务消费者:
pom.xml配置文件,在服务提供者的pom.xml基础添加如下如下依赖:
<dependency> <groupId>com.rhwayfun</groupId> <artifactId>provider-demo</artifactId> <version>0.0.1-SNAPSHOT</version></dependency>
- 1
- 2
- 3
- 4
- 5
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
使用服务提供者发布的服务:
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()); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
以上就是使用dubbo的入门实例,我们发现使用dubbo调用远程服务非常方便,感觉调用本地接口没什么很大不同。其实,这就是dubbo的高明之处了,有兴趣可以阅读以下dubbo的源码。