项目文件下载地址: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即可看到结果

最新文章

  1. 虚拟机安装ubuntu问题解决办法
  2. 【OpenJudge 1665】完美覆盖
  3. TCP\IP三次握手连接,四次握手断开分析
  4. Linux环境下使用gcc编译,gdb反汇编C语言程序
  5. Exploit利用学习1:MS09-001
  6. 设计模式之美:Facade(外观)
  7. jquery中datagrid中getSelected和getSelections的应用
  8. Composer PHP 依赖管理工具
  9. Cookie API
  10. IKVM - 0.42.0.3 .NET平台上的Java实现
  11. COM简单应用示例
  12. http://www.lanceyan.com/tech/mongodb/mongodb_repset1.html
  13. 循环-10. 求序列前N项和(15)
  14. Java知IO
  15. HTML常用基础标签
  16. A1112. Stucked Keyboard
  17. C# DllImport 相对路径无法找到dll
  18. C#中数据库事务、存储过程基本用法
  19. (转)Go和HTTPS
  20. HashMap中的hash函数

热门文章

  1. Python基础学习之集合
  2. 【CSS】等高布局
  3. Maven报错:Missing artifact jdk.tools:jdk.tools:jar:1.6
  4. STM32-开发环境搭建-STM32CubeMX-安装及配置
  5. poj 3485 区间选点
  6. poj 2057 树形DP,数学期望
  7. 砍树,POJ(2665)
  8. CentOS安装配置MongoDB
  9. 推荐优秀的开源GIS软件
  10. 在 publicId 和 systemId 之间需要有空格。