原地址:http://www.cnblogs.com/chanshuyi/p/deep_insight_dubbo_config.html

一、介绍

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

  根据 DUBBO 官方文档,配置 DUBBO 有 4 种方式,分别是:

  • XML 配置文件方式
  • properties 配置文件方式
  • annotation 配置方式
  • API 配置方式

二、简单项目实例(没有实际创建运行)

1、创建 DubboDemo 项目,并创建 interface 模块、provider 模块、consumer 模块,它们都是 DubboDemo 的子模块。其中 interface 模块存放所有的接口、provider 模块提供服务、consumer 消费服务。创建完成后的项目结构如下:

2、pom.xml 加入依赖,为所有模块提供 JUnit 和 LOG4J 依赖。

在 provider 模块和 consumer 模块的 resources 目录里加入 log4j.properties 配置文件

3、在 interface 模块中创建接口 com.chanshuyi.service.IUserService

interface 模块配置完毕。

4、在 provider 模块中引入 Spring、Dubbo、interface 模块依赖

创建 com.chanshuyi.service.impl.UserServiceImpl 类,实现 IUserService 接口

5、创建 Spring 配置文件,配置注解扫描 com.chanshuyi.service.impl 包,并引入 spring-provider.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"
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.xsd"> <!-- **************************** 注解扫描 **************************** -->
<context:component-scan base-package="com.chanshuyi.service.impl"/>
<!-- **************************** /注解扫描 **************************** --> <!-- **************************** 导入其他XML文件 **************************** -->
<import resource="spring-provider.xml"/>
<!-- **************************** /导入其他XML文件 **************************** -->
</beans>

6、创建 spring-provider.xml 文件,它是 dubbo 的主要配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<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="dubbodemo-provider"/>
<!-- 连接到哪个本地注册中心 -->
<dubbo:registry id="dubbodemo" address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="28080"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service registry="dubbodemo" timeout="3000" interface="com.chanshuyi.service.IUserService" ref="userService"/>
</beans>

可以看到这里有几个关键参数:application、registry、protocol、service。

  • application 指当前应用名称,主要用来给 zookeeper 注册中心计算应用间依赖关系。
  • registry 用来声明一个注册中心,这里声明了一个id 为 registry 的注册中心,地址是本地服务器的 2181 端口(这里要与 zookeeper 配置文件的 clientPort 属性值一致)。
  • protocol 指该应用用 dubbo 协议在 28080 端口暴露服务,其他应用可以通过这个接口调用服务。
  • service 用来声明需要暴露的服务接口,这里暴露了IUserService 接口,并将接口注册到 id 为 dubbodemo 的注册中心,它引用了 Spring 中名为 userService 的类,超时时间为 3 秒。

到这里 provider 提供者的配置基本上完成,但我们还需要写一个启动类将 provider 启动起来提供服务。

7、创建 com.chanshuyi.util.BeanFactoryUtil.java,是加载 Spring 的工具类

package com.chanshuyi.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanFactoryUtil {
private static ApplicationContext ctx_producer = null; public final static String ApplicationContextRoot = "";
public final static String ApplicationContextPath = ApplicationContextRoot + "applicationContext.xml"; public static void init() {
if (ctx_producer == null) {
synchronized (BeanFactoryUtil.class) {
if(ctx_producer == null){
String[] configLocations = new String[]{ApplicationContextPath};
ctx_producer = new ClassPathXmlApplicationContext(configLocations);
}
}
}
} public static ApplicationContext getContext() {
init();
return ctx_producer;
}
}

8、创建 com.chanshuyi.util.SystemDetails.java,用于输出系统信息

9、创建 com.chanshuyi.Launcher.java,用于启动 provider 服务,是启动入口

package com.chanshuyi;

import com.chanshuyi.util.BeanFactoryUtil;
import com.chanshuyi.util.SystemDetails;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class Launcher { private static Log logger = LogFactory.getLog(Launcher.class); /**
* @param args
*/
public static void main(String[] args) {
System.out.println("=======================");
System.out.println(" Core包启动 ");
SystemDetails.outputDetails();
System.out.println("======================="); getLocalip();
// 初始化spring
logger.info("开始初始化core服务");
BeanFactoryUtil.init(); try{
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 取得本机ip地址 注意:Spring RmiServiceExporter取得本机ip的方法:InetAddress.getLocalHost()
*/
private static void getLocalip() {
try {
System.out.println("服务暴露的ip: "
+ java.net.InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

到这里 provider 模块配置结束。我们运行 Launcher.main() 方法启动服务,并打开 zookeeper 注册中心(点击这里下载,双击 bin/zkServer.cmd 运行即可),启动 provider 服务。

10、接下来我们编写 consumer 代码。

在 consumer 的 pom.xml 中导入 Spring、dubbo、interface 模块依赖

创建类 com.chanshuyi.UserServiceConsumer.java

package com.chanshuyi;

import com.chanshuyi.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Administrator on 2016/1/19.
*/
public class UserServiceConsumer { private static Logger logger = LoggerFactory.getLogger(UserServiceConsumer.class); public static void main(String args[]) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService userService = (IUserService) ctx.getBean("userService");
logger.info("执行结果:" + userService.login("hello", "hello"));
}
}

配置 applicationContext.xml 文件以及 spring-consumer.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- **************************** 导入其他XML文件 **************************** -->
<import resource="spring-consumer.xml"/>
<!-- **************************** /导入其他XML文件 **************************** -->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<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="dubbodemo-consumer"/>
<!-- 连接到哪个注册中心(连接到本机的2181端口zookeeper) -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 消费方用什么协议获取服务(用dubbo协议在20880端口暴露服务) -->
<dubbo:protocol port="28080"/>
<!-- 提供哪些接口给消费者调用 -->
<dubbo:reference id="userService" interface="com.chanshuyi.service.IUserService"/>
</beans>

spring-consumer.xml 的配置大致与 spring-provider.xml 相同,只是 dubbo:service 节点换成 dubbo:reference 节点。 节点表示引用一个服务,其中 id 表示该服务的唯一标识,可以用该 id 实现 IOC 注入,interface 表示引用的服务接口。

到这里 consumer 模块配置基本结束。我们运行 UserServiceConsumer.main() 方法调用 provider 服务(provider 服务要开启哦),成功之后会打印出是否调用成功。

二、总结

DUBBO 框架是在 Spring 的基础上加上一个简单的配置文件即可把一个服务暴露出去。

DUBBO 配置文件基本有 application、registry、protocol 3个公共参数分别告诉了 DUBBO 以及 zookeeper 注册中心:我是谁?我向谁注册?怎么调用我的服务? 通过这 3 个配置,其他消费者就可以找到对应服务。

其他配置方式参考原地址。

dubbo配置方式简单介绍的更多相关文章

  1. Dubbo配置方式详解

    Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是阿里巴巴 SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次 ...

  2. [转载,感觉写的非常详细]DUBBO配置方式详解

    [转载,感觉写的非常详细]DUBBO配置方式详解 原文链接:http://www.cnblogs.com/chanshuyi/p/5144288.html DUBBO 是一个分布式服务框架,致力于提供 ...

  3. dubbo学习实践(3)之Dubbo整合Consul及Dubbo配置方式

    前言:上一篇中,已经写到了使用zookeeper为注册中心的配置,下面写下配置Consul为注册中心 1. Consul注册中心验证 修改provider和consumer的服务配置文件 Provid ...

  4. dubbo实现原理简单介绍

    一.什么是dubbo   Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,     ...

  5. dubbo&hsf&spring-cloud简单介绍

    Dubbo: 简介:Dubbo是一个分布式服务框架,以及SOA治理方案.其功能主要包括:高性能NIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与降级等. 底部NIO基于netty ...

  6. dubbo框架的简单介绍

    以下的官网的介绍. dubbo是SOA.小例子是简单的远程调用(生产者消费者的模式出现).http://blog.csdn.net/huangyekan/article/details/4217267 ...

  7. spring Bean装配的几种方式简单介绍

    Spring容器负责创建应用程序中的bean同时通过ID来协调这些对象之间的关系.作为开发人员,我们需要告诉Spring要创建哪些bean并且如何将其装配到一起. spring中bean装配有两种方式 ...

  8. spring 整合quartz的方式——简单介绍

    一.继承QuartzJobBean,重写executeInternal方法 <bean name="statQuartzJob" class="org.spring ...

  9. H5页面前后端通信 (3种方式简单介绍)

    1.ajax:短连接 2.websocket :长连接,双向的.   node搭建的websocket服务器,推送信息给客户端浏览器 :https://www.cnblogs.com/fps2tao/ ...

随机推荐

  1. python笔记16-函数

    函数说白了,就是把一组代码合到一起,可以实现某种功能,需要再用到这个功能的话,直接调用这个函数就行了 1.定义函数def def my_open():#函数名,def定义函数,my_open给这个函数 ...

  2. jar 包启动

    java -Xms256m -Xmx512m -Xmn256m -jar /home/apps/video/video.jar --spring.profiles.active=test #!/bin ...

  3. re模块+面向对象

    re模块 re:其实就是带有特殊语法的字符串 语法:单个字符和多个字符 单个字符: \d是匹配所有的数字 \D是匹配所有的非数字 \s是所有的换行符,制表符,空白等,回车符 \S是所有费换行符,空白和 ...

  4. # 学号 20175223 《Java程序设计》第3周学习总结

    学号 20175223 <Java程序设计>第3周学习总结 教材学习内容总结 第四章要点: 要点1:面向对象三个性质:封装性.继承.多态: 要点2:类:类声明.类体.成员变量.方法.类的U ...

  5. chown语法

    chown 作用:改变某个文件或目录的所有者和所属的组, 该命令可以向某个用户授权,使该用户编程指定文件的所有者或者改变文件的所属组, 用户可以是用户或者是用户ID, 用户组可以是组名或者租ID,   ...

  6. 16.求Sn=a+aa+aaa+aaaa.......之值

    其中a是一个数字,n表示a的位数,例如:2+22+222+2222+22222(此时n=5): #include <stdio.h> #include <stdlib.h> i ...

  7. 剑指Offer 56. 删除链表中重复的结点 (链表)

    题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...

  8. php英语单词大全95

    abstract抽象的 -挨伯丝拽克特 access存取.访问 -挨克色丝 account账户 -厄靠恩特 action动作 -爱克身 activate激活 -爱克特维特 active活动的 -爱克得 ...

  9. stylelint 安装配置

    1.安装 stylelint: npm i stylelint -g npm i stylelint stylelint-config-standard --save-dev 2.在 scripts ...

  10. python魔法方法

    1.__call__ 实现__call__后,该类的对象可以被调用 举例如: class test_call_: def __init__(self, n): self.n = n def __cal ...