## SpringBoot配合Dubbo,使用@Service和@Reference,group实现接口多实现
---
公司项目升级,需要实现springBoot + Dubbo,并支持一个接口多个实现的情况。遇到了几个坑,在这里记录下。
### 1. 安装Zookeeper
在 [官网](https://archive.apache.org/dist/zookeeper/zookeeper-3.5.6/) 上下载最新版本3.5.6(注意下载 bin 包)
> 1. 将下载好的压缩包,解压到对应目录
> 2. cd apache-zookeeper-3.5.6-bin/conf/ // 切换到配置目录
> 3. mv zoo_sample.cfg zoo.cfg //更改默认配置文件名称
> 4. vi zoo.cfg // 编辑配置文件,自定义dataDir,我的配置贴在下面
这里会遇到第一个坑:zk从3.5.5开始,带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包,而tar.gz的包里面是只是源码,无法直接使用。不小心下了 apache-zookeeper-3.5.6.tar.gz 这个包,启动时会报错"错误: 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain"。
###### zoo.cfg
```
# 发送心跳的间隔时间, 毫秒为单位
tickTime=2000
# 集群用到,初始化连接时,leader最多能容忍client多少个心跳间隔,默认是10,就代表10*2000=20000 , 毫秒
initLimit=10
# 集群用到,正常通信时,如果client超过此时间间隔没有心跳
syncLimit=5
# 保存数据的目录
dataDir=/Users/ouitsu/document/apache-zookeeper-3.5.6-bin/data
# 暴露的接口
clientPort=2181
# zk3.5的新特性,Zookeeper AdminServer,默认端口是8080,坑的一批
admin.serverPort=12181
```
这里是第二个坑,zk的Zookeeper AdminServer默认端口和Dubbo admin默认端口冲突,会报错:Unable to start AdminServer, exiting abnormally org.apache.zookeeper.server,在这里定义端口即可解决。
配置完成,开始启动zk
> 1. cd到zookeeper的bin目录下
> 2. ./zkServer.sh start 启动
> 3. ./zkServer.sh stop 停止
### 开始搭建项目
#### 1. 生产者
目录结构如下:

###### provider_pom文件如下
```xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.0.RELEASE com.uaepay.pay.channel dubbo_provider 0.0.1-SNAPSHOT dubbo_provider Demo project for Spring Boot 1.8 com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 org.springframework.boot spring-boot-devtools runtime true uaepay.cmf.channel dubbo_facade 1.0-SNAPSHOT org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine io.projectreactor reactor-test test com.101tec zkclient 0.10 org.apache.zookeeper zookeeper 3.4.12 org.springframework.boot spring-boot-starter-web 2.2.0.RELEASE org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone
```
###### provider_application:
```java
package com.uaepay.pay.channel.dubbo_provider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo //启动dubbo的配置
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
```
###### 两个生产实现
```java
package com.uaepay.pay.channel.dubbo_provider.service;
import com.alibaba.dubbo.config.annotation.Service; // 这里要注意引用dubbo的Service
import org.springframework.stereotype.Component; // 对应的,使用spring的Component
import uaepay.cmf.channel.MockResponseService;
import uaepay.cmf.channel.vo.User;
@Service(group = "mockResponseServiceImpl") // 定义group
@Component
public class MockResponseServiceImpl implements MockResponseService {
@Override
public User getUserInfo() {
User user = new User();
user.setUserName("1111");
return user;
}
}
```
```java
package com.uaepay.pay.channel.dubbo_provider.service;
import com.alibaba.dubbo.config.annotation.Service;// 使用alibaba.dubbo的Service注解
import org.springframework.stereotype.Component;// 使用Spring的Component注解
import uaepay.cmf.channel.MockResponseService;
import uaepay.cmf.channel.vo.User;
@Service(group = "mockResponseServiceImpl2") //定义group
@Component
public class MockResponseServiceImpl2 implements MockResponseService {
@Override
public User getUserInfo() {
User user = new User();
user.setUserName("222222");
return user;
}
}
```
###### application.properties
```properties
dubbo.application.name=dubbo-provider-service
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.port=20782
dubbo.protocol.dispatcher=message
dubbo.protocol.threadpool=fixed
dubbo.protocol.threads=200
server.port=8871
```
####2. 消费者
目录结构如下:

###### pom文件
```xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.0.RELEASE com.uaepay.pay.channel dubbo_consumer 0.0.1-SNAPSHOT dubbo_consumer Demo project for Spring Boot 1.8 com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 org.springframework.boot spring-boot-devtools runtime true uaepay.cmf.channel dubbo_facade 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine com.101tec zkclient 0.10 org.apache.zookeeper zookeeper 3.4.12 org.springframework.boot spring-boot-starter-web 2.2.0.RELEASE org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine com.alibaba fastjson 1.2.28 org.springframework.boot spring-boot-maven-plugin
```
###### ConsumerApplication:
```Java
package com.uaepay.pay.channel.dubbo_consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
```
###### 消费者的实现类
```java
package com.uaepay.pay.channel.dubbo_consumer.service;
import com.alibaba.dubbo.config.annotation.Reference;// 使用Dubbo的reference 获取对应实现
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uaepay.cmf.channel.MockResponseService;
import java.util.LinkedHashMap;
import java.util.TreeMap;
@RestController
@RequestMapping("/test")
public class MockServiceImpl {
@Reference(group = "mockResponseServiceImpl")//对应定义group
private MockResponseService mockResponseServiceImpl;
@Reference(group = "mockResponseServiceImpl2")// 对应定义group
private MockResponseService mockResponseServiceImpl2;
@RequestMapping("/getUserInfo")
public void getUserInfo(){
System.out.println("得到的详情为..."+JSON.toJSONString(mockResponseServiceImpl.getUserInfo()));
}
@RequestMapping("/getUserInfo1")
public void getUserInfo11(){
System.out.println("得到的详情为..."+JSON.toJSONString(mockResponseServiceImpl2.getUserInfo()));
}
}
```
###### application.properties
```properties
dubbo.application.name=dubbo-consumer-service
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.check=false
server.port=8862
```
#### facade包
项目结构如下:

###### pom文件
```xml
4.0.0 uaepay.cmf.channel dubbo_facade 1.0-SNAPSHOT dubbo_facade http://www.example.com UTF-8 1.7 1.7 junit junit 4.11 test org.projectlombok lombok true commons-lang commons-lang 2.4 maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-jar-plugin 3.0.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-site-plugin 3.7.1 maven-project-info-reports-plugin 3.0.0
```
###### 实体类
```Java
package uaepay.cmf.channel.vo;
import java.io.Serializable;
public class User implements Serializable {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
```
#### 接口
```java
package uaepay.cmf.channel;
import uaepay.cmf.channel.vo.User;
public interface MockResponseService {
public User getUserInfo();
}
```
### 运行项目
#### 启动provider
```
Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100000dd3c5000c, negotiated timeout = 30000
```
成功打印出这些,说明生产者已经注册成功,如果未出现,检查一下是不是没引入com.101tec.zkclient的依赖,或者zk是否成功启动。
#### 启动Consumer
```
has been built.
has been built.
```
成功打印这个,证明消费者配置成功。
如果打印的是
来源:https://www.cnblogs.com/metabolism/p/11767156.html