据阿里开发团队宣布,Dubbo Spring Boot Starter 1.0.0 公测版已开发完毕,即将发布至 Maven 公有仓库,目前正在内部测试中。
Dubbo Spring Boot Starter 致力于简化 Dubbo 应用在 Spring Boot 环境中的开发,主要包括自动装配(Auto-Configure)、外部化配置(Externalized-Configuration)以及生产准备(Actuator)等。
版本依赖
特别提醒,以下版本依赖尚未发布到 Maven 公有仓库,请自行 checkout 代码到本地,并且通过命令 $ ./mvnw clean install
构建:
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
同时,dubbo-spring-boot-starter 底层依赖 Dubbo 2.5.9。
快速上手
服务接口(RPC)
public interface DemoService { String sayHello(String name); }
服务提供者(Provider)
实现DemoServer
接口提供服务(放置于 com.alibaba.boot.dubbo.demo.provider.service
包下):
@Service( version = "1.0.0", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}" ) public class DefaultDemoService implements DemoService { public String sayHello(String name) { return "Hello, " + name + " (from Spring Boot)"; } }
配置application.properties
以提供外部化配置源:
# Spring boot application spring.application.name = dubbo-provider-demo server.port = 9090 management.port = 9091 # Dubbo 组件 (如 @Service , @Reference) 扫描路径,多路径以","分割 dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service # Dubbo Config Bean 外部化配置 dubbo.application.id = dubbo-provider-demo dubbo.application.name = dubbo-provider-demo dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 12345 dubbo.registry.id = my-registry
提供服务提供者引导类:
@SpringBootApplication public class DubboProviderDemo { public static void main(String[] args) { SpringApplication.run(DubboProviderDemo.class,args); } }
服务消费者(Consumer)
实现服务消费者 Controller
(位于com.alibaba.boot.dubbo.demo.consumer.controller
包下):
@RestController public class DemoConsumerController { @Reference(version = "1.0.0", application = "${dubbo.application.id}", url = "dubbo://localhost:12345") private DemoService demoService; @RequestMapping("/sayHello") public String sayHello(@RequestParam String name) { return demoService.sayHello(name); } }
增加外部化配置至application.properties
:
# Spring boot application spring.application.name = dubbo-consumer-demo server.port = 8080 management.port = 8081 # Dubbo Config Bean 外部化配置 dubbo.application.id = dubbo-consumer-demo dubbo.application.name = dubbo-consumer-demo dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 12345
提供服务消费者引导类:
@SpringBootApplication(scanBasePackages = "com.alibaba.boot.dubbo.demo.consumer.controller") public class DubboConsumerDemo { public static void main(String[] args) { SpringApplication.run(DubboConsumerDemo.class,args); } }
转自 http://www.oschina.net/news/92687/dubbo-spring-boot-starter-1-0-0