Dubbo
官方文档中提到有三种方式来开发Dubbo
的应用程序,分别是
- 基于XML配置实现
- 继续注解实现(常用 / 推荐)
- 基于API实现
鉴于我们平时开发过程中用Spring Boot
比较多,所以本篇博客以Spring Boot
为例,整合Dubbo来构建一个简单的分布式服务。
前期准备
本文用的注册中心是zookeeper,所以需要下载并安装zookeeper。安装zookeeper的示例网上有很多了,也很简单,自行安装就好啦。启动zookeeper之后如下图:
项目结构
整个项目结构也很简单,如下所示:
dubbo-springboot-api
只定义了简单的接口。dubbo-springboot-provider
服务提供者。dubbo-springboot-consumer
服务消费者。
这三个子项目我创建的都是Spring Boot
程序。但其实dubbo-springboot-provider
和dubbo-springboot-consumer
创建成Spring Boot
程序就足够用了。
创建一个空的maven项目,命名为dubbo-springboot
<?xml version="1.0" encoding="UTF-8"?>
<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.bazooka</groupId>
<artifactId>dubbo-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-springboot-api</module>
<module>dubbo-springboot-consumer</module>
<module>dubbo-springboot-provider</module>
</modules>
</project>
创建dubbo-springboot-api
子项目
整个子项目其实什么也没有做,只是单纯的定义一个接口。
package com.bazooka.api.service;
public interface HelloDubboService {
String sayHello(String name);
}
创建dubbo-springboot-provider
子项目
这个子项目主要是以服务提供者
的身份编写HelloDubboService
的实现,来供服务消费者远程调用。
- pom如下
<dependencies>
<!-- 引入spring-boot-starter以及dubbo和curator的依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.bazooka</groupId>
<artifactId>dubbo-springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
- 编写服务提供者的实现
@Service
@Component
public class HelloDubboServiceImpl implements HelloDubboService {
@Override
public String sayHello(String name) {
return "欢迎" + name + "使用Dubbo。";
}
}
这里需要注意的是@Service
是Dubbo提供的注解,而不是Spring提供的注解。这个注解的意思主要是用来暴露服务的。
- 修改application.properties
#当前服务/应用的名字
dubbo.application.name=dubbo-springboot-provider
#注册中心的协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
#通信规则(通信协议和接口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心
dubbo.monitor.protocol=registry
- 添加
@EnableDubbo
注解
这个注解的含义主要是来扫描包,实现Dubbo的自动装配,相当于配置dubbo.scan.base-packages
。
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
一切就绪之后启动ProviderApplication
。
创建dubbo-springboot-consumer
子项目
这个子项目主要是来远程调用服务提供者的实现。
- pom保持一致就可以啦。
- 定义一个
ConsumerService
并实现它。- 这里的
@Service
是import org.springframework.stereotype.Service
@Reference
来代替@Autowired
- 这里的
@Service
public class ConsumerServiceImpl implements ConsumerService {
@Reference
HelloDubboService helloDubboService;
@Override
public String sayHello(String message) {
return helloDubboService.sayHello(message);
}
}
- 修改
application.properties
server.port=8081
dubbo.application.name=order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
- 同样的需要在main方法上添加
@EnableDubbo
注解
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- 创建一个
HelloDubboController
如下:
@RestController
public class HelloDubboController {
@Autowired
ConsumerService consumerService;
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name) {
return consumerService.sayHello(name);
}
}
一切就绪之后启动ConsumerApplication
查看测试结果
看到结果是我预期的结果,证明项目没什么问题哈。