如何基于盤古框架開發(fā)Dubbo微服務(wù)應(yīng)用


本文介紹如何基于盤古開發(fā)框架開發(fā)一個微服務(wù)應(yīng)用。文中所述僅為搭建一個微服務(wù)應(yīng)用的基本框架(服務(wù)注冊&服務(wù)發(fā)現(xiàn)),如要增加配置中心、網(wǎng)關(guān)代理、數(shù)據(jù)持久化、緩存等能力請參考使用指南的相關(guān)章節(jié)。

服務(wù)提供者

安裝相關(guān)盤古模塊

<!-- 盤古 Parent -->
<parent>
    <groupId>com.gitee.pulanos.pangu</groupId>
    <artifactId>pangu-parent</artifactId>
    <version>latest.version.xxx</version>
    <relativePath/>
</parent>
<!-- 基礎(chǔ)模塊 -->
<dependency>
    <groupId>com.gitee.pulanos.pangu</groupId>
    <artifactId>pangu-spring-boot-starter</artifactId>
</dependency>
<!-- Dubbo模塊 -->
<dependency>
    <groupId>com.gitee.pulanos.pangu</groupId>
    <artifactId>pangu-dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- 服務(wù)接口包 -->
<dependency>
    <groupId>com.gitee.pulanos.pangu</groupId>
    <artifactId>pangu-examples-dubbo-api</artifactId>
    <version>1.0.0</version>
</dependency>

盤古框架微服務(wù)交互基于 Dubbo 提供的面向接口代理的高性能 RPC 調(diào)用能力。對于內(nèi)部服務(wù)模塊之間的交互調(diào)用或者是傳統(tǒng)方式的網(wǎng)關(guān)接口調(diào)用,都需要依賴 API 接口包。當(dāng)然,對于ShenYu網(wǎng)關(guān)而言,使用的是泛化調(diào)用不需要依賴接口包,詳見相關(guān)章節(jié)。

本地配置

為便于理解,本文基于本地配置的方式編寫。若改為標(biāo)準(zhǔn)的 Nacos 配置中心模式,請參閱:配置中心 章節(jié)。

spring.application.name=pangu-examples-dubbo-service
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.consumer.timeout=5000
#服務(wù)注冊中心地址
dubbo.registry.address=nacos://${nacos.server-addr}?namespace=${nacos.namespace}
dubbo.consumer.check=false

實現(xiàn)服務(wù)接口

UserEntity findUserEntity(Long id);
@Service(version = "1.0.0", group = "pangu-showcases-dubbo-service")
public class UserServiceImpl implements UserService {
    @Override
    public UserEntity findUserEntity(Long id) {
        log.info("參數(shù)ID:{}", id);
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setName("云南碼農(nóng)大熊");
        return userEntity;
    }
}

啟動入口

@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
    public static void main(String[] args) {
        PanGuApplicationBuilder.init(DubboProviderApplication.class).run(args);
    }
}

通過 @EnableDubbo 注解開啟 Dubbo 支持。由于 Dubbo 的使用 netty 作為底層網(wǎng)絡(luò)通信,決定了盤古微服務(wù)應(yīng)用啟動和提供服務(wù)并不需要依賴 Servlet 容器。

-Dnacos.server-addr=127.0.0.1:8848 -Dnacos.namespace=pangu-dev

服務(wù)注冊

成功啟動應(yīng)用會自動像 Nacos 服務(wù)注冊中心注冊服務(wù)。登錄 Nacos 控制臺即可在【服務(wù)管理->服務(wù)列表】頁查看效果。如下圖所示。


服務(wù)消費者

上述服務(wù)注冊到 Nacos 服務(wù)中心以后就可以對外提供服務(wù)了??梢栽谌魏我粋€ SpringBean 組件中(一般是 Service、Manager 等),引入服務(wù)接口后就像本地接口調(diào)用一樣調(diào)用遠程服務(wù)。Dubbo 將提供高性能的基于代理的遠程調(diào)用能力,服務(wù)以接口為粒度,為開發(fā)者屏蔽遠程調(diào)用底層細(xì)節(jié)。服務(wù)消費端所需要的依賴和提供端是一樣的,這里不再贅述。僅給出消費相關(guān)代碼。如下所示。

@Component
public class UserAdminManager {
    @Reference(version = "1.0.0", group = "pangu-examples-dubbo-service")
    private UserService userService;
    public void findUserEntityById(Long id){
        log.info("開始Dubbo遠程調(diào)用...");
        UserEntity userEntity = userService.findUserEntity(id);
        log.info("[OK] 調(diào)用成功 {}", userEntity);
    }
}

本文相關(guān)范例源碼

下一步

繼續(xù)閱讀其它章節(jié)獲取你想要的答案或通過我們的 開發(fā)者社區(qū) 尋求更多幫助。

參考文獻

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容