当前位置: 首页 > news >正文

实现RPC接口的demo记录

1.Thrift RPC 接口实现 Demo

@Service
public class DemoServiceImpl implements DemoService.Iface {private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);@Overridepublic String sayHello(Context context, String msg) throws TException {logger.info("接收到 RPC 请求, msg: {}", msg);return "Hello " + msg;}
}

2.Thrift RPC 配置 Demo

@Configuration
public class DemoThriftConfiguration {@Value("${rpc.server.port:9090}")private int port;/*** 定义 Thrift Server*/@Beanpublic ServiceBuilder<DemoService.Iface> demoServer(DemoServiceImpl serviceImpl) {return ServiceBuilder.fromInstance(DemoService.Iface.class, port, serviceImpl);}/*** 定义 Thrift Client*/@Beanpublic DemoService.Iface demoClient() {return ClientBuilder.create(DemoService.Iface.class, "excover").withTimeout(2000, TimeUnit.MILLISECONDS)// 直连地址,仅用于本地测试;这里请求服务自己.withDirectHost(HostAndPort.fromParts("127.0.0.1", port)).buildStub();}
}
3.Thrift RPC Client 调用 Demo
@RestController
@RequestMapping("/rpc")
public class DemoThriftController {private static final Logger logger = LoggerFactory.getLogger(DemoThriftController.class);@Resourceprivate DemoService.Iface demoClient;/*** curl http://127.0.0.1:8080/rpc/sayHello\?msg=demo*/@GetMapping("/sayHello")public String sayHello(String msg) {try {return demoClient.sayHello(ContextHelper.getContext(), msg);} catch (TException e) {logger.error("RPC 请求失败: {}", e.getMessage(), e);return "error";}}
}

http://www.lryc.cn/news/471575.html

相关文章:

  • Python期末题目 | 期末练习题【概念题+代码】
  • OpenCV基本操作(python开发)——(6)视频基本处理
  • 详解Java之Spring MVC篇一
  • ubuntu20.04上使用 Verdaccio 搭建 npm 私有仓库
  • Python实现办公自动化的数据可视化与报表生成
  • 前端知识串联笔记(更新中...)
  • PostgreSQL根据字符串的长度排序
  • 计算机网络:网络层 —— IP数据报的发送和转发过程
  • 【算力基础】GPU算力计算和其他相关基础(TFLOPS/TOPS/FP32/INT8...)
  • UI自动化测试(app端)4.0
  • C#与C++交互开发系列(十):数组传递的几种形式
  • 【C++复习】第一弹-基础性语法
  • 软考高级备考记录
  • 图为大模型一体机新探索,赋能智能家居行业
  • 精氨酸/赖氨酸多肽(芋螺肽)
  • C++音视频04:音视频编码、生成图片
  • ImageSharp报错
  • Android中常用adb命令
  • PostgreSQL的奥秘:全面解读JSONB——非结构化数据支持的深入探索
  • tornado,flaskd这两个框架主要是干什么的
  • Sigrity Power SI Noise coupling analysis模式如何进行压降仿真分析操作指导
  • 国产游戏技术能否引领全球?
  • 【前端】在 Next.js 开发服务器中应该如何配置 HTTPS?
  • 基于深度学习算法的动物检测系统(含PyQt+代码+训练数据集)
  • 微信小程序美团点餐
  • 音频剪辑还花钱?2024年这4款免费工具让你告别烦恼
  • 【YOLO模型】(4)--YOLO V3超超超超详解!!!
  • 管理类联考 信息整理和经验分享
  • JetBrains IDE中GPU进程(JCEF)重启问题(Too many restarts of GPU-process)解决方案
  • 《泛基因组:高质量参考基因组的新标准》