原地址: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. SQL-41 构造触发器

    题目描述 构造一个触发器audit_log,在向employees_test表中插入一条数据的时候,触发插入相关的数据到audit中.CREATE TABLE employees_test(ID IN ...

  2. 什么是Maven项目

    1.通俗理解Maven:https://blog.csdn.net/shuzhe66/article/details/45009175 个人总结: Maven项目会有pom文件! 当前的项目需要依赖其 ...

  3. python 写的员工信息查询

    #!/use/bin/env pythonn#_*_ coding:utf-8 _*_import timedef Bre():    while True:        Bre_falg = ra ...

  4. LADP(Lightweight Directory Access Protocol)轻量目录访问协议~小知识

    What is LDAP and how does it work(implementation)? LDAP stands for “Lightweight Directory Access Pro ...

  5. Coprime Sequence (HDU 6025)前缀和与后缀和的应用

    题意:给出一串数列,这串数列的gcd为1,要求取出一个数使取出后的数列gcd最大. 题解:可以通过对数列进行预处理,求出从下标为1开始的数对于前面的数的gcd(数组从下标0开始),称为前缀gcd,再以 ...

  6. Java基础之枚举类型

    枚举 为什么需要枚举 在Java开发过程中,有时需要定义一系列有限的数据,如:月份.星期等.<br> java1.5之前,主要通过定义一系列静态常量完成. class Genders{ p ...

  7. myelipse与maven结合配置

    添加插件 指定maven中jar包默认仓库地址 maven中debug模式中断点关联不到项目代码的问题 解决方法参考文章:https://blog.csdn.net/urnot/article/det ...

  8. NN 激活函数 待修改

    Softmax 函数/算法 https://www.zhihu.com/question/23765351 RELU 激活函数及其他相关的函数 http://blog.csdn.net/u013146 ...

  9. NumPy 基础用法

    NumPy 是高性能科学计算和数据分析的基础包. 它是 pandas 等其他各种工具的基础. 主要功能: ndarray 一个多维数组结构, 高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...

  10. 域名到站点的负载均衡技术一览(主要是探讨一台Nginx抵御大并发的解决方案)(转)https://www.cnblogs.com/EasonJim/p/7823410.html

    一.问题域 Nginx.LVS.Keepalived.F5.DNS轮询,往往讨论的是接入层的这样几个问题: 1)可用性:任何一台机器挂了,服务受不受影响 2)扩展性:能否通过增加机器,扩充系统的性能 ...