dubbo实践2--引用dubbo服务

   上篇讲解了如何发布一个dubbo服务,这节讲解web工程如何引用dubbo发布的服务。

         首先我们在web工程也需要添加dubbo的依赖,由于我们在taotao-manager-service工程已经添加了dubbo的依赖并且解决了包冲突的问题,那么我们便可以直接把taotao-manager-service工程下的关于dubbo的依赖部分拷贝过来,拷贝的内容如下:

[html]  view plain  copy
  1. <!-- dubbo相关的jar包 -->  
  2.     <dependency>  
  3.         <groupId>com.alibaba</groupId>  
  4.         <artifactId>dubbo</artifactId>  
  5.         <exclusions>  
  6.             <exclusion>  
  7.                 <artifactId>spring</artifactId>  
  8.                 <groupId>org.springframework</groupId>  
  9.             </exclusion>  
  10.             <exclusion>  
  11.                 <artifactId>netty</artifactId>  
  12.                 <groupId>org.jboss.netty</groupId>  
  13.             </exclusion>  
  14.         </exclusions>  
  15.     </dependency>  
  16.     <dependency>  
  17.         <groupId>org.apache.zookeeper</groupId>  
  18.         <artifactId>zookeeper</artifactId>  
  19.     </dependency>  
  20.     <dependency>  
  21.         <groupId>com.github.sgroschupf</groupId>  
  22.         <artifactId>zkclient</artifactId>  
  23.     </dependency>  
         拷贝后的taotao-manager-web工程的pom.xml文件的内容如下:

[html]  view plain  copy
  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-parent</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.   </parent>  
  8.   <groupId>com.taotao</groupId>  
  9.   <artifactId>taotao-manager-web</artifactId>  
  10.   <version>0.0.1-SNAPSHOT</version>  
  11.   <packaging>war</packaging>  
  12.     
  13.   <dependencies>  
  14.     <!-- 依赖taotao-common -->  
  15.     <dependency>  
  16.         <groupId>com.taotao</groupId>  
  17.         <artifactId>taotao-common</artifactId>  
  18.         <version>0.0.1-SNAPSHOT</version>  
  19.     </dependency>  
  20.     <!-- Spring -->  
  21.     <dependency>  
  22.         <groupId>org.springframework</groupId>  
  23.         <artifactId>spring-context</artifactId>  
  24.     </dependency>  
  25.     <dependency>  
  26.         <groupId>org.springframework</groupId>  
  27.         <artifactId>spring-beans</artifactId>  
  28.     </dependency>  
  29.     <dependency>  
  30.         <groupId>org.springframework</groupId>  
  31.         <artifactId>spring-webmvc</artifactId>  
  32.     </dependency>  
  33.     <dependency>  
  34.         <groupId>org.springframework</groupId>  
  35.         <artifactId>spring-jdbc</artifactId>  
  36.     </dependency>  
  37.     <dependency>  
  38.         <groupId>org.springframework</groupId>  
  39.         <artifactId>spring-aspects</artifactId>  
  40.     </dependency>  
  41.     <dependency>  
  42.         <groupId>org.springframework</groupId>  
  43.         <artifactId>spring-jms</artifactId>  
  44.     </dependency>  
  45.     <dependency>  
  46.         <groupId>org.springframework</groupId>  
  47.         <artifactId>spring-context-support</artifactId>  
  48.     </dependency>  
  49.     <!-- JSP相关 -->  
  50.     <dependency>  
  51.         <groupId>jstl</groupId>  
  52.         <artifactId>jstl</artifactId>  
  53.     </dependency>  
  54.     <dependency>  
  55.         <groupId>javax.servlet</groupId>  
  56.         <artifactId>servlet-api</artifactId>  
  57.         <scope>provided</scope>  
  58.     </dependency>  
  59.     <dependency>  
  60.         <groupId>javax.servlet</groupId>  
  61.         <artifactId>jsp-api</artifactId>  
  62.         <scope>provided</scope>  
  63.     </dependency>  
  64.     <!-- dubbo相关的jar包 -->  
  65.     <dependency>  
  66.         <groupId>com.alibaba</groupId>  
  67.         <artifactId>dubbo</artifactId>  
  68.         <exclusions>  
  69.             <exclusion>  
  70.                 <artifactId>spring</artifactId>  
  71.                 <groupId>org.springframework</groupId>  
  72.             </exclusion>  
  73.             <exclusion>  
  74.                 <artifactId>netty</artifactId>  
  75.                 <groupId>org.jboss.netty</groupId>  
  76.             </exclusion>  
  77.         </exclusions>  
  78.     </dependency>  
  79.     <dependency>  
  80.         <groupId>org.apache.zookeeper</groupId>  
  81.         <artifactId>zookeeper</artifactId>  
  82.     </dependency>  
  83.     <dependency>  
  84.         <groupId>com.github.sgroschupf</groupId>  
  85.         <artifactId>zkclient</artifactId>  
  86.     </dependency>  
  87.   </dependencies>  
  88.   <build>  
  89.     <plugins>  
  90.         <plugin>  
  91.             <groupId>org.apache.tomcat.maven</groupId>  
  92.             <artifactId>tomcat7-maven-plugin</artifactId>  
  93.             <configuration>  
  94.                 <path>/</path>  
  95.                 <port>8081</port>  
  96.             </configuration>  
  97.         </plugin>  
  98.     </plugins>  
  99.   </build>  
  100. </project>  
       下面我们需要在taotao-manager-web工程下的springmvc.xml文件中引用dubbo服务,添加的内容如下。其中<dubbo:application name="taotao-manager-web"/>是要在注册中心申报一下名字,名字不能冲突。<dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>同服务端一样,在注册中心注册下dubbo服务所在的IP及端口。<dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />便是要引用服务端的接口了。

[html]  view plain  copy
  1. <!-- 引用dubbo服务 -->  
  2.     <dubbo:application name="taotao-manager-web"/>  
  3.     <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>    
  4.     <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />  

      引用dubbo服务之后springmvc的完整内容如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
  9.         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  10.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">  
  11.           
  12.     <!-- 配置注解驱动 -->  
  13.     <mvc:annotation-driven />  
  14.     <!-- 视图解析器 -->  
  15.     <bean  
  16.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  17.         <property name="prefix" value="/WEB-INF/jsp/" />  
  18.         <property name="suffix" value=".jsp" />  
  19.     </bean>  
  20.     <!-- 配置包扫描器,扫描@Controller注解的类 -->  
  21.     <context:component-scan base-package="com.taotao.controller"/>  
  22.       
  23.     <!-- 引用dubbo服务 -->  
  24.     <dubbo:application name="taotao-manager-web"/>  
  25.     <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>    
  26.     <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />  
  27. </beans>  

      由于taotao-manager-web工程与taotao-mananger-interface是独立的两个工程,因此要在taotao-manager-web工程中要添加对taotao-manager-interface的依赖,添加的内容如下:

[html]  view plain  copy
  1. <dependency>  
  2.         <groupId>com.taotao</groupId>  
  3.         <artifactId>taotao-manager-interface</artifactId>  
  4.         <version>0.0.1-SNAPSHOT</version>  
  5.     </dependency>  
       添加后当前taotao-manager-web工程的pom.xml文件全部内容如下:

[html]  view plain  copy
  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-parent</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.   </parent>  
  8.   <groupId>com.taotao</groupId>  
  9.   <artifactId>taotao-manager-web</artifactId>  
  10.   <version>0.0.1-SNAPSHOT</version>  
  11.   <packaging>war</packaging>  
  12.     
  13.   <dependencies>  
  14.     <!-- 依赖taotao-common -->  
  15.     <dependency>  
  16.         <groupId>com.taotao</groupId>  
  17.         <artifactId>taotao-common</artifactId>  
  18.         <version>0.0.1-SNAPSHOT</version>  
  19.     </dependency>  
  20.     <dependency>  
  21.         <groupId>com.taotao</groupId>  
  22.         <artifactId>taotao-manager-interface</artifactId>  
  23.         <version>0.0.1-SNAPSHOT</version>  
  24.     </dependency>  
  25.     <!-- Spring -->  
  26.     <dependency>  
  27.         <groupId>org.springframework</groupId>  
  28.         <artifactId>spring-context</artifactId>  
  29.     </dependency>  
  30.     <dependency>  
  31.         <groupId>org.springframework</groupId>  
  32.         <artifactId>spring-beans</artifactId>  
  33.     </dependency>  
  34.     <dependency>  
  35.         <groupId>org.springframework</groupId>  
  36.         <artifactId>spring-webmvc</artifactId>  
  37.     </dependency>  
  38.     <dependency>  
  39.         <groupId>org.springframework</groupId>  
  40.         <artifactId>spring-jdbc</artifactId>  
  41.     </dependency>  
  42.     <dependency>  
  43.         <groupId>org.springframework</groupId>  
  44.         <artifactId>spring-aspects</artifactId>  
  45.     </dependency>  
  46.     <dependency>  
  47.         <groupId>org.springframework</groupId>  
  48.         <artifactId>spring-jms</artifactId>  
  49.     </dependency>  
  50.     <dependency>  
  51.         <groupId>org.springframework</groupId>  
  52.         <artifactId>spring-context-support</artifactId>  
  53.     </dependency>  
  54.     <!-- JSP相关 -->  
  55.     <dependency>  
  56.         <groupId>jstl</groupId>  
  57.         <artifactId>jstl</artifactId>  
  58.     </dependency>  
  59.     <dependency>  
  60.         <groupId>javax.servlet</groupId>  
  61.         <artifactId>servlet-api</artifactId>  
  62.         <scope>provided</scope>  
  63.     </dependency>  
  64.     <dependency>  
  65.         <groupId>javax.servlet</groupId>  
  66.         <artifactId>jsp-api</artifactId>  
  67.         <scope>provided</scope>  
  68.     </dependency>  
  69.     <!-- dubbo相关的jar包 -->  
  70.     <dependency>  
  71.         <groupId>com.alibaba</groupId>  
  72.         <artifactId>dubbo</artifactId>  
  73.         <exclusions>  
  74.             <exclusion>  
  75.                 <artifactId>spring</artifactId>  
  76.                 <groupId>org.springframework</groupId>  
  77.             </exclusion>  
  78.             <exclusion>  
  79.                 <artifactId>netty</artifactId>  
  80.                 <groupId>org.jboss.netty</groupId>  
  81.             </exclusion>  
  82.         </exclusions>  
  83.     </dependency>  
  84.     <dependency>  
  85.         <groupId>org.apache.zookeeper</groupId>  
  86.         <artifactId>zookeeper</artifactId>  
  87.     </dependency>  
  88.     <dependency>  
  89.         <groupId>com.github.sgroschupf</groupId>  
  90.         <artifactId>zkclient</artifactId>  
  91.     </dependency>  
  92.   </dependencies>  
  93.   <build>  
  94.     <plugins>  
  95.         <plugin>  
  96.             <groupId>org.apache.tomcat.maven</groupId>  
  97.             <artifactId>tomcat7-maven-plugin</artifactId>  
  98.             <configuration>  
  99.                 <path>/</path>  
  100.                 <port>8081</port>  
  101.             </configuration>  
  102.         </plugin>  
  103.     </plugins>  
  104.   </build>  
  105. </project>  
        maven更新完之后,我们可以在taotao-manager-web工程所依赖的包中找到taotao-manager-interface,如下图所示。

       添加完对taotao-manager-interface的依赖后,我们便可以直接使用它了。

        如果我们使用@ResponseBody注解后访问的时候报406错误(无法把对象转换为json串),90%是可能没有引入jackson包,我们到"Maven Dependencies"下查看是否有如下图所示的三个包。

        如果没有,那么我们需要在taotao-common工程的pom.xml文件中引入jackson的依赖,由于我们的taotao-mananger-web工程依赖了taotao-common,因此它会自动依赖jackson包。现在我们的taotao-common中是依赖了jackson的,因此我们不用做任何处理。

[html]  view plain  copy
  1. <!-- Jackson Json处理工具包 -->  
  2.         <dependency>  
  3.             <groupId>com.fasterxml.jackson.core</groupId>  
  4.             <artifactId>jackson-databind</artifactId>  
  5.         </dependency>  
       到此,我们的SSM框架整合便整合完了。
相关文章
相关标签/搜索