一直想弄一个dubbo+springboot的demon,网上搜了不少教程总是不尽人意,要么就是需要xml配置,要么就是版本太低,尤其是各种版本充斥期间,作为一个初学者,感觉很乱。最近整出一个demo(属于非常基础的~~~~),记录下来:
回头看dubbo的github已经很详细了~~~~~
暂不使用注册中心,使用dubbo的url直连,有时间添加注册中心(zookeeper或者nacos)
参考官方项目:
https://github.com/alibaba/dubbo-spring-boot-starter
1,版本:
springboot :2.1.6
dubbo-spring-boot-starter:2.7.1
jdk:1.8
2,搭建服务提供者:
项目结构:
pom.xml 如下:
application.properties 配置如下:
spring.application.name=dubbo-auto-configuration-provider-demo
#扫描服务实现的包路径
dubbo.scan.base-packages=com.example.demo.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
#不使用注册中心通过url直接连接
dubbo.registry.address=N/A
springboot启动类:
service类:
注意:@Service使用:org.apache.dubbo.config.annotation.Service 原dubbo(com.alibaba.dubbo.config.annotation.Service)因为已搬迁至apache原注解不推荐使用
3,搭建服务消费者:
项目结构:(要建一个和服务提供这一样的接口类)
pom.xml :
application.properties 配置如下:不需要指定通信协议(采用直连url已指定)
spring.application.name=dubbo-auto-configure-consumer-sample
测试:
先启动 服务提供者,再启动消费者:
浏览器 http://localhost:8080/hello: 调用消费者,消费者通过 helloService.say(applicationName); 调用服务提供者:
调用成功,以上。
项目地址:
https://github.com/990753707/dubbo-p
https://github.com/990753707/dubbo-c
补充nacos作为注册中心:
https://blog.csdn.net/u010890632/article/details/98643516