您的位置:首页 > 教育 > 培训 > 深圳地址大全_上海公司沪牌价格_产品线上营销推广方案_郑志平爱站网创始人

深圳地址大全_上海公司沪牌价格_产品线上营销推广方案_郑志平爱站网创始人

2025/5/12 3:34:08 来源:https://blog.csdn.net/mumuwang1234/article/details/143311843  浏览:    关键词:深圳地址大全_上海公司沪牌价格_产品线上营销推广方案_郑志平爱站网创始人
深圳地址大全_上海公司沪牌价格_产品线上营销推广方案_郑志平爱站网创始人

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";}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com