註解配置
本文使用 Spring Boot + 註解模式說明 Dubbo 應用程式開發。在此處查看不使用 Spring Boot 的 Spring 註解開發模式 [完整範例](https://github.com/apache/dubbo-samples/tree/master/1-basic/ dubbo-samples-annotation)
在 Dubbo Spring Boot 開發中,您只需新增幾個註解並配置 application.properties
或 application.yml
檔案即可完成 Dubbo 服務定義
- 註解包含
@DubboService
、@DubboReference
和EnableDubbo
。其中,@DubboService
和@DubboReference
用於標記 Dubbo 服務,EnableDubbo
啟動 Dubbo 相關配置並指定 Spring Boot 掃描套件路徑。 - 設定檔
application.properties
或application.yml
以下內容的完整範例,請參考 dubbo-samples
新增 Maven 依賴項
使用 Dubbo Spring Boot Starter,首先引入以下 Maven 依賴項
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Zookeeper -->
<!-- NOTICE: Dubbo only provides dependency management module for Zookeeper, add Nacos or other product dependency directly if you want to use them. -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
然後將其新增到對應模組的 pom 中
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<!-- dubbo starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- spring starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
區分上面 ** 和 **
(Note: This needs more context to translate accurately. It likely refers to two different code snippets or configurations. Please provide the missing context.)
application.yml 或 application.properties
除了 service 和 reference 之外的元件都可以在 application.yml 檔案中設定。如果要擴展 service 或 reference 的註解配置,需要新增 dubbo.properties
配置檔或使用其他非註解方式,例如 Java Config。詳情請見下面的[擴展註解配置](#擴展註解配置)。
服務組件和引用組件也可以通過 id
與應用程式中的全域組件關聯,以下列配置為例
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: -1
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
通過註解將服務與上面定義的特定註冊中心關聯
@DubboService(registry="zk-registry")
public class DemoServiceImpl implements DemoService {}
通過 Java Config 配置關聯也是如此
@Configuration
public class ProviderConfiguration {
@Bean
public ServiceConfig demoService() {
ServiceConfig service = new ServiceConfig();
service.setRegistry("zk-registry");
return service;
}
}
註解配置
@DubboService 註解
@Service
註解自 3.0 版本起已棄用,請使用@DubboService
以區別於 Spring 的@Service
註解
定義 Dubbo 服務介面後,提供服務介面的實現邏輯,並使用 @DubboService
註解標記,即可實現 Dubbo 的服務暴露
@DubboService
public class DemoServiceImpl implements DemoService {}
如果要設定服務參數,@DubboService
也提供了一種設定常用參數的方式。如果有更複雜的參數設定需求,可以考慮使用其他設定方法
@DubboService(version = "1.0.0", group = "dev", timeout = 5000)
public class DemoServiceImpl implements DemoService {}
@DubboReference 註解
@Reference
註解自 3.0 版本起已棄用,請使用@DubboReference
以區別於 Spring 的@Reference
註解
@Component
public class DemoClient {
@DubboReference
private DemoService demoService;
}
@DubboReference
註解會被自動注入為一個 Dubbo 服務代理實例,可以使用 demoService 發起遠端服務呼叫
@EnableDubbo 註解
必須配置 @EnableDubbo
註解,否則 Dubbo 註解定義的服務將不會被載入,@EnableDubbo
可以定義在主類別上
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProviderApplication.class, args);
}
}
Spring Boot 註解預設只會掃描主類別所在的套件。如果服務定義在其他套件中,則需要新增 EnableDubbo(scanBasePackages = {"org.apache.dubbo.springboot.demo.provider"})
的配置
擴展註解配置
雖然可以通過 @DubboService
和 @DubboReference
調整配置參數(如下方程式碼片段所示),但總體而言,註解提供的配置項仍然十分有限。這種情況下,如果存在更複雜的參數設定需求,可以使用 Java Config
或 dubbo.properties
兩種方式。
@DubboService(version = "1.0.0", group = "dev", timeout = 5000)
@DubboReference(version = "1.0.0", group = "dev", timeout = 5000)
使用 Java Config 取代註解
需要注意的是,Java Config 是 @DubboService
或 @DubboReference
的替代方案,建議用於配置需求複雜的服務。
@Configuration
public class ProviderConfiguration {
@Bean
public ServiceConfig demoService() {
ServiceConfig service = new ServiceConfig();
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
service.setGroup("dev");
service.setVersion("1.0.0");
Map<String, String> parameters = new HashMap<>();
service. setParameters(parameters);
return service;
}
}
通過 dubbo.properties 補充配置
對於使用 @DubboService
或 @DubboReference
的場景,可以使用 dubbo.properties 作為配置的補充,[具體格式](../principle/#1-configuration format) 在這裡有更詳細的說明。
dubbo.service.org.apache.dubbo.springboot.demo.DemoService.timeout=5000
dubbo.service.org.apache.dubbo.springboot.demo.DemoService.parameters=[{myKey:myValue},{anotherKey:anotherValue}]
dubbo.reference.org.apache.dubbo.springboot.demo.DemoService.timeout=6000
屬性格式配置目前結構性不強,例如 key 字段較為冗餘,未來會考慮支援 yaml 格式。