公告
今天小馬哥非常高興地向各位小夥伴宣佈,官方 Dubbo Spring Boot Starter 1.0.0 公測版已開發完畢,即將釋出至 Maven 公有倉庫,目前正在內部測試中。
為了收集更多的使用者反饋,小馬哥誠邀大家一同參與使用、測試以及共同維護,專案工程地址:https://github.com/dubbo/dubb…
如果您喜愛並想了解 Dubbo Spring Boot 工程的動態,不妨點選 GitHub Star 按鈕加以關注。
如有任何意見和建議,請將反饋內容傳送於此:https://github.com/dubbo/dubb…
瞭解更多計劃和細節,請登入 Dubbo Google Group 參與討論。
主要特性
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);
}
}
詳情請參考官方示例程式碼:https://github.com/dubbo/dubb…