0.版本
已有项目的Springboot版本:2.2.6.RELEASE
Dubbo版本:maven配置是0.2.0,源码版本是2.6.2
1.Maven整合已有或新建项目
将多个项目整合成功一个项目多模块,多模块直接需要互相调用,所以都是平级的,既是生产者也是消费者
2.添加依赖
因为各模块已在pom配置了parent为主项目,所以再主项目的pom添加相关依赖即可
<properties>
<curator-framework.version>4.0.1</curator-framework.version>
<zookeeper.version>3.4.13</zookeeper.version>
<dubbo.starter.version>0.2.0</dubbo.starter.version>
</properties>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.starter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
3.yml配置
dubbo:
application:
name: 应用名
protocol:
name: dubbo
# port端口每个项目配成不一样的
port: 20880
registry:
address: zookeeper://zk的IP:2181
provider:
timeout: 1000
# 如果多项目互为生产者消费者,此项需设为false,不然启动时会检查生产者是否启动,先启动的消费者就连不上后启动的生产者
consumer:
check: false
# 自定义dubbo version,用于区分service版本
dv:
service:
version: 1.0.0
4.注解
在启动类添加注解
@EnableDubbo
5.新建公共接口项目
新建一个项目,定义两系统需要交互的接口,我就叫它对外Service吧
接口内容如下,就一个简单的接口
public interface DwService {
String hello(String name);
}
6.生产者者实现类
1.此@Service要用dubbo的,version取了yml中的配置
2.实现类继承公共接口,将接口方法实现
import com.alibaba.dubbo.config.annotation.Service;
import com.***.***api.service.DwService;
@Service(version = "${dv.service.version}")
public class ***DwServiceImpl implements DwService {
@Override
public String hello(String name) {
return "I'm provider ,hello, " + name;
}
}
7.消费者调用类
在消费者端实现调用
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.***.***api.service.DwService;
@RestController
public class ***DwController {
@Reference(version = "${dv.service.version}")
private DwService dwService;
@RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
return dwService.hello(name);
}
}
8.测试
启动两个项目,在生产者页面,修改浏览器地址栏url
http://127.0.0.1:8888/应用名称/hello/生产者
参考:
https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples
https://github.com/apache/dubbo-spring-boot-project