今天小馬哥很是高興地向各位小夥伴宣佈,官方 Dubbo Spring Boot Starter 1.0.0 公測版已開發完畢,即將發佈至 Maven 公有倉庫,目前正在內部測試中。java
爲了收集更多的用戶反饋,小馬哥誠邀你們一同參與使用、測試以及共同維護,項目工程地址:https://github.com/dubbo/dubb...
若是您喜好並想了解 Dubbo Spring Boot 工程的動態,不妨點擊 GitHub Star 按鈕加以關注。git
若有任何意見和建議,請將反饋內容發送於此:https://github.com/dubbo/dubb...github
瞭解更多計劃和細節,請登陸 Dubbo Google Group 參與討論。spring
Dubbo Spring Boot Starter 致力於簡化 Dubbo 應用在 Spring Boot 環境中的開發,主要包括自動裝配(Auto-Configure)、外部化配置(Externalized-Configuration)以及生產準備(Actuator)編程
特別提醒,如下版本依賴還沒有發佈到 Maven 公有倉庫,請自行 checkout 代碼到本地,而且經過命令$ ./mvnw clean install
構建:segmentfault
<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
。app
public interface DemoService { String sayHello(String name); }
實現DemoServer
接口提供服務(放置於 com.alibaba.boot.dubbo.demo.provider.service
包下):ide
@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
# 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); } }
實現服務消費者 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); } }
詳情請參考官方示例代碼:https://github.com/dubbo/dubb...