【发布时间】:2020-04-11 09:24:03
【问题描述】:
编辑:我不想使用 Consul 或 ZooKeeper。我想在本地网络上查找 Web 服务的地址。
WCF 中服务发现类的 gRPC 等效项是什么,例如:本示例中使用的ServiceDiscoveryBehavior 和 UdpDiscoveryEndpoint 和 DiscoveryClient:
服务:
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
{
// Add calculator endpoint
serviceHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), string.Empty);
// ** DISCOVERY ** //
// Make the service discoverable by adding the discovery behavior
ServiceDiscoveryBehavior discoveryBehavior = new ServiceDiscoveryBehavior();
serviceHost.Description.Behaviors.Add(discoveryBehavior);
// Send announcements on UDP multicast transport
discoveryBehavior.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());
// ** DISCOVERY ** //
// Add the discovery endpoint that specifies where to publish the services
serviceHost.Description.Endpoints.Add(new UdpDiscoveryEndpoint());
// Open the ServiceHost to create listeners and start listening for messages.
serviceHost.Open();
}
客户:
{
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
Collection<EndpointDiscoveryMetadata> calculatorServices =
(Collection<EndpointDiscoveryMetadata>)
discoveryClient.Find(new FindCriteria(typeof(ICalculator))).Endpoints;
discoveryClient.Close();
CalculatorClient client = new CalculatorClient();
client.Endpoint.Address = calculatorServices[0].Address;
}
【问题讨论】:
-
ASP.NET Core gRPC for WCF developers: WS-Discovery 协议用于定位本地网络上的服务。 gRPC 服务通常使用 DNS 或服务注册中心(例如 Consul 或 ZooKeeper)定位。
-
你也可以看看这个帖子:GRPC Service Discovery.
-
您可能还会发现Node.js: Protobuf, gRPC and Discovery Services 很有用。
-
@RezaAghaei stackoverflow.com/questions/37534274/grpc-service-discovery 想要发现给定 GRPC 服务地址的服务能够接收的 GRPC 请求。我想发现地址。
标签: c# .net wcf .net-core grpc