淘淘商城第十二课(发布dubbo服务)

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

       单一工程中spring的配置如下(不用Dubbo的情况)

       <bean id="xxxService" class="com.xx.XxxServiceImpl"/>

      <bean id="xxxAction" class="com.xxx.XxxAction">

             <property name="xxxService" ref="xxxService"/>

      </bean>

       用Dubbo的话,分为发布服务和调用服务,发布服务方式如下:

      <!--和本地服务一样实现远程服务-->

     <bean id="xxxService" class="com.xx.XxxServiceImpl"/>

     <!--增加暴露远程服务配置-->

     <dubbo:service interface="com.xx.XxxService" ref="xxxServiceImpl"/>

      调用服务的方式如下:

      <!--增加引用远程服务配置-->

      <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包下新建一个实现类,如下图所示。


       下面我们来发布dubbo服务,当然,在此之前我们需要先把dubbo相关的包给引进来,我们需要在taotao-manager-service工程的pom.xml文件中添加这三者的依赖。

  1. <!-- dubbo相关的jar包 -->
  2. <dependency>
  3. <groupId>com.alibaba </groupId>
  4. <artifactId>dubbo </artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.zookeeper </groupId>
  8. <artifactId>zookeeper </artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.github.sgroschupf </groupId>
  12. <artifactId>zkclient </artifactId>
  13. </dependency>
         当前taotao-manager-service工程的pom.xml文件的全部内容如下:

  1. <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">
  2. <modelVersion>4.0.0 </modelVersion>
  3. <parent>
  4. <groupId>com.taotao </groupId>
  5. <artifactId>taotao-manager </artifactId>
  6. <version>0.0.1-SNAPSHOT </version>
  7. </parent>
  8. <artifactId>taotao-manager-service </artifactId>
  9. <packaging>war </packaging>
  10. <dependencies>
  11. <dependency>
  12. <groupId>com.taotao </groupId>
  13. <artifactId>taotao-manager-dao </artifactId>
  14. <version>0.0.1-SNAPSHOT </version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.taotao </groupId>
  18. <artifactId>taotao-manager-interface </artifactId>
  19. <version>0.0.1-SNAPSHOT </version>
  20. </dependency>
  21. <!-- Spring -->
  22. <dependency>
  23. <groupId>org.springframework </groupId>
  24. <artifactId>spring-context </artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework </groupId>
  28. <artifactId>spring-beans </artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework </groupId>
  32. <artifactId>spring-webmvc </artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework </groupId>
  36. <artifactId>spring-jdbc </artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework </groupId>
  40. <artifactId>spring-aspects </artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework </groupId>
  44. <artifactId>spring-jms </artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework </groupId>
  48. <artifactId>spring-context-support </artifactId>
  49. </dependency>
  50. <!-- dubbo相关的jar包 -->
  51. <dependency>
  52. <groupId>com.alibaba </groupId>
  53. <artifactId>dubbo </artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.apache.zookeeper </groupId>
  57. <artifactId>zookeeper </artifactId>
  58. </dependency>
  59. <dependency>
  60. <groupId>com.github.sgroschupf </groupId>
  61. <artifactId>zkclient </artifactId>
  62. </dependency>
  63. </dependencies>
  64. </project>
       maven更新之后,我们来查看下taotao-manager-service目前所依赖的包情况,如下图所示。

       我们发现在dubbo-2.5.3.jar包下面有一个spring-2.5.6.SEC03.jar,要知道我们整个工程现在所使用的spring都是4.2.4版本的,这儿冒出个2.5.6版本,这就有可能会产生冲突,那么这个2.5.6版本的spring包是从哪儿来的呢?其实是依赖传递过来的。如下图所示,我们点击"Dependency Hierarchy",在上方找到dubbo,可以看到dubbo确实依赖着spring-2.5.6版本。



        那么既然包冲突了,我们就要解决掉它,方法是我们在dubbo中去掉对spring-2.5.6的依赖,方法如下图所示,我们在要删掉的spring-2.5.6.SEC03上右键,在弹出的菜单中点击"Exclude Maven Artifact..."。


     在弹出的对话框中点击"OK"。


        我们再回到pom.xml视图下,发现dubbo依赖中添加一段配置,这段配置就是用来去掉对spring-2.5.6版本的依赖的。


        保存pom.xml文件,maven会自动去掉spring-2.5.6版本的jar包,如下图所示,发现dubbo下面确实没有spring-2.5.6版本的jar包了。


       下面我们再看看是否还有jar包冲突,我们发现还有netty的jar包冲突,我们再解决这个冲突问题。


       我们还是到依赖视图进行查看,我们发现dubbo下依赖的netty版本是3.2.5,那么我们便去掉该依赖,留下3.2.7的高版本,操作方法跟上面一样,就不啰嗦了。

      解决完了jar包冲突,现在我们开始发布服务。我们在taotao-manager-service的applicationContext-service.xml文件中发布服务。

      首先我们需要在文件头部添加对dubbo的引用及约束,如下所示。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context= "http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p"
  4. xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx"
  5. xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo"
  6. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  9. 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
  10. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  12. <!-- 配置包扫描器,扫描所有带@Service注解的类 -->
  13. <context:component-scan base-package="com.taotao.service"/>
  14. </beans>
      为了让eclipse能够识别dubbo的约束,我们需要手动配置一下约束,方法是点击Window------>Preferences,弹出的对话框如下,我们找到XML,然后找到XML Catalog,在右侧点击"Add"按钮。

       我们会看到如下图所示界面,我们点击"File System...",Keys type我们选择URI,选择我们的dubbo.xsd文件所在的目录。


       dubbo的约束文件大家可以到:https://download.csdn.net/download/anaitudou/10502339这个地址进行下载,下载完之后解压,选择dubbo.xsd文件之后如下图所示,我们点击"OK"。


       点击"OK"之后,我们可以看到如下图所示界面,我们点击"OK"。这便配置好了dubbo的约束。


      配置好了dubbo的约束,我们再看看是否还有别的约束需要配置,我们打开配置文件,发现报spring-context-4.2.xsd没有配置。因此我们还需要配置它的约束,步骤同上面 一样,这里不再赘述。如果还有其他约束需要配置就配置一下。

 

      下面我们在applicationContext-service.xml文件中添加如下配置,其中<dubbo:application name="taotao-manager" />是用来配置在注册中心的名字。我们注意到我们在声明需要暴露的服务接口的时候,ref="itemServiceImpl"这句话,我们并没有声明过id为itemServiceImpl的类,为什么可以直接使用呢?这是因为我们配置了<context:component-scan base-package="com.taotao.service"/>扫描范围,我们在com.taotao.service包下新建了一个ItemServiceImpl的实现类并且我们没有明确指定其ID,这样的话,系统会默认它的首字母小写作为ID,这样虽然我们没有明确定义itemServiceImpl我们依然可以使用。另外注意<dubbo:registry protocol="zookeeper" address="192.168.156.40:2181" />配置的IP地址是我们安装dubbo的虚拟机的IP地址

  1. <!-- 发布dubbo服务 -->
  2. <!-- 提供方应用信息,用于计算依赖关系 -->
  3. <dubbo:application name="taotao-manager" />
  4. <!-- 注册中心的地址 -->
  5. <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181" />
  6. <!-- 用dubbo协议在20880端口暴露服务 -->
  7. <dubbo:protocol name="dubbo" port="20880" />
  8. <!-- 声明需要暴露的服务接口 -->
  9. <dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" timeout="300000"/>
       当前applicationContext.xml文件的全部内容如下。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context= "http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p"
  4. xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx"
  5. xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo"
  6. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  9. 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
  10. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  12. <!-- 配置包扫描器,扫描所有带@Service注解的类 -->
  13. <context:component-scan base-package="com.taotao.service"/>
  14. <!-- 发布dubbo服务 -->
  15. <!-- 提供方应用信息,用于计算依赖关系 -->
  16. <dubbo:application name="taotao-manager" />
  17. <!-- 注册中心的地址 -->
  18. <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181" />
  19. <!-- 用dubbo协议在20880端口暴露服务 -->
  20. <dubbo:protocol name="dubbo" port="20880" />
  21. <!-- 声明需要暴露的服务接口 -->
  22. <dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" timeout="300000"/>
  23. </beans>
      这样我们就发布了一个服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值