项目文件下载地址:http://download.csdn.net/detail/aqsunkai/9552711

概述

    Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

其核心部分包含

  • 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
  • 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  • 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

Dubbo能做什么

透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。

软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。

服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

主要核心部件

Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.

RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能

Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

实例

搭建maven web项目

不会搭建maven项目的可以参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51286373

本例我搭建了两个项目:dubbo-provider和dubbo-customer

修改配置文件

dubbo-provider项目

在pom.xml文件中增加dubbo、zookeeper、zkclient的jar包:

<!-- http://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<!-- <type>pom</type> -->
</dependency>

因为要作为web项目启动,web.xml文件中需要增加:

必须有ContextLoaderListener监听器,applicationContext.xml才会成功加载

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

下面是DemoService和DemoServiceImpl的内容

public interface DemoService {
String getName(String firstName,String lastName);
}
public class DemoServiceImpl implements DemoService{
@Override
public String getName(String firstName, String lastName) {
return "hello, "+firstName+" " +lastName;
}
}
applicationContext.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="demoService" class="com.cn.provider.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://127.0.0.1:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.cn.provider.DemoService"
ref="demoService"/>
</beans>
该项目作为web项目用tomcat启动的话,已经配置完毕,还可以直接用main方法加载配置文件模拟项目启动,需要多一个java类
  Provider中的内容为:
package com.cn.provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
private final static Logger logger = LoggerFactory.getLogger(Provider.class);
public static void main(String[] args) throws Exception {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
logger.info("Provider Context start success");
} catch (Exception e) {
logger.error("Provider Context start error\n"+e.getMessage());
}
synchronized (Provider.class) {
while (true) {
try{
Provider.class.wait();
}catch(InterruptedException e){
logger.error("synchronized error\n"+e.getMessage());
}
}
}
}
}

dubbo-customer项目

因为dubbo-customer需要引入dubbo-provider项目中DemoService的jar包,pom.xml文件内容要加上:

<!-- http://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<!-- <type>pom</type> -->
</dependency>
<dependency>
<groupId>javabuilder</groupId>
<artifactId>javabuilder</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/dubbo-provider.jar</systemPath>
</dependency>

记得把dubbo-provider.jar放到项目WEB-INF/lib下,生成jar包的方法可参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51711580

整个项目我想既可以用main方法启动加载配置文件,也可以作为web项目用tomcat启动,在浏览器中看到结果,那么我一定需要在pom.xml中引入spring的jar包吗,答案是no,我只需要写servlet,直接进入doGet方法即可验证,那么就需要修改web.xml

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>servletDemo</servlet-name>
<servlet-class>com.cn.customer.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
applicationContext.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="customer" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="demoService"
interface="com.cn.provider.DemoService"/> <!-- 目的是用ApplicationContext获取bean,与dubbo项目无关 -->
<bean class="com.cn.customer.AppContext"/>
</beans>

servlet.java文件的内容为:

public class Servlet extends HttpServlet{

     /**
*
*/
private static final long serialVersionUID = 1L;
//初始化
public void init() throws ServletException {
System.out.println("我是init()方法!用来进行初始化工作");
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doGet()方法!用来处理GET请求");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY>");
/*
* 通过Spring提供的工具类获取ApplicationContext对象
*/
//ServletContext sc = this.getServletContext(); //和下面一行一样,都能获取ServletContext
ServletContext sc = request.getSession().getServletContext();
//第一种获取bean方法,获取失败时抛出异常
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
DemoService demoService1 = (DemoService)ac1.getBean("demoService");
String name1 = demoService1.getName("tom", "Edison");
out.println(name1);
out.println("<br>");
//第二种获取bean方法,获取失败时返回null
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);
DemoService demoService2 = (DemoService)ac2.getBean("demoService");
String name2 = demoService2.getName("tom", "Edison");
out.println(name2);
out.println("<br>");
//第三种获取bean方法
WebApplicationContext wac = (WebApplicationContext)sc.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
DemoService demoService3 = (DemoService)wac.getBean("demoService");
String name3 = demoService3.getName("tom", "Edison");
out.println(name3);
out.println("<br>");
//第四种获取bean方法,实现ApplicationContextAware接口
AppContext aContext = new AppContext();
DemoService demoService4 = (DemoService)aContext.getBean("demoService");
String name4 = demoService4.getName("tom", "Edison");
out.println(name4);
out.println("</BODY>");
out.println("</HTML>");
}
//处理POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doPost()方法!用来处理POST请求");
doGet(request, response);
}
//销毁实例
public void destroy() {
super.destroy();
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
}
}
   上面文件中的获取bean的方法:第一二三种都是直接获取,第四种需要写一个实现ApplicationContextAware接口的类,在java类中获取spring的bean的方法可以参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51700645
public class AppContext implements ApplicationContextAware{

    private static ApplicationContext applicationContext;
/**
* 当继承了ApplicationContextAware类之后,那么程序在调用
* getBean(String)的时候会自动调用该方法,不用自己操作
*/
@Override
public void setApplicationContext(
org.springframework.context.ApplicationContext applicationContext)
throws BeansException {
this.applicationContext= applicationContext;
} public Object getBean(String beanName){
return this.applicationContext.getBean(beanName);
}
}
提醒一句,别忘了导入dubbo-provider中的DemoService接口的jar包,该项目作为web项目用tomcat启动的话,已经配置完毕,还可以直接用main方法加载配置文件模拟项目启动,需要多一个java类
   Customer.java内容为:
public class Customer{
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String name = demoService.getName("tom", "Edison");
System.out.println(name);
System.in.read();
}
}

启动项目

   1 启动zookeeper注册中心,可参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51683632
   2 启动项目,dubbo-provider和dubbo-customer项目都分别支持main方法和tomcat启动,两两组合启动即可。如果dubbo-customer项目用tomcat启动的话,在浏览器url输入http://localhost:8088/dubbo-customer/index即可看到结果

dubbo本地搭建实例的更多相关文章

  1. DUBBO本地搭建及小案例

    DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...

  2. DUBBO本地搭建及小案例 (转)

    DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...

  3. java 学习笔记(三)ZooKeeper集群搭建实例,以及集成dubbo时的配置 (转)

    ZooKeeper集群搭建实例,以及集成dubbo时的配置 zookeeper是什么: Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式 ...

  4. 【2020-03-21】Dubbo本地环境搭建-实现服务注册和消费

    前言 本周主题:加班工作.本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度.今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理.之所以说是重拾,是 ...

  5. 超快速使用docker在本地搭建hadoop分布式集群

    超快速使用docker在本地搭建hadoop分布式集群 超快速使用docker在本地搭建hadoop分布式集群 学习hadoop集群环境搭建是hadoop入门的必经之路.搭建分布式集群通常有两个办法: ...

  6. Spring boot dubbo+zookeeper 搭建------基于gradle项目的消费端与服务端分离实战

    1. Dubbo简介 Dubbo是Alibaba开源的分布式框架,是RPC模式的一种成熟的框架,优点是可以与Spring无缝集成,应用到我们的后台程序中.具体介绍可以查看Dubbo官网. 2. Why ...

  7. Dubbo本地存根是什么,Dubbo本地伪装又是什么?

    真正的大师永远怀着一颗学徒的心 哈喽!大家好,我是小奇,一位程序员界的学徒 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 前言 书接上回,昨天打了 ...

  8. Hibernate框架搭建实例

    一,Hibernate是一个持久层,是一个专门负责管理数据库连接的框架: 二,Hibernate的搭建实例: 1.在Hibernate的官方网站(http://www.hibernate.org)可以 ...

  9. nodejs,node原生服务器搭建实例

    nodejs,node原生服务器搭建实例

随机推荐

  1. mouse事件在JQ中的应用(在动画与交互中用得比较多).

    mousedown与mouseup事件 用户交互操作中,最简单直接的操作就是点击操作,因此jQuery提供了一个mousedown的快捷方法可以监听用户鼠标按下的操作,与其对应的还有一个方法mouse ...

  2. leetcode:栈

    1. evaluate-reverse-polish-notation Evaluate the value of an arithmetic expression in Reverse Polish ...

  3. Arduino-定义串口

    在一个老外写的代码中找到了一个非常好的定义串口的方法!   Arduino用下面这种方法定义串口可以方便的把协议应用的任意的端口,大大提高了代码的修改性和移植性.       以下是范例:       ...

  4. 2019.03.13 ZJOI2019模拟赛 解题报告

    得分: \(55+12+10=77\)(\(T1\)误认为有可二分性,\(T2\)不小心把\(n\)开了\(char\),\(T3\)直接\(puts("0")\)水\(10\)分 ...

  5. Gym 100090D Insomnia

    从 n 变到 1,有多少种方案? 打表记忆化. #include <bits/stdc++.h> using namespace std; int n; ]; int dfs(int n) ...

  6. vue中的js动画与Velocity.js结合

    vue里面除了用css写动画,还可以用js写动画,vue的transition中,定义了几个动画钩子 第一个动画钩子:@before-enter <div id='app'> <tr ...

  7. CKEditor4x word导入不保存格式的解决方案

    后台上传文档时,目前功能都通过word直接复制黏贴实现,之前和word控件朋友一起测试找个问题,原始代码CK4.X没有找个问题. 第一时间排查config.js的配置发现端倪,测试解决! 由于配合ck ...

  8. 解决调用Office组件的问题

    在修改一个之前工作的好好的工具的时候出了如下错误: 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel ...

  9. MapReduce计算每年最大值测试样例生成程序

    Demo.java package com.java; import java.io.BufferedWriter; import java.io.File; import java.io.FileW ...

  10. Thymeleaf模板引擎绕过浏览器缓存加载静态资源js,css文件

    浏览器会缓存相同文件名的css样式表或者javascript文件.这给我们调试带来了障碍,好多时候修改的代码不能在浏览器正确显示. 静态常见的加载代码如下: <link rel="st ...