上节课我们一起学习了如何发布一个dubbo服务,这节我们一起学习下web工程如何引用dubbo发布的服务。
首先我们在web工程也需要添加dubbo的依赖,由于我们在taotao-manager-service工程已经添加了dubbo的依赖并且解决了包冲突的问题,那么我们便可以直接把taotao-manager-service工程下的关于dubbo的依赖部分拷贝过来,拷贝的内容如下:
- <!-- dubbo相关的jar包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <artifactId>spring</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- <exclusion>
- <artifactId>netty</artifactId>
- <groupId>org.jboss.netty</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- </dependency>
- <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-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.taotao</groupId>
- <artifactId>taotao-manager-web</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <dependencies>
- <!-- 依赖taotao-common -->
- <dependency>
- <groupId>com.taotao</groupId>
- <artifactId>taotao-common</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>
- <!-- JSP相关 -->
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jsp-api</artifactId>
- <scope>provided</scope>
- </dependency>
- <!-- dubbo相关的jar包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <artifactId>spring</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- <exclusion>
- <artifactId>netty</artifactId>
- <groupId>org.jboss.netty</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <configuration>
- <path>/</path>
- <port>8081</port>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
- <!-- 引用dubbo服务 -->
- <dubbo:application name="taotao-manager-web"/>
- <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>
- <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />
引用dubbo服务之后springmvc的完整内容如下:
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
- <!-- 配置注解驱动 -->
- <mvc:annotation-driven />
- <!-- 视图解析器 -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 配置包扫描器,扫描@Controller注解的类 -->
- <context:component-scan base-package="com.taotao.controller"/>
- <!-- 引用dubbo服务 -->
- <dubbo:application name="taotao-manager-web"/>
- <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>
- <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />
- </beans>
由于taotao-manager-web工程与taotao-mananger-interface是独立的两个工程,因此要在taotao-manager-web工程中要添加对taotao-manager-interface的依赖,添加的内容如下:
- <dependency>
- <groupId>com.taotao</groupId>
- <artifactId>taotao-manager-interface</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <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-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.taotao</groupId>
- <artifactId>taotao-manager-web</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <dependencies>
- <!-- 依赖taotao-common -->
- <dependency>
- <groupId>com.taotao</groupId>
- <artifactId>taotao-common</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>
- <!-- JSP相关 -->
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jsp-api</artifactId>
- <scope>provided</scope>
- </dependency>
- <!-- dubbo相关的jar包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <artifactId>spring</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- <exclusion>
- <artifactId>netty</artifactId>
- <groupId>org.jboss.netty</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <configuration>
- <path>/</path>
- <port>8081</port>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
添加完对taotao-manager-interface的依赖后,我们便可以直接使用它了。
如果我们使用@ResponseBody注解后访问的时候报406错误(无法把对象转换为json串),90%是可能没有引入jackson包,我们到"Maven Dependencies"下查看是否有如下图所示的三个包。
如果没有,那么我们需要在taotao-common工程的pom.xml文件中引入jackson的依赖,由于我们的taotao-mananger-web工程依赖了taotao-common,因此它会自动依赖jackson包。现在我们的taotao-common中是依赖了jackson的,因此我们不用做任何处理。
- <!-- Jackson Json处理工具包 -->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- </dependency>