第一个dubbo程序
Dubbo是一个高性能服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,使得应用可通过高性能RPC实现服务的输出和输入功能,和Spring框架可以无缝集成。
作为一个分布式服务框架,以及SOA治理方案,Dubbo其功能主要包括:高性能NIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与服务降级等。Dubbo最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
Dubbo包含远程通讯、集群容错和自动发现三个核心部分。提供透明化的远程方法调用,实现像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。同时具备软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。可以实现服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
下图来自Dubbo官网,描述了服务注册中心、服务提供方、服务消费方、服务监控中心之间的调用关系,具体如下图所示:

今天的第一个dubbo程序主要就是实现Consumer对Provider的调用
一、Provider(服务提供方)步骤
1.pom.xml如下
<!-- 这里是配置的编译JDK版本和编码方式 -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<!-- spring引入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<!-- zookeeper核心包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
<!-- dubbo核心包引入 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.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-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <context:component-scan base-package="cn.duanjt" /> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="storeServer" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="21880" /> <!--和本地bean一样实现服务 -->
<bean id="vipUserService" class="cn.duanjt.service.impl.VipUserService" /> <dubbo:service interface="cn.duanjt.service.UserService" ref="vipUserService" protocol="dubbo" />
</beans>
说明:
- 我们需要在包cn.duanjt(可以是子包下面)下面创建一个接口UserService
- 我们需要在包cn.duanjt(可以是子包下面)下面创建一个接口UserService的实现类VipUserService
- 如上的xml配置就是根据接口和实现类暴露接口给zookeeper
- protocol表示协议方式,可以是dubbo、rmi等。因为dubbo是支持多协议的
3.主方法代码如下:
public class SpringServer {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("classpath:dubbo.xml");
ac.start();
System.out.println("服务端启动完成");
System.in.read();
}
}
二、Consumer(服务消费方)步骤
其实消费方也是可以使用jar包的方式调用,但是为了实现不同的方式,消费方我们使用spring-mvc的方式进行实现。
1.首先搭建一个普通的spring-mvc程序,不会的可以参考spring-mvc
2.pom.xml如下:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<!-- servlet的引用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- spring引用 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- zkclient引用 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<!-- zookeeper核心包的引用 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
<!-- dubbo核心包的引用 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
3.在springmvc的配置文件(spring-mvc.xml)中引入dubbo配置

4.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: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"> <!-- 就是注册到zookeeper的名字 -->
<dubbo:application name="store_client"></dubbo:application>
<!-- zookeeper地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <dubbo:consumer check="false" />
<!-- 从zookeeper引用 -->
<dubbo:reference id="userService" interface="cn.duanjt.service.UserService"></dubbo:reference>
</beans>
说明:
- 需要创建一个接口UserService。该接口和服务方的接口完全一致。
5.controller的代码如下:
@Controller
public class UserController {
@Autowired
UserService service; @RequestMapping("/index")
public String index(HttpServletRequest request, HttpServletResponse response) {
String res = service.getDetail("china");//远程rpc调用
request.setAttribute("detail", res);//将结果设置到detail中
return "index";
}
}
6.前端jsp页面(index.jsp)如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>首页</title>
</head>
<body>
你好,接收到的数据是: ${detail }
</body>
</html>
前端请求的地址是:http://localhost:8080/index
最后附一张图:

第一个dubbo程序的更多相关文章
- 一个JAVA程序员成长之路分享
我搞JAVA也有些日子了, 因为我比较贪玩,上进心不那么强, 总是逼不得已为了高薪跳槽才去学习, 所以也没混成什么大牛, 但好在现在也已经成家立业, 小日子过的还算滋润, 起码顶得住一月近万元的吃喝拉 ...
- 一个老牌程序员说:做Java开发,怎么可以不会这 20 种类库和 API
- DirectX游戏编程(一):创建一个Direct3D程序
一.环境 Visual Studio 2012,DirectX SDK (June 2010) 二.准备 1.环境变量(如没有配置请添加) 变量名:DXSDK_DIR 变量值:D:\Software\ ...
- 从头开始搭建一个dubbo+zookeeper平台
本篇主要是来分享从头开始搭建一个dubbo+zookeeper平台的过程,其中会简要介绍下dubbo服务的作用. 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后 ...
- 第一个python程序
一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...
- 编写第一个MapReduce程序—— 统计气温
摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...
- 1.3 第一个C#程序
几乎没一门编程语言的第一个程序都叫“你好,世界”,所以先在visual studio 中创建一个Helloworld程序. 各部分的详细内容: Main方法是程序运行的起点,最重要的代码就写在Main ...
- 一个.net程序员的安卓之旅-Eclipse设置代码智能提示功能
一个.net程序员的安卓之旅-代码智能提示功能 过完年回来就决心开始学安卓开发,就网上买了个内存条加在笔记本上(因为笔记本原来2G内存太卡了,装了vs2010.SQL Server 2008.orac ...
- MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(02)
1.3.1 应用程序对象 MFC应用程序的核心就是基于CWinApp类的应用程序对象,CWinApp提供了消息循环来检索消息并将消息调度给应用程序的窗口.当包含头文件<afxwin.h>, ...
随机推荐
- dell win 10笔记本关闭多媒体键,启用功能键的快捷方式
自从使用win 10之后,在使用快捷键方面就没有win 7之前来的顺手,比如F8切换投影仪,F5/F6调试等等.特地搜了下,使用Fn+Esc可以在功能键和多媒体键之间切换.
- shell if 语句
一.过程式 编程语言的代码执行顺序: a.顺序执行:逐条执行: b.选择执行: 代码有一个分支:条件满足时才会执行: 俩个或以上的分支:只会执行其中一个满足条件的分支: c.循环执行: 代码片段(循环 ...
- JS实现复制页面文字弹出消息提醒
先上效果图: 简洁版: <script type="text/javascript"> document.body.oncopy=function(){ alert(& ...
- Base64编码为什么会使数据量变大?
当把byte[]通过Convert.ToBase64String转换成Base64编码字符串时数据量明显变大,为何呢?这里就得先探究一下什么是Base64编码. Base64编码的思想是是采用64个基 ...
- Python3 tkinter基础 Button command 单击按钮 在console中打印文本
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux内核中的最简单的输入输出调度算法noop
1. noop是什么? noop是一种输入输出调度算法 2. noop的别称 又称为电梯调度算法 3. noop原理是怎样的? 将输入输出请求放到一个FIFO队列中,然后按次序执行队列中的输入输出请求 ...
- 全排列+字符串查找|扑克排序|2014年蓝桥杯A组题解析第六题-fishers
标题:扑克序列 A A 2 2 3 3 4 4, 一共4对扑克牌.请你把它们排成一行. 要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌. 请填写出所有符合要求的排 ...
- 【做题】uoj#370滑稽树上滑稽果——巧妙dp
一个显然的结论是最终树的形态必然是一条链.具体证明只要考虑选定树上的某一条链,然后把其他部分全部接在它后面,这样答案一定不会变劣. 那么,一开始的想法是考虑每一位的最后出现位置,但这并不容易实现.注意 ...
- spring配置redis
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 3、iptables扩展及使用
iptables/netfilter netfilter: kernel framework,位于内核中的协议框架 iptables 是规则管理命令行工具 四表:filter, nat, mangl ...