基于Netty实现的轻量级分布式服务框架

news/2024/6/27 22:14:56

对分布式技术比较感兴趣,于是在闲暇时间写了一个简单的RPC框架娱乐一下,项目持续更新中......

GitHub项目地址: Pudding

如果感觉Pudding对你有帮助可以顺手点个Star哦......哈哈

直接看一下示例代码吧

第一步: 启动注册中心

public class RegistryTest {public static void main(String[] args) {// 创建服务注册中心ServiceRegistry registry = new DefaultServiceRegistry();// 启动注册中心registry.startRegistry(20000);}
}

第二步: 启动服务提供者

public class ProviderTest {public static void main(String[] args) {// 创建服务提供者和服务包装器ServiceProvider provider = new DefaultServiceProvider();ServiceWrapper serviceWrapper = new DefaultServiceWrapper();// 连接注册中心provider.connectRegistry("127.0.0.1:20000");// 创建服务MyService myService = new MyServiceImpl();// 包装服务为ServiceMetaService service = serviceWrapper.build(myService, "127.0.0.1:30001");// 启用服务provider.startService(service);// 发布所有已启用的服务provider.publishAllService();}
}

第三步: 启动服务消费者

同步调用

public class SyncConsumerTest {public static void main(String[] args) {// 创建服务消费者ServiceConsumer serviceConsumer = new DefaultServiceConsumer();// 连接注册中心serviceConsumer.connectRegistry("127.0.0.1:20000");// 订阅服务serviceConsumer.subscribeService(MyService.class);// 创建同步服务代理MyService myService = ProxyFactory.createSyncProxy(MyService.class);try {// 发起调用int result = myService.add(100, 200);System.out.println("调用结果: " + result);} catch (InvokeTimeoutException e) {System.out.println("远程调用超时");} catch (InvokeFailedException e) {System.out.println("远程调用失败");}}
}

异步调用

public class AsyncConsumerTest {public static void main(String[] args) {// 创建服务消费者ServiceConsumer serviceConsumer = new DefaultServiceConsumer();// 连接注册中心serviceConsumer.connectRegistry("127.0.0.1:20000");// 订阅服务serviceConsumer.subscribeService(MyService.class);// 创建同步服务代理MyService myService = ProxyFactory.createAsyncProxy(MyService.class);// 发起调用myService.add(100, 200);// 监听调用结果InvokeFuture.addInvokeFutureListener(new InvokeFutureListener<Integer>() {@Overridepublic void success(Integer result) {System.out.println("调用结果: " + result);}@Overridepublic void failure(Exception e) {e.printStackTrace();}});}
}

http://lihuaxi.xjx100.cn/news/252719.html

相关文章

BlackBerry携手大音科技打造声学解决方案,赋能长城汽车新一代高端车型

2022年9月29日&#xff0c;BlackBerry与上海大音信息科技有限公司今日宣布&#xff0c;基于BlackBerry QNX声学技术提供支持的大音科技——“音”2.0 应用程序&#xff0c;已在长城汽车旗下高端品牌魏牌的摩卡、拿铁和玛奇朵系列车型的智能座舱系统中实现了部署。 大音科技的“…

单点登录

1.服务器端安装IDM的WebGate插件&#xff0c;已登录用户的认证信息加载在Http Header中&#xff0c;浏览请求时即可从Request请求中获取该用户的相关信息。 相当于接口传入账号信息。 2.取到接口道本系统中&#xff0c;判断账号信息。 转载于:https://www.cnblogs.com/yokiblog…

CQRS实践(3): Command执行结果的返回

上篇随笔讨论了CQRS中Command的一种基本实现。 面对UI中的各种命令&#xff0c;Controller会创建相应的Command对象&#xff0c;然后将其交给CommandBus&#xff0c;由CommandBus统一派发到相应的CommandExecutor中去执行&#xff0c;我们的ICommandBus的接口声明如下: public …

【组队学习】【28期】基于Python的会员数据化运营

基于Python的会员数据化运营 论坛版块&#xff1a; http://datawhale.club/c/team-learning/37-category/37 开源内容&#xff1a; https://github.com/datawhalechina/team-learning-data-mining/tree/master/MemberOperations 学习目标 数据化运营是业务知识与编程技能…

centos安装及网络配置

感谢老师传授&#xff0c;共同学习&#xff01;谢谢&#xff01;仅供自己日后复习之用&#xff01;centos安装关键点&#xff1a;创建分区&#xff1a;/ 系统分区/boot 启动分区SWAP 交换分区&#xff0c;虚拟内存。主要是缓解物理内存不足。虚拟化软件&#xff1a;VMware work…

【组队学习】【28期】基于transformers的自然语言处理(NLP)入门

基于transformers的自然语言处理(NLP)入门 论坛版块&#xff1a; http://datawhale.club/c/team-learning/39-category/39 开源内容&#xff1a; https://github.com/datawhalechina/Learn-NLP-with-Transformers 学习目标 自然语言处理&#xff08;Natural Language Pro…

IIS负载均衡-Application Request Route详解第三篇:使用ARR进行Http请求的负载均衡(上)...

IIS负载均衡-Application Request Route详解第三篇&#xff1a;使用ARR进行Http请求的负载均衡&#xff08;上&#xff09; 在前两篇文章中&#xff0c;我们已经讲述如何配置与安装ARR&#xff0c;从本篇文章开始&#xff0c;我们将重点的来讲述如何在使用ARR进行负载均衡。 本…

MongoDB 学习使用

博客教程&#xff1a; https://jingyan.baidu.com/article/dca1fa6f0428a4f1a440522e.html转载于:https://www.cnblogs.com/harlem/p/10148315.html