tnblog
首页
视频
资源
登录

grpc 在vs中的运用与proto同步

6616人阅读 2020/4/2 16:24 总访问:2537868 评论:0 收藏:0 手机
分类: grpc

  思 考 问 题


 grpc 服务器当对 proto 文件进行修改时每次都去 Copy 到本地 Client 是比较麻烦的,有没有快捷的方法解决?



GrpcServer.Web 服务器端的修改



(一)修改 Message.proto 文件,去掉原有的 salary 字段



syntax = "proto3";

option csharp_namespace = "GrpcServer.Web.Protos";
import "Enums.proto";
import "google/protobuf/timestamp.proto";

message Employee {
	int32 id = 1;
	int32 no = 2;
	string firstName = 3;
	string lasterName = 4;

	MonthSalary monthSalary = 6;
	EmployeeStatus status =7;

	google.protobuf.Timestamp lastModfied = 8;

	reserved "salary";
	reserved 5;
}

message MonthSalary {
	float basic = 1;
	float bonus = 2;
}

message GetByNoRequest{
	int32 no = 1;
}

message EmployeeResponse{
	Employee employee =1;
}

message GetAllRequest {}

message AddPhotoRequest{
	bytes data = 1;
}

message AddPhotoResponse{
	bool isOk = 1;
}

message EmployeeRequest{
	Employee employee = 1;
}

service EmployeeService{
	rpc GetByNo(GetByNoRequest) returns (EmployeeResponse);
	rpc GetAll(GetAllRequest) returns (stream EmployeeResponse);
	rpc AddPhoto(stream AddPhotoRequest) returns (AddPhotoResponse);
	rpc Save(EmployeeRequest) returns (EmployeeResponse);
	rpc SaveAll(stream EmployeeRequest) returns (stream EmployeeResponse);
}



(二)添加 Enums.proto 文件



并对属性进行修改



syntax = "proto3";

option csharp_namespace = "GrpcServer.Web.Protos";

enum EmployeeStatus {
	NORMAL = 0;
	ONVACATION = 1;
	RESIGNED = 2;
	RETIRED = 3;
}


(三)对 InMemoryData.cs 修改


注意:在添加 时间字段 的值时,其值必须是 Utc


public class InMemoryData
{
    public static List<Employee> Employees = new List<Employee>() {
        new Employee{
            Id = 1,
            No = 1994,
            FirstName = "Chandler",
            LasterName = "Bing",
            MonthSalary = new MonthSalary()
            {
                Basic = 1000f,
                Bonus = 85f
            },
            Status = EmployeeStatus.Retired,
            LastModfied = Timestamp.FromDateTime(DateTime.UtcNow)
            //Salary = 2200
        },
        new Employee{
            Id = 2,
            No = 1999,
            FirstName = "Rachl",
            LasterName = "Green",
            MonthSalary = new MonthSalary()
            {
                Basic = 1000f,
                Bonus = 0f
            },
            Status = EmployeeStatus.Resigned,
            LastModfied = Timestamp.FromDateTime(DateTime.UtcNow)
            //Salary = 2400
        },
        new Employee{
            Id = 3,
            No = 2452,
            FirstName = "MI",
            LasterName = "UIMI",
            MonthSalary = new MonthSalary()
            {
                Basic = 1000f,
                Bonus = 1250.9f
            },
            Status = EmployeeStatus.Onvacation,
            LastModfied = Timestamp.FromDateTime(DateTime.UtcNow)
            //Salary = 2600
        }
    };
}


(四)双击 GrpcServer.Web 



最后 Ctrl+S  保存


GrpcClient 客户端的修改


(一)删除 Protos 文件夹下面的所有文件



(二)添加客户端相关资源

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.11.4" />
    <PackageReference Include="Grpc.AspNetCore" Version="2.27.0" />
    <PackageReference Include="Grpc.Net.Client" Version="2.27.0" />
    <PackageReference Include="Grpc.Net.ClientFactory" Version="2.27.0" />
    <PackageReference Include="Grpc.Tools" Version="2.27.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

(三)然后跟着我操作


注意这个时候也需要修改 Enums.proto 文件的属性哟!!!


大家这个时候自需要在服务器上修改 proto 文件即可同步到客户端



最后运行一下



感谢各位的观看!




欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价
这一世以无限游戏为使命!
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术