相当于 WCF 服务发现的 gRPC
gRPC equivalent of WCF service discovery
编辑:我不想使用 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;
}
gRPC 没有 类 等同于 ServiceDiscoveryBehavior
、UdpDiscoveryEndpoint
或 DiscoveryClient
。
您必须使用 System.Net.Sockets
使用 UDP 广播编写您自己的网络发现。
服务:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.
FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
Server grpcServer = new Server
{
Services = { Simulator.BindService(new Service()) },
Ports = { new ServerPort(ipAddress.ToString(), 8080, ServerCredentials.Insecure) }
};
grpcServer.Start();
Task.Run(() =>
{
while (true)
{
UdpClient udpServer = new UdpClient(8888);
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] clientRequestData = udpServer.Receive(ref clientEndPoint);
string clientRequest = Encoding.ASCII.GetString(clientRequestData);
Console.WriteLine($"Recived {clientRequest} from {clientEndPoint.Address}");
byte[] responseData = Encoding.ASCII.GetBytes("Response");
udpServer.Send(responseData, responseData.Length, clientEndPoint);
udpServer.Close();
}
});
客户:
UdpClient udpClient = new UdpClient { EnableBroadcast = true };
byte[] requestData = Encoding.ASCII.GetBytes("Request");
udpClient.Send(requestData, requestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] serverResponseData = udpClient.Receive(ref serverEndPoint);
string serverResponse = Encoding.ASCII.GetString(serverResponseData);
Console.WriteLine($"Recived {serverResponse} from {serverEndPoint.Address}");
IPAddress ipAddress = serverEndPoint.Address;
udpClient.Close();
var grpcChannel = new Channel(ipAddress.ToString(), 8080, ChannelCredentials.Insecure);
var grpcClient = new Client(grpcChannel);
编辑:我不想使用 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;
}
gRPC 没有 类 等同于 ServiceDiscoveryBehavior
、UdpDiscoveryEndpoint
或 DiscoveryClient
。
您必须使用 System.Net.Sockets
使用 UDP 广播编写您自己的网络发现。
服务:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.
FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
Server grpcServer = new Server
{
Services = { Simulator.BindService(new Service()) },
Ports = { new ServerPort(ipAddress.ToString(), 8080, ServerCredentials.Insecure) }
};
grpcServer.Start();
Task.Run(() =>
{
while (true)
{
UdpClient udpServer = new UdpClient(8888);
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] clientRequestData = udpServer.Receive(ref clientEndPoint);
string clientRequest = Encoding.ASCII.GetString(clientRequestData);
Console.WriteLine($"Recived {clientRequest} from {clientEndPoint.Address}");
byte[] responseData = Encoding.ASCII.GetBytes("Response");
udpServer.Send(responseData, responseData.Length, clientEndPoint);
udpServer.Close();
}
});
客户:
UdpClient udpClient = new UdpClient { EnableBroadcast = true };
byte[] requestData = Encoding.ASCII.GetBytes("Request");
udpClient.Send(requestData, requestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] serverResponseData = udpClient.Receive(ref serverEndPoint);
string serverResponse = Encoding.ASCII.GetString(serverResponseData);
Console.WriteLine($"Recived {serverResponse} from {serverEndPoint.Address}");
IPAddress ipAddress = serverEndPoint.Address;
udpClient.Close();
var grpcChannel = new Channel(ipAddress.ToString(), 8080, ChannelCredentials.Insecure);
var grpcClient = new Client(grpcChannel);