发布dubbo服务
在上一篇文章中,我们已经搭建好dao层,service层和controller层了,所以下面让我们来发布dubbo服务.
在此之前我们需要先把dubbo相关的包给引进来,在OA-system-service工程中添加依赖
<!-- 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>
如此一来,当前taotao-manager-service工程的pom.xml文件的全部内容如下:这儿,大家要注意,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的依赖
<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-service</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</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>
现在我们开始发布服务,我们在OA-system-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: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
">
<!-- 配置spring容器创建时要扫描的包 -->
<context:component-scan base-package="com.QEcode.OA.service.impl"></context:component-scan>
</beans>
如果dubbo约束报错,那么应该是你没在eclipse配置约束.
下面我们在applicationContext-service.xml文件中添加如下配置:
<!-- 使用dubbo发布服务 --> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="OA-system" />
扫描二维码关注公众号,回复:
4250920 查看本文章
<dubbo:registry protocol="zookeeper" address="${dubbo.address}" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.QEcode.OA.service.UserService" ref="userServiceImpl" /> |
其中<dubbo:application name="
OA-system
" />
是用来配置在注册中心的名字,标识我们当前应用的一个名称,你可以随便起,但是最好不要跟其他的应用重复,最好跟你的工程名相对应。
从上面可以看到,${dubbo.address}需要我们在配置文件中配置,所以我们要在resource目录下新建一个resource.properties文件,用以存放一些配置.
resource.properties内容如下:
#dubbo地址 dubbo.address=192.168.43.170:2181 |
至此,我们就发布了一个服务。
===============================================================================================
在写博客的时候,可能在项目中有一些问题没有被发现,在我修改后,忘记写到博客上,所以我将这个项目上传到github上,大家可以在github上获取项目的代码
下面是github地址,大家Fork and Star
OA-Reconsitution