dubbo集成于springboot后,只需引入依赖,yml配置
服务提供方
SpringBoot启动类添加 @EnableDubbo
server:
port: 8080 #Tomcat端口
spring:
dubbo:
application:
name: springboot-dubbo-demo #应用名
registry:
address: zookeeper://localhost #zookeeper地址
port: 2181 #提供注册的端口
protocol:
name: dubbo
port: 20890 #dubbo服务暴露的端口
scan: com.springboot.dubbo.service.impl #扫描的包名(扫描到具体的dubbo的serviceImpl类)
在具体提供服务的serviceImpl上加上dubbo的Service注解
@Service(version = "1.0.0",timeout = 3000)
服务消费方
spring:
dubbo:
application:
name: springboot-dubbo-demo
registry:
address: zookeeper://localhost
port: 2181
scan: com.springboot.dubbo.controller
Controller类中的代码结构使用服务注入时使用dubbo的Reference注解:
@Reference(version = "1.0.0",timeout = 300)
private TestService testService;
另外单独做一个api的module,entity和interface都放在里面,服务调用方和消费方都去依赖,就保持了实体类和接口方法都一致。
springcloud中由于使用feign调用,需要调用和服务controller一样的方法参数和返回entity,所以在api模块中,建立抽取出的pojo和从controller中抽取的interface
cloud的消费端单独编写client的接口extends 引入的外部maven模块的xxxApi接口,加上@FeignClient(服务名) 调用,在service引入即可