You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gRPC反射中隐藏特定服务,仅暴露指定服务?

问题:如何让gRPC反射仅暴露指定服务?

当前运行着一个带有反射服务的gRPC服务器,代码示例如下:

Server server = ServerBuilder.forPort(9092)
            .addService(ProtoReflectionService.newInstance())
            .addService(new StructServiceImpl())
            .addService(new ChatServiceImpl()).build();
    server.start();

目前StructService和ChatService均通过反射对外暴露,想了解是否有办法仅让ChatService被反射,而不向外部暴露StructService?


可以实现,核心是不使用默认的全量反射服务,而是自定义反射服务的服务范围,只包含你想暴露的ChatService。

具体代码修改如下:

// 获取ChatService的官方服务描述符
ServiceDescriptor chatServiceDescriptor = ChatServiceGrpc.getServiceDescriptor();

// 创建仅包含ChatService的自定义反射服务
ProtoReflectionService customReflectionService = ProtoReflectionService.newInstance()
    .withServiceDescriptors(chatServiceDescriptor);

// 构建服务器:添加自定义反射服务、StructService、ChatService
Server server = ServerBuilder.forPort(9092)
    .addService(customReflectionService)
    .addService(new StructServiceImpl())
    .addService(new ChatServiceImpl())
    .build();
server.start();

说明:

  • 默认的ProtoReflectionService.newInstance()会自动扫描并注册所有添加到服务器的服务,所以StructService会被反射暴露。
  • 通过withServiceDescriptors()方法,我们可以明确指定反射服务要对外暴露的服务描述符集合,这样只有ChatService会出现在反射结果中,而StructService依然能正常提供业务服务,只是不会被外部通过反射发现。

内容的提问来源于stack exchange,提问作者Dilan Sachintha Nayanajith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 00:10:41