新闻、帮助、产品更新动态

最新的业界新闻,产品系统更新开发动态,帮助教程和活动发布

如何基于盘古框架开发Dubbo微服务应用

发布日:2022-05-22 14:01       阅读数:

本文介绍如何基于盘古开发框架开发一个微服务应用。文中所述仅为搭建一个微服务应用的基本框架(服务注册&服务发现),如要增加配置中心、网关代理、数据持久化、缓存等能力请参考使用指南的相关章节。
 
服务提供者
安装相关盘古模块

<!-- 盘古 Parent -->
<parent>
	<groupId>com.gitee.pulanos.pangu</groupId>
	<artifactId>pangu-parent</artifactId>
	<version>latest.version.xxx</version>
	<relativePath/>
</parent>
<!-- 基础模块 -->
<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>
<!-- 服务接口包 -->
<dependency>
	<groupId>com.gitee.pulanos.pangu</groupId>
	<artifactId>pangu-examples-dubbo-api</artifactId>
    <version>1.0.0</version>
</dependency>
盘古框架微服务交互基于 Dubbo 提供的面向接口代理的高性能 RPC 调用能力。对于内部服务模块之间的交互调用或者是传统方式的网关接口调用,都需要依赖 API 接口包。当然,对于ShenYu网关而言,使用的是泛化调用不需要依赖接口包,详见相关章节。
 
本地配置
为便于理解,本文基于本地配置的方式编写。若改为标准的 Nacos 配置中心模式,请参阅:配置中心 章节。spring.application.name=pangu-examples-dubbo-service
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.consumer.timeout=5000
#服务注册中心地址
dubbo.registry.address=nacos://${nacos.server-addr}?namespace=${nacos.namespace}
dubbo.consumer.check=false实现服务接口
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("参数ID:{}", id);
		UserEntity userEntity = new UserEntity();
		userEntity.setId(id);
		userEntity.setName("云南码农大熊");
		return userEntity;
	}
}

启动入口

@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
	public static void main(String[] args) {
		PanGuApplicationBuilder.init(DubboProviderApplication.class).run(args);
	}
}
通过 @EnableDubbo 注解开启 Dubbo 支持。由于 Dubbo 的使用 netty 作为底层网络通信,决定了盘古微服务应用启动和提供服务并不需要依赖 Servlet 容器。

 

-Dnacos.server-addr=127.0.0.1:8848 -Dnacos.namespace=pangu-dev
服务注册
成功启动应用会自动像 Nacos 服务注册中心注册服务。登录 Nacos 控制台即可在【服务管理->服务列表】页查看效果。如下图所示。

服务消费者
上述服务注册到 Nacos 服务中心以后就可以对外提供服务了。可以在任何一个 SpringBean 组件中(一般是 Service、Manager 等),引入服务接口后就像本地接口调用一样调用远程服务。Dubbo 将提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。服务消费端所需要的依赖和提供端是一样的,这里不再赘述。仅给出消费相关代码。如下所示。
@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远程调用...");
		UserEntity userEntity = userService.findUserEntity(id);
		log.info("[OK] 调用成功 {}", userEntity);
	}
}
本文相关范例源码
pangu-examples-dubbo-api:服务接口包
pangu-examples-dubbo-service:服务提供者
pangu-examples-dubbo-consumer:服务消费者

编辑:航网科技   来源:腾讯云

本文版权归原作者所有 转载请注明出处

联系我们

客服部:深圳市龙华区龙胜商业大厦5楼B5区

业务部:深圳市南山区讯美科技广场2栋12楼1202

资质证书

  • Copyright © 2011-2020 www.hangw.com. All Rights Reserved 深圳航网科技有限公司 版权所有 增值电信业务经营许可证:粤B2-20201122 - 粤ICP备14085080号

    在线客服

    微信扫一扫咨询客服


    全国免费服务热线
    0755-36300002

    返回顶部