gRPC使用HTTP2传输,当netcore 调用时默认是使用ssl加密.如果不方便部署ssl站点,或者不想在代码中使用https调用。可在调用RPC服务之前声明:
#增加代码,如果是内网调用,可不关注https
if (!SERVER_GRPC.StartsWith("https"))
{
AppContext.SetSwitch(
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
完整测试代码
private const string SERVER_GRPC = "http://192.168.0.238:5009";
public static async Task Main()
{
#去掉https支持关键代码
if (!SERVER_GRPC.StartsWith("https"))
{
AppContext.SetSwitch(
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
//1.声明客户端
var channel = GrpcChannel.ForAddress(SERVER_GRPC);
var searchClient = new SearchSvc.SearchSvcClient(channel);
var response=searchClient.Search(new SearchRequest() { Keywords = "test" });
}