一步步完成Maven+Spring+Dubbo+Zookeeper的整合示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一步步完成Maven+Spring+Dubbo+Zookeeper的整合示例相关的知识,希望对你有一定的参考价值。
本文给出一个整合Maven+Spring+Dubbo+Zookeeper的示例,并且一步步给出完成步骤,并对其中可能遇到的问题进行解决~
Maven模块化
创建一个名为dubbo-demo的Maven工程,该工程包含三个模块
- dubbo-demo-interface (定义对外开放接口)
- dubbo-demo-provider (接口实现)
- dubbo-demo-consumer (接口调用)
dubbo-demo工程中的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>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-demo-provider</module>
<module>dubbo-demo-consumer</module>
<module>dubbo-demo-interface</module>
</modules>
</project>
关于dubbo-demo Maven工程的模块化构建,请参考博文【使用Maven构建模块化工程】
接下来,我们就一步一步来完成interface、provider以及consumer模块的内容~
模块interface
定义接口
创建一个接口(GreetingService),包含一个hello接口,如:
package com.xxx.dubbo.demo.service;
/**
* @author wangmengjun
*
*/
public interface GreetingService {
String hello(String name);
}
简单的接口就定义好了~
有了接口,就可以在Provider中完成接口的实现,并暴露接口服务为Dubbo服务给其它模块使用(如consumer)~
模块Provider
添加依赖包
- 添加interface依赖包
<dependency>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 添加spring依赖包
<!--引入Spring依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
- 添加dubbo依赖包
<!-- Dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4-snapshot</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
</dependency>
- 添加zk-client依赖包
<!-- ZK-client -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
dubbo-demo-provider模块详细的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.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-demo-provider</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.framework.version>4.3.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
<dependency>