1、添加依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
2、生产者配置文件
spring:
application:
name: dubbo-provider
demo:
service:
version: 1.0.0
dubbo:
scan:
basePackages: xxx.xxx.xxx #DemoServiceImpl所在的包路径
application:
id: dubbo-provider
name: dubbo-provider
protocol:
id: dubbo
name: dubbo
port: 20880
status: server
registry:
id: my-registry
address: zookeeper://xxx.xxx.xx.xx:2181
timeout: 100000
3、消费者配置文件
spring:
application:
name: dubbo-consumer
demo:
service:
version: 1.0.0
dubbo:
application:
id: dubbo-consumer
name: dubbo-consumer
protocol:
id: dubbo
name: dubbo
port: 20880
registry:
address: zookeeper://xxx.xx.xxx.xx:2181
timeout: 100000
4、定义service接口
public interface DemoService {
String sayHello(String name);
}
5、生产者实现service接口
@Service(version = "${demo.service.version}",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}"
)
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
}
6 、消费者调用service
@RestController
public class DemoConsumerController {
@Reference(version = "${demo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://localhost:20880")
private DemoService demoService;
@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name) {
return demoService.sayHello(name);
}
}
7、zookeeper集群时
配置改为
dubbo:
registry:
protocol: zookeeper
address: ip:2181,ip:2182,ip:2183
8、源码地址
https://gitee.com/hsshy/beam-parent
参考链接:
1.[https://github.com/apache/incubator-dubbo-spring-boot-project][1]
[1]: https://github.com/apache/incubator-dubbo-spring-boot-project