使用的SpringBoot的版本是
2.1.3.RELEASE
首先添加Dubbo相关依赖
<!--引入dubbo的依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.4</version>
<optional>true</optional>
</dependency>
<!-- 引入zookeeper的依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
然后新建项目-api项目-该项目需要一个接口即可,然后使用maven命令达成jar包,安装到本地仓库中
package com.voole.service; public interface IndexService { public String index(String message); }
新建生产者项目,添加dubbo相关依赖,
添加api包的依赖,添加接口实现类
package com.voole.platform.app; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; import com.voole.service.IndexService; @Component @Service(version="1.0.0",interfaceClass=IndexService.class) public class IndexServiceImpl implements IndexService { @Override public String index(String message) { System.out.println(message); return message+"-yes"; } }
启动类中添加注解信息
@EnableDubbo
配置文件中添加配置信息
dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 dubbo.registry.id=dubbo-provider dubbo.application.name=dubbo-provider dubbo.scan.base-packages=com.voole.business dubbo.protocol.port=20882 dubbo.protocol.name=dubbo dubbo.protocol.status=server dubbo.protocol.id=dubbo
新建消费者项目,添加dubbo依赖,api项目包依赖
配置文件中添加配置信息
# dubbo config dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 dubbo.registry.id=dubbo-consumer dubbo.application.name=dubbo-consumer
启动类中添加相关注解信息
@EnableDubbo
@DubboComponentScan(basePackages= {"com.voole.service"})
之后在想调用的地方添加
@Reference(check=false) private IndexService indexService;
就可以使用了。(zk用的是本地,测试的时候记得启动zk)