1 创建一个3 maven项目
1 定义接口项目 service
2 服务的提供者 provider
3 服务消费者 consumer
service 项目中只定义接口 不做其他任何事情
2 我们在该项目中创建一个接口 ,随便写一个方法
3 我们到 provider 项目中 pom.xml中引入相关包
在引入dubbo包的时候注意 为了防止spring冲突 要将spring依赖移除掉
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
我们需要实现service项目中的接口 所以也要引入service依赖
4 写一个实现类 来实现接口中的hello方法
5 我们再创建一个源文件夹来存放配置文件 src/main/resource
6 在源文件夹下创建目录 META-INF META-INF下再创建spring (必须叫这个名字)
7 创建两个xml文件 分别配置spring和dubbo
applicationContext.xml
applicationContext-dubbo.xml
8 在applicationContext.xml中 扫描 实现类所在的包
<context:component-scan base-package="com.dubbo.provider"></context:component-scan>
想被扫描到 实现类中也必须加入@Service注解
9 在applicationContext-dubbo.xml 中配置
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="wy_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 端口是zookeeper 中配置的2181-->
<dubbo:registry address="zookeeper://192.168.80.141:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 具体的实现bean -->
<bean id="testImpl" class="com.dubbo.provider.TestImpl" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.dubbo.service.Itest" ref="testImpl" />
10 写一个带有main方法的类
public static void main(String[] args) {
// TODO Auto-generated method stub
Main.main(args);
}
11 成功