(转)淘淘商城系列——发布dubbo服务
http://blog.csdn.net/yerenyuan_pku/article/details/72758639
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。那么Dubbo应该怎么使用呢?下面我将揭晓答案。
如果不用Dubbo,单一工程中spring的配置可能如下:
<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<bean id="xxxAction" class="com.xxx.XxxAction">
<property name="xxxService" ref="xxxService" />
</bean>
一旦用了Dubbo,在本地服务的基础上,只需做简单配置,即可完成远程化服务。例如,将上面的配置文件拆分成两份,将服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml,并在提供方增加暴露服务配置<dubbo:service>
,在消费方增加引用服务配置<dubbo:reference>
。所以,我们要是使用了Dubbo的话,就要分为发布服务和调用服务两部分了,发布服务方式如下:
<!-- 和本地服务一样实现远程服务 -->
<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<!-- 增加暴露远程服务配置,interface:指定服务的接口,ref:指定接口的实现类 -->
<dubbo:service interface="com.xxx.XxxService" ref="xxxService" />
(表现层)调用服务的方式如下:
<!-- 增加引用远程服务配置,interface:指定服务的接口-->
<dubbo:reference id="xxxService" interface="com.xxx.XxxService" />
<!-- 和本地服务一样使用远程服务 -->
<bean id="xxxAction" class="com.xxx.XxxAction">
<property name="xxxService" ref="xxxService" />
</bean>
下面我们便以一个具体的例子来说明dubbo的使用方法,我们的需求是,想根据商品的ID来查询商品的详细信息,商品表是tb_item,如下图所示。
既然要查询就要有接口,我们在taotao-manager-interface工程下新建包com.taotao.service,并在该包下新建ItemService接口类,在该接口中添加一个根据ID查商品的方法,如下图所示。
由于taotao-manager-service和taotao-manager-interface都是独立的jar工程,要想在taotao-manager-service中引用taotao-manager-interface的接口就需要在taotao-manager-service的pom.xml文件中添加对taotao-manager-interface的依赖,如下图所示。
如果我记得没错的话,之前我就已添加过了。
然后我们在taotao-manager-service的com.taotao.service.impl包下新建一个实现类,如下图所示。
下面我们来发布dubbo服务,当然,在此之前我们需要先把dubbo相关的包给引进来,我们需要在taotao-manager-service工程的pom.xml文件中添加这三者的依赖。
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
这儿,大家要注意,dubbo-2.5.3.jar包下面依赖了一个spring-2.5.6.SEC03.jar包,要知道我们整个工程现在所使用的spring都是4.2.4版本的,这儿冒出个2.5.6版本,这就有可能会产生冲突,那么这个2.5.6版本的spring包是从哪儿来的呢?其实是依赖传递过来的。所以我们应在dubbo中去掉对spring-2.5.6的依赖。同理,我们还要在dubbo中去掉对netty-3.2.5的依赖,maven更新之后,我们来查看下taotao-manager-service目前所依赖的jar包情况,如下图所示。
如此一来,当前taotao-manager-service工程的pom.xml文件的全部内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taotao-manager-service</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
</project>
现在我们开始发布服务,我们在taotao-manager-service工程的applicationContext-service.xml文件中发布服务。
首先我们需要在文件头部添加对dubbo的引用及约束,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.taotao.service"></context:component-scan>
</beans>
效果如下:
接下来为了能让eclipse能够识别dubbo的约束,我们需要手动配置一下该约束。我也相信大家都能配置,故在此不再赘述。配置好了dubbo的约束之后,我们再看看是否还有别的约束需要配置,如果还有其他约束需要配置就配置一下,这里也不再赘述。
下面我们在applicationContext-service.xml文件中添加如下配置:
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-manager" />
<dubbo:registry protocol="zookeeper" address="192.168.25.167:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />
- 1
其中<dubbo:application name="taotao-manager" />
是用来配置在注册中心的名字,标识我们当前应用的一个名称,你可以随便起,但是最好不要跟其他的应用重复,最好跟你的工程名相对应。我们注意到我们在声明需要暴露的服务接口的时候,ref=”itemServiceImpl”这句话,我们并没有声明过id为itemServiceImpl的类,为什么可以直接使用呢?这是因为我们配置了<context:component-scan base-package="com.taotao.service"/>
扫描范围,我们在com.taotao.service包下新建了一个ItemServiceImpl的实现类并且我们没有明确指定其ID,这样的话,系统会默认它的首字母小写作为ID,这样虽然我们没有明确定义id为itemServiceImpl的bean,我们依然还是可以使用的。另外注意<dubbo:registry protocol="zookeeper" address="192.168.25.167:2181" />
配置的IP地址是我们安装dubbo的虚拟机的IP地址,其中2181是zookeeper默认运行的端口号,即客户端连接服务端的端口号是2181。
这样一来,当前applicationContext-service.xml文件的全部内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.taotao.service"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-manager" />
<dubbo:registry protocol="zookeeper" address="192.168.25.167:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />
</beans>
至此,我们就发布了一个服务。
(转)淘淘商城系列——发布dubbo服务的更多相关文章
- (转)淘淘商城系列——引用dubbo服务
http://blog.csdn.net/yerenyuan_pku/article/details/72758663 上文我们一起学习了如何发布一个dubbo服务,本文我就来教大家如何在web工程中 ...
- (三)发布Dubbo服务
我们现在来学习下发布Dubbo服务,主要参考dubbo开发包里的demo源码:由浅入深的讲解下这个小demo: github地址:https://github.com/apache/incubator ...
- 《Duubo系列》-Dubbo服务暴露过程
我今天来就带大家看看 Dubbo 服务暴露过程,这个过程在 Dubbo 中其实是很核心的过程之一,关乎到你的 Provider 如何能被 Consumer 得知并调用. 今天还是会进行源码解析,毕竟我 ...
- Dubbo(2)发布Dubbo服务
主要参考Dubbo源码包里面的dubbo-demo源码: 1.项目结构: 2.pom.xml中的依赖: <project xmlns="http://maven.apache.org/ ...
- (转)淘淘商城系列——SSM框架整合之Dao层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...
- dubbo服务的发布和调用
Dubbo是分布式服务架构,是一个优秀的开源服务型框架,使得应用可以通过高性能的rpc实现服务的输入和输出功能.其实dubbo就是资源调度和治理中心的管理工具. 发布dubbo服务:在提供服务的应用中 ...
- dubbo 发布 RPC 服务
Dubbo 发布 RPC 服务 建立服务提供者项目 pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- (五)消费Dubbo服务
前面我们搞了发布Dubbo服务,发布的服务就是用来消费的,所以我们这里来调用服务,消费下: 创建maven项目 dubbo-demo-consumer pom.xml配置下: <dependen ...
- (转) 淘淘商城系列——CMS内容管理系统工程搭建
http://blog.csdn.net/yerenyuan_pku/article/details/72825801 淘淘商城系列——CMS内容管理系统工程搭建 上文我们一起搭建了表现层中的商城门户 ...
随机推荐
- POJ - 1470 Closest Common Ancestors(离线Tarjan算法)
1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...
- 有关使用HTTP协议传输二进制文件
HTTP协议是基于字符(ASCII)的,当Content-Type项为text/xml,则内容是文本格式:当二进制格式时,Content-Type项为image/gif,就是了.例如,浏览器请求一张图 ...
- windows系统修改mysql端口的方法
1.首先在控制面板--管理工具--服务里停止mysql服务
- java的内部类解析
内部类分为四种: 成员内部类.类方法与普通方法同级: 局部内部类.类方法内部,局部内部类有构造器,通过构造器把外部的变量传入局部内部类再使用是完全可以的 匿名内部类.匿名内部类是唯一没有构造器的类,和 ...
- 《Perceptual Losses for Real-Time Style Transfer and Super-Resolution》论文笔记
参考 http://blog.csdn.net/u011534057/article/details/55052304 代码 https://github.com/yusuketomoto/chain ...
- 【411】COMP9024 Assignment1 问题汇总
1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt' 实际上是没有链接math数学库,所以要 $gcc test.c –lm //-lm就是链接到m ...
- 【WIP】markdown
创建: 2018/03/18 [任务表]TODO 这个博客从来不点发布到首页, 完全100%自用, 全部详细完整的干货.千辛万苦找到这里看到一片空白, 是不是很愤怒? 那就对啦233333
- 洛谷 P2774 方格取数问题【最小割】
因为都是正整数,所以当然取得越多越好.先把所有点权加起来,黑白染色后,s向所有黑点连流量为点权的边,所有白点向t连流量为点权的边,然后黑点向相邻的四个白点连流量为inf的边,表示不可割,这样一来,对于 ...
- 使用VS2008,VS2010编译64位的应用程序
要编译生成64位的应用程序,就必须把vs2008,或vs2010的配置管理器设置为x64. 如果你的配置管理器那里没有x64这个选项,那么是你在安装vs时可能没有安装这个组件.你不用卸载vs,只需打开 ...
- [转]Boosting
1 Boosting算法的起源 Boosting方法是一种用来提高弱分类算法准确度的方法,这种方法通过构造一个预测函数系列,然后以一定的方式将他们组合成一个预测函数.Boosting是一种提高任意给定 ...