上文我们一起学习了如何发布一个dubbo服务,本文我就来教大家如何在web工程中引用dubbo发布的服务。
首先我们在web工程也需要添加dubbo的依赖,我们可以直接把OA-system-service工程下的关于dubbo的依赖部分拷贝过来,拷贝的内容如下:
<!-- 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> |
拷贝后的OA-system-web工程的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.QEcode</groupId>
<artifactId>OA-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>OA-system-web</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- 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>
<!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</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>
下面我们需要在OA-system-web工程下的springmvc.xml文件中引用dubbo服务,添加的内容如下。
<!-- 引用dubbo服务 --> <dubbo:application name="OA-system-web"/> <!-- 超时时间 --> <dubbo:consumer timeout="5000" /> <dubbo:registry protocol="zookeeper" address="${dubbo.address}"/> <dubbo:reference interface="com.QEcode.OA.service.UserService" id="userService" /> |
<dubbo:consumer timeout="5000" />表示在连接超过5秒后自动断开连接,这是为了防止某个服务发生死循环一直连接,当然在实际中,5秒也是很长的时间了,不过我们有时候要debug,所以把连接时间调长一点.同时,要注意的是:
可以看到,service层的是ref,表示引用userServiceImpl bean,而web层的是id,表示将这个bean注入到spring容器中,那么我们就可以在controller中使用该bean了.
同样我们需要在resources目录下新建一个resource.properties文件,并配置dubbo地址
由于在applicationContext-web.xml中,我们没有让spring加载配置文件,所以我们需要添加一下内容来让spring加载配置文件
<!-- 加载配置文件 --> <context:property-placeholder location="classpath:resource/*.properties"/> |
引用dubbo服务之后,完整的applicationContext-web.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties"/>
<context:component-scan base-package="com.QEcode.OA.controller" />
<!-- 引用dubbo服务 -->
<dubbo:application name="OA-system-web"/>
<!-- 超时时间 -->
<dubbo:consumer timeout="5000" />
<dubbo:registry protocol="zookeeper" address="${dubbo.address}"/>
<dubbo:reference interface="com.QEcode.OA.service.UserService" id="userService" />
</beans>
服务调用测试
我们已经发布和引用了dubbo服务了,而且我们在之前的博客中以及构建好服务了,不过要修改一下UserAction
现在可以测试一下dubbo服务是否好使.
首先要启动搭建有Zookeeper服务的虚拟机.
打开终端,启动Zookeeper服务
zkServer.sh start
我们还需要做件事情,就是配置防火墙,需要开放相关端口:2181,20880,当然,你也可以把防火墙关闭了.
现在,启动OA-system-service和OA-system-web两个工程的Tomcat
然后,在浏览器中输入http://localhost:8081/OA-system-web/user/userAction_findById.action
可以看到能够跳转到success.jsp,接下来看看控制台是否输出了userService的地址
可以看到userService不为空,说明我们dubbo搭建成功
===============================================================================================
在写博客的时候,可能在项目中有一些问题没有被发现,在我修改后,忘记写到博客上,所以我将这个项目上传到github上,大家可以在github上获取项目的代码
下面是github地址,大家Fork and Star
OA-Reconsitution