Dubbo入门基础与实例讲解(转)
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
一、Dubbo简介
1.1、Dubbo是什么?
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架
其核心部分包含:
1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
1.2. Dubbo能做什么?
1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
1.3. dubbo的架构
dubbo架构图如下所示:
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
0 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
1.4. dubbo使用方法
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐)
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
下面以一个实例来做说明,看这个实例前,建议先看看这里 Dubbo-Admin管理平台和Zookeeper注册中心的搭建
二、服务提供者
提供者的目录如下:工程下载地址:http://download.csdn.net/detail/evankaka/9054253
其中初始化工程的创建如下:
首先,选择创建
然后,next,记得红框打上,这里直接快速
最后,输入项目名等
好了,项目有了,接下来就是加代码了
1、service代码
接口层如下:ProviderService.java
- package com.lin.service;
- /**
- * 功能概要:提供者service接口层
- *
- * @author linbingwen
- * @since 2015年8月26日
- */
- public interface ProviderService {
- public String sayHello(String name);
- }
实现层如下:ProviderServiceImpl.java
- package com.lin.service;
- /**
- * 功能概要:功能概要:提供者service实现层
- *
- * @author linbingwen
- * @since 2015年8月26日
- */
- public class ProviderServiceImpl implements ProviderService {
- public String sayHello(String name) {
- return "Hello:~~~~~~~~~~~~~~~~~~~~~~~~"+name+"你好,你好~~";
- }
- }
2、Spring配置文件-applicationProvider.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="providerService" class="com.lin.service.ProviderServiceImpl" />
- <!-- 提供方应用信息,用于计算依赖关系 -->
- <dubbo:application name="dubbo_provider" />
- <!-- 使用multicast广播注册中心暴露服务地址
- <dubbo:registry address="multicast://localhost:1234" />-->
- <!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="zookeeper://127.0.0.1:2181" />
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="29014" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.lin.service.ProviderService" ref="providerService" />
- </beans>
3、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.lin</groupId>
- <artifactId>dubbo_provider</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>
4、启动提供者服务
在src/test/java下添加
ProviderServiceTest.java内容如下:
- package com.lin.service;
- import java.io.IOException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * 功能概要:
- *
- * @author linbingwen
- * @since 2015年8月26日
- */
- public class ProviderServiceTest {
- /**
- * @author linbingwen
- * @since 2015年8月26日
- * @param args
- */
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[]{"applicationProvider.xml"});
- context.start();
- System.out.println("提供者服务已注册成功");
- System.out.println("请按任意键取消提供者服务");
- try {
- System.in.read();//让此程序一直跑,表示一直提供服务
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
运行结果:
因为我这里使用的是本地的zookeeper注册中心,所以一定要先打开它!!!一定要先打开它!!!一定要先打开它!!!打开D:\Java\Tool\zookeeper-3.4.6\bin下的zkServer.cmd,然后一直开着,不要关了!!!!!!!!!!!!!然后一直开着,不要关了!!!!!!!!!!!!!
接着运行上面的main方法,输出如下:
然后我们可以到注册中心去看看服务注册上去了没有。(注意,这里的zookeper注册中心在我本地电脑已搭建好,搭建过程看这里Dubbo-Admin管理平台和Zookeeper注册中心的搭建)
可以看到,提供者已注册成功。
三、消费者
1、还是一个maven项目,整个结构如下,工程下载地址:http://download.csdn.net/detail/evankaka/9054253
2、Spring配置文件applicationConsumer.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="dubbo_consumer" />
- <!-- 使用multicast广播注册中心暴露发现服务地址 -->
- <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
- <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="providerService" interface="com.lin.service.ProviderService" />
- </beans>
3、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.lin</groupId>
- <artifactId>dubbo_consumer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <properties>
- <spring.version>3.2.8.RELEASE</spring.version>
- </properties>
- <dependencies>
- <!-- 添加provider的jar包 -->
- <dependency>
- <groupId>com.lin</groupId>
- <artifactId>dubbo_provider</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- 添加dubbo依赖 -->
- <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>
- <!-- 添加zk客户端依赖 -->
- <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>
和提供者的POM文件基本上是一样的,只不过加了对提供者的依赖。
4、消费者调用提供者
在src/test/java写一个ConsumerServiceTest.java
- package com.lin.service;
- import java.io.IOException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * 功能概要:
- *
- * @author linbingwen
- * @since 2015年8月26日
- */
- public class ConsumerServiceTest {
- /**
- * @author linbingwen
- * @since 2015年8月26日
- * @param args
- */
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] { "applicationConsumer.xml" });
- context.start();
- ProviderService providerService = (ProviderService) context.getBean("providerService");
- System.out.println(providerService.sayHello("林炳文Evankaka"));
- System.out.println("Press any key to exit.");
- try {
- System.in.read();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
然后启动:
再打开注册中心,发面有消费者在使用提供者,因为都是一个电脑,所以IP都一样。当然。其它电脑也可以访问这个提供者的!
Dubbo入门基础与实例讲解(转)的更多相关文章
- Dubbo学习总结(1)——Dubbo入门基础与实例讲解
Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo是一个分布式服务框架,致力于 ...
- [转帖]linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)
linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习) https://www.cnblogs.com/caozy/p/9261224.html 总结的挺好的 ...
- Java入门系列:实例讲解ArrayList用法
本文通过实例讲解Java中如何使用ArrayList类. Java.util.ArrayList类是一个动态数组类型,也就是说,ArrayList对象既有数组的特征,也有链表的特征.可以随时从链表中添 ...
- Vue.js 基础指令实例讲解(各种数据绑定、表单渲染大总结)——新手入门、高手进阶
Vue.js 是一套构建用户界面的渐进式框架.他自身不是一个全能框架--只聚焦于视图层.因此它非常容易学习,非常容易与其它库或已有项目整合.在与相关工具和支持库一起使用时,Vue.js 也能完美地驱动 ...
- Dubbo入门之一:实例1
原文地址:http://blog.csdn.net/ruishenh/article/details/23180707?utm_source=tuicool 1. 概述 Dubbo是一个分布式服务 ...
- Express入门介绍vs实例讲解
下午在团队内部分享了express相关介绍,以及基于express的实例.内容提纲如下. 什么是Express 为什么要用Express 路由规则 一切皆中间件 实例:Combo Applicatio ...
- [转]Scrapy简单入门及实例讲解
Scrapy简单入门及实例讲解 中文文档: http://scrapy-chs.readthedocs.io/zh_CN/0.24/ Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用 ...
- TCP入门与实例讲解
内容简介 TCP是TCP/IP协议栈的核心组成之一,对开发者来说,学习.掌握TCP非常重要. 本文主要内容包括:什么是TCP,为什么要学习TCP,TCP协议格式,通过实例讲解TCP的生命周期(建立连接 ...
- 【智能算法】粒子群算法(Particle Swarm Optimization)超详细解析+入门代码实例讲解
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 算法起源 粒子群优化算法(PSO)是一种进化计算技术(evolutionary computation),1995 年由E ...
随机推荐
- java多线程12设计模式
1.Single Threaded Execution Pattern(单线程运行模式) 2.Immutable Pattern(一成不变的模式) 3.Guarded Suspension Patte ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- hdu 5071 Chat(模拟)
题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...
- crm操作安全字段
using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; /// <summary> ...
- BlueJ的code pad
Java的REPL BlueJ的code pad实用吗?Java对(Read-Eval-Print Loop)不提供原生支持.这样的"交互式解释器"或"交互式编程环境&q ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- SWT的GridLayout一些参数解释
1. GridLayout类的说明GridLayout在包org.eclipse.swt.layout中,各参数意义如下:1. numColumns指定布局器中的列数2. horizontalSpac ...
- 浅析JAVA设计模式之工厂模式(一)
1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...
- sharepoint 2013 userprofile 用户信息
Sharepoint2013获得当前用户userfrofile 基本介绍: 什么使用户配置文件. 用户属性和用户配置文件属性提供有关 SharePoint 用户的信息,如显示名称.电子邮件.标题以及其 ...
- ArcGIS For Flex报错
1.错误描写叙述 2.错误原因 3.解决的方法