GRPC融合到aps.netcore 中

1. 添加包Grpc.AspNetCore

Startup 类中注入:

public void ConfigureServices(IServiceCollection services)
{
// 1、配置Grpc
services.AddGrpc();
}

 

app.UseEndpoints(endpoints =>
{
// 2、开启Grpc
endpoints.MapGrpcService<CourseServiceImpl>();

}

 

Program类中配置

Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
// 3、配置Kestrel服务器
webBuilder.UseKestrel(options => {
options.Listen(IPAddress.Loopback, 5001, options => {
// 3.1 默认证书
options.UseHttps();
});

// 3.2 配置协议为http2
options.ConfigureEndpointDefaults(options => {
options.Protocols = HttpProtocols.Http2;
});
});
webBuilder.UseStartup<Startup>();
});

 services类

/// <summary>
/// 课程服务实现
/// </summary>
public class CourseServiceImpl : CourseService.CourseServiceBase
{
public override Task<CourseResult> GetCourse(CourseForm request, ServerCallContext context)
{
// 1、获取课程编号
Console.WriteLine($"课程编号:{request.Id}");

// 2、查询之后返回
CourseResult courseResult = new CourseResult();
courseResult.Name = "微服务架构";

return Task.FromResult<CourseResult>(courseResult);
}

/// <summary>
/// 获取课程列表(服务器流式传输)
/// </summary>
/// <param name="request"></param>
/// <param name="responseStream"></param>
/// <param name="context"></param>
/// <returns></returns>
public override async Task GetCourseList(CourseForm request, IServerStreamWriter<CourseResult> responseStream, ServerCallContext context)
{
// 1、获取课程编号
Console.WriteLine($"课程编号:{request.Id}");

// 2、课程数据
List<CourseResult> courseResults = new List<CourseResult>()
{
new CourseResult()
{
Name = "夯实根基"
},
new CourseResult()
{
Name = ".net core"
}

};

// 3、返回课程列表
foreach (CourseResult courseResult in courseResults)
{
await responseStream.WriteAsync(courseResult);
}

}

/// <summary>
/// 3、获取课程列表通过学生列表(服务器流式传输)
/// </summary>
/// <param name="requestStream"></param>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<CourseResult> GetCourseFromStudentList(IAsyncStreamReader<CourseForm> requestStream, ServerCallContext context)
{
// 1、迭代获取客户端学生列表
while (await requestStream.MoveNext())
{
CourseForm courseForm = requestStream.Current;

Console.WriteLine($"课程编号:{courseForm.Id}");
}

CourseResult courseResult = new CourseResult();
courseResult.Name = "返回成功";

return await Task.FromResult<CourseResult>(courseResult);
}

 

客户端方法

 

/// <summary>
/// 获取课程
/// </summary>
public static void GetCourseByStudent()
{
// 1、建立连接
GrpcChannel grpcChannel = GrpcChannel.ForAddress("https://localhost:5001");

// 2、客户端创建
CourseServiceClient courseServiceClient = new CourseService.CourseServiceClient(grpcChannel);

// 3、获取课程(微服务架构)
CourseResult courseResult = courseServiceClient.GetCourse(new CourseForm {
Id = 3
});

Console.WriteLine($"返回值打印:{courseResult.Name}");

grpcChannel.Dispose();
// 4、打印课程信息
}

 

/// <summary>
/// 2、获取课程列表
/// </summary>
public static async void GetCourseListByStudent()
{
// 1、建立连接
GrpcChannel grpcChannel = GrpcChannel.ForAddress("https://localhost:5001");

// 2、客户端创建
CourseServiceClient courseServiceClient = new CourseService.CourseServiceClient(grpcChannel);

// 3、获取课程(微服务架构)
AsyncServerStreamingCall<CourseResult> streamingCall = courseServiceClient.GetCourseList(new CourseForm
{
Id = 3
});

// 4、获取数据流
IAsyncStreamReader<CourseResult> streamReader = streamingCall.ResponseStream;

// 4.1 开始迭代
while (await streamReader.MoveNext())
{
// 4.2 获取数据
CourseResult courseResult = streamReader.Current;

Console.WriteLine($"课程信息:{courseResult.Name}");
}

grpcChannel.Dispose();
}

 

/// <summary>
/// 3、传递学习列表(客户端的流式传输)
/// </summary>
public static async void GetCourseByStudentList()
{
// 1、建立连接
GrpcChannel grpcChannel = GrpcChannel.ForAddress("https://localhost:5001");

// 2、客户端创建
CourseServiceClient courseServiceClient = new CourseService.CourseServiceClient(grpcChannel);

// 3、获取课程(微服务架构)
AsyncClientStreamingCall<CourseForm, CourseResult> streamingCall = courseServiceClient.GetCourseFromStudentList();

// 4、获取请求流
IClientStreamWriter<CourseForm> clientStream = streamingCall.RequestStream;

for (int i = 1; i <= 161;i ++)
{
await clientStream.WriteAsync(new CourseForm
{
Id = i
}); ;
}

// 4.1 确定完成
await clientStream.CompleteAsync();

// 4.2 接受结果
Console.WriteLine($"获取结果:{ streamingCall.ResponseAsync.Result.Name}");

// grpcChannel.Dispose();
}

 

Protos 文件夹中的实体,需要在服务端和客户端进行同步。

/// <summary>
/// 1、一元调用
/// 2、服务器流式传输
/// 3、客户端流式传输
/// 4、服务器与客户端流式传输

 MQTT  https://www.cnblogs.com/js-jiangchenyang/p/17867388.html

posted @ 2024-04-08 00:08  一码事  阅读(29)  评论(0编辑  收藏  举报