如何在 gRPC Angular 应用程序中使用 TypeScript Protobuf 生成的文件

10

问题概述

我正在学习如何使用 gRPC,并想尝试进行客户端-服务器连接。服务器(使用 Elixir 编写)已经能够工作,但是由于我主要是后端开发人员,在 Angular 中实现它遇到了很多麻烦,我希望能得到一些帮助。

我正在使用 Angular 8.2.9Angular CLI 8.3.8Node 10.16.0 进行这个项目。

我已经完成了什么?

  1. ng new test-grpc 命令创建了一个新的 Angular CLI 项目。
  2. 使用 ng g... 命令生成了一个模块和一个组件,并修改基本路由以使页面和 URL 可以正常工作。
  3. 安装了 Protobuf 和 gRPC 库 npm install @improbable-eng/grpc-web @types/google-protobuf google-protobuf grpc-web-client protoc ts-protoc-gen --save
  4. 将我的 Elixir 代码中的 .proto 文件复制到了 src/proto 文件夹中。
  5. 在 package.json 的 scripts 中添加了一个 protoc 命令:"protoc": "protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts.cmd --js_out=import_style=commonjs,binary:src/app/proto-gen --ts_out=service=true:src/app/proto-ts -I ./src/proto/ ./src/proto/*.proto"
  6. 调用命令生成在 src/app/proto-tssrc/app/proto-js 中找到的 js 和 ts 文件。
  7. 替换了生成文件名称中的任何 _-
  8. 然后我尝试在 ngOnInit 中添加以下代码:https://github.com/improbable-eng/grpc-web/tree/master/client/grpc-web#usage-overview 如下所示:
const getBookRequest = new GetBookRequest();
getBookRequest.setIsbn(60929871);
grpc.unary(BookService.GetBook, {
  request: getBookRequest,
  host: host,
  onEnd: res => {
    const { status, statusMessage, headers, message, trailers } = res;
    if (status === grpc.Code.OK && message) {
      console.log("all ok. got book: ", message.toObject());
    }
  }
});

我尝试过:

  • 在构造函数参数中创建请求。
  • 找到如何添加typescript文件(我只能找到在angular项目中添加javascript文件的方法,而生成的javascript比typescript更重,没有任何解释)。
  • 理解jspb.Message是什么(我仍然不知道,但我认为它与此文件相关:https://github.com/protocolbuffers/protobuf/blob/master/js/message.js)。
  • 尝试查找有关如何在angular应用程序中实现rGPD的教程(404未找到)。

.proto文件、生成的两个typescript文件和尝试使用它们的组件

.proto文件():

// src/proto/user.proto
syntax = "proto3";

service UserService {
  rpc ListUsers (ListUsersRequest) returns (ListUsersReply);
}

message ListUsersRequest {
  string message = 1;
}


message ListUsersReply {
  repeated User users = 1;
}

message User {
  int32 id = 1;
  string firstname = 2;
}

Typescript 生成的代码(2个文件):

// src/app/proto-ts/user-pb.d.ts

import * as jspb from "google-protobuf";

export class ListUsersRequest extends jspb.Message {
  getMessage(): string;
  setMessage(value: string): void;

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): ListUsersRequest.AsObject;
  static toObject(includeInstance: boolean, msg: ListUsersRequest): ListUsersRequest.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
  static serializeBinaryToWriter(message: ListUsersRequest, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): ListUsersRequest;
  static deserializeBinaryFromReader(message: ListUsersRequest, reader: jspb.BinaryReader): ListUsersRequest;
}

export namespace ListUsersRequest {
  export type AsObject = {
    message: string,
  }
}

export class ListUsersReply extends jspb.Message {
  clearUsersList(): void;
  getUsersList(): Array<User>;
  setUsersList(value: Array<User>): void;
  addUsers(value?: User, index?: number): User;

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): ListUsersReply.AsObject;
  static toObject(includeInstance: boolean, msg: ListUsersReply): ListUsersReply.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
  static serializeBinaryToWriter(message: ListUsersReply, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): ListUsersReply;
  static deserializeBinaryFromReader(message: ListUsersReply, reader: jspb.BinaryReader): ListUsersReply;
}

export namespace ListUsersReply {
  export type AsObject = {
    usersList: Array<User.AsObject>,
  }
}

export namespace User {
  export type AsObject = {
    id: number,
    firstname: string,
  }
}

// src/app/proto-ts/user-pb-service.d.ts
import * as user_pb from "./user-pb";
import {grpc} from "@improbable-eng/grpc-web";

type UserServiceListUsers = {
  readonly methodName: string;
  readonly service: typeof UserService;
  readonly requestStream: false;
  readonly responseStream: false;
  readonly requestType: typeof user_pb.ListUsersRequest;
  readonly responseType: typeof user_pb.ListUsersReply;
};

export class UserService {
  static readonly serviceName: string;
  static readonly ListUsers: UserServiceListUsers;
}

export type ServiceError = { message: string, code: number; metadata: grpc.Metadata }
export type Status = { details: string, code: number; metadata: grpc.Metadata }
interface UnaryResponse {
  cancel(): void;
}
interface ResponseStream<T> {
  cancel(): void;
  on(type: 'data', handler: (message: T) => void): ResponseStream<T>;
  on(type: 'end', handler: (status?: Status) => void): ResponseStream<T>;
  on(type: 'status', handler: (status: Status) => void): ResponseStream<T>;
}
interface RequestStream<T> {
  write(message: T): RequestStream<T>;
  end(): void;
  cancel(): void;
  on(type: 'end', handler: (status?: Status) => void): RequestStream<T>;
  on(type: 'status', handler: (status: Status) => void): RequestStream<T>;
}
interface BidirectionalStream<ReqT, ResT> {
  write(message: ReqT): BidirectionalStream<ReqT, ResT>;
  end(): void;
  cancel(): void;
  on(type: 'data', handler: (message: ResT) => void): BidirectionalStream<ReqT, ResT>;
  on(type: 'end', handler: (status?: Status) => void): BidirectionalStream<ReqT, ResT>;
  on(type: 'status', handler: (status: Status) => void): BidirectionalStream<ReqT, ResT>;
}

export class UserServiceClient {
  readonly serviceHost: string;

  constructor(serviceHost: string, options?: grpc.RpcOptions);
  listUsers(
    requestMessage: user_pb.ListUsersRequest,
    metadata: grpc.Metadata,
    callback: (error: ServiceError|null, responseMessage: user_pb.ListUsersReply|null) => void
  ): UnaryResponse;
  listUsers(
    requestMessage: user_pb.ListUsersRequest,
    callback: (error: ServiceError|null, responseMessage: user_pb.ListUsersReply|null) => void
  ): UnaryResponse;
}

尝试在组件文件中使用它们:
// src/app/modules/user/pages/list/list.component.ts
import { Component, OnInit } from '@angular/core';
import { grpc } from "@improbable-eng/grpc-web";

import { UserService } from '../../../../proto-ts/user-pb-service.d'
import { ListUsersRequest } from '../../../../proto-ts/user-pb.d'

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    const listUsersRequest = new ListUsersRequest();
    listUsersRequest.setMessage("Hello world");

    grpc.unary(UserService.ListUsers, {
      request: listUsersRequest,
      host: "0.0.0.0:50051",
      onEnd: res => {
        const { status, statusMessage, headers, message, trailers } = res;
        if (status === grpc.Code.OK && message) {
          console.log("all ok. got the user list: ", message.toObject());
        } else {
          console.log("error");
        }
      }
    });
  }
}

期望结果与实际结果

我希望能够在组件(或服务)中使用非 Angular 的 TypeScript 代码。 由于上述代码(但不包括组件文件)是生成的,因此不应进行修改,因为对 .proto 文件的任何更改都会覆盖对这两个生成文件所做的任何修改。

目前,我被这两个错误信息阻止了,这些错误信息取决于我最后保存的文件:如果我最后保存的是 protobuf 生成的文件,则不会在控制台中出现任何错误,否则会出现 TypeError: _proto_ts_user_pb_d__WEBPACK_IMPORTED_MODULE_4__.ListUsersRequest is not a constructor 或者 src\app\proto-ts\user-pb-service.d.ts and src\app\proto-ts\user-pb.d.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property. 错误信息。

tsconfig.json 中的 'files' 或 'include' 添加到其中(该文件由 Angular CLI 生成且未进行任何修改)会创建另一个错误:Refused to load the image 'http://localhost:4200/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.


你可能需要展示tsconfig.json文件,并且以更具体的方式展示错误信息:哪个类被报告为不是构造函数?哪个file_name.ts文件缺失? - YakovL
感谢您的评论。我已更新错误信息。tsconfig.json是由CLI生成的,没有任何修改(当我看到新的错误时,我删除了我添加的几行)。 - Aridjar
TypeScript对grpc-web的支持目前还处于相当实验阶段。首先,protobuf没有官方的TypeScript支持。您现在可以查看https://github.com/grpc/grpc-web/pull/626。有人尝试让grpc-web在Angular应用程序中工作。 - Stanley Cheung
我尝试在一个新的Angular应用程序中复现,一切都正常。不过,我没有时间调查为什么会出现这种情况。 - Aridjar
4个回答

3

我知道这个问题很老了,而且你已经解决了问题。

但我想补充两点:

  1. Grpc-web堆栈过于复杂。protobuf JavaScript API文档稀少,grpc-web也不好,生成的TypeScript声明只会让情况更加糟糕。

  2. 官方堆栈有替代品!例如ts-proto生成纯TypeScript。

我在使用grpc web和Angular时遇到了非常相似的问题,出于挫败感,我开始开发protobuf-ts。它是一个protoc插件,生成TypeScript并支持grpc-web。它是从头编写的,可能是一种可行的替代方案......


0

这里有个错误:

import { UserService } from '../../../../proto-ts/user-pb-service.d'
import { ListUsersRequest } from '../../../../proto-ts/user-pb.d'

正确的导入方式:(在模块名称末尾删除“.d”)

请注意,*.js 和 *.d.ts 必须在同一个文件夹中。

import { UserService } from '../../../../proto-ts/user-pb-service'
import { ListUsersRequest } from '../../../../proto-ts/user-pb'

package.json:选项编译ts_out=service=grpc-web:

  "scripts": {
    "grpc:all": "protoc --plugin=protoc-gen-ts=node_modules\\.bin\\protoc-gen-ts.cmd --js_out=import_style=commonjs,binary:./src/app/proto/generated --ts_out=service=grpc-web:./src/app/proto/generated -I ./src/app/proto ./src/app/proto/*.proto"
  },

上述编译选项是针对带有库"@improbable-eng/grpc-web"的grpc,请参阅指南:ts-protoc-gen

您的代码应该像这样:

// src/app/modules/user/pages/list/list.component.ts
import { Component, OnInit } from '@angular/core';
import { grpc } from "@improbable-eng/grpc-web";

import { UserService } from '../../../../proto-ts/user-pb-service'
import { ListUsersRequest } from '../../../../proto-ts/user-pb'

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    // build ProtoBuf Message to send
    const listUsersRequest = new ListUsersRequest();
    listUsersRequest.setMessage("Hello world");

    // ClientName = ServiceName + "Client"
    const service = new UserServiceClient('http://localhost:8080');
    
    // build headers of httpRequest
    // gRPC base on http2 protocol
    let metadata = new grpc.Metadata();

    metadata.set('header1', 'headerValue1');
    metadata.set('header2', 'headerValue2');

    // call gRPC service method
    service.ListUsers(listUsersRequest , metadata, (error, reply) => {
      if (error) {
        console.log(error)
        return;
      }

      console.log(reply));
    });
  }
}

0

0

这不是最好的方法。更像是一个变通方法(如果您的项目还不够大)。

最近我又回到了这个问题上。不想尝试新的东西,所以我只是按照以下步骤创建了一个新的angular项目:

  1. 将gRPC放在第一位
  2. 检查是否一切正常
  3. 从初始项目中添加一个模块
  4. 重复2和3,直到没有更多的模块可添加

现在它对我来说已经可以工作了。

就像我说的那样,这不是最好的解决方案,因为初始问题仍然存在于初始项目中。但这总比没有好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接