You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/12/01 09:03:38 UTC

[GitHub] [dubbo-js] wawIsready commented on issue #322: Mvp 版本讨论

wawIsready commented on issue #322:
URL: https://github.com/apache/dubbo-js/issues/322#issuecomment-1333442409

   > 因为protocolbuff 强依赖proto或者proto生成的valid type文件定义,所以IDL生成的代码需要和序列化模块耦合起来 接下来讨论IDL和serialization的协作机制以及代码生成部分。
   > 
   > ## 序列化接口设计
   > ```ts
   > interface Serialization {
   >   // load 所有的proto文件,获取request类型
   >   loadProto(protoPath: string): void;
   >   
   >  // encode 请求数据,后置type参数且是可选,为了将来切换其他协议如不需要type类型,可以不传,我们接口可以不改 
   >   encode<T>(data: T, type?: string): Buffer;
   > 
   > // decode 请求数据,后置type参数且是可选,为了将来切换其他协议如不需要type类型,可以不传,我们接口可以不改 
   >   decode<T>(data: Buffer, type?: string): T;
   > }
   > ```
   > 
   > ## 序列化模块的设计
   > * 模块名 dubbo-serialization
   > * 实现上述接口方法
   > 
   > ## IDL 代码生成
   > 针对Mvp的proto定义,对client stub service代码样例:
   > 
   > ```ts
   > // define service interface
   > export interface Mvp {
   >   SayHello(req: HelloRequest): Promise<HelloReply>;
   >   Check(req: HealthCheckRequest): Promise<HealthCheckResponse>;
   > }
   > 
   > // define enum
   > enum ServingStatus {
   >   UNKNOWN = 0,
   >   SERVING = 1,
   >   NOT_SERVING = 2,
   >   SERVICE_UNKNOWN = 3, // Used only by the Watch method.
   > }
   > 
   > // define request && response, 实际代码可以根据不同的namespace生成到不同的目录
   > export interface HealthCheckResponse {
   >   status: ServingStatus;
   > }
   > 
   > export interface HealthCheckRequest {
   >   service: string;
   > }
   > 
   > export interface HelloRequest {
   >   name: string;
   > }
   > 
   > export interface HelloReply {
   >   message: string;
   > }
   > 
   > 
   > // define service metadata
   > 
   > import ds from 'dubbo-serialization'
   > 
   > // TODO 或者对于ecode和decode的过程,IDL生成代码只返回 {path, data} 由invoke来负责底层的调用
   > export const Mvp = {
   >   SayHello: { 
   >      path: "/helloworld.Mvp/SayHello", 
   >      encode(data: HelloRequest) {
   >        return ds.encode(data, ` hellorequest在对象path路径 `)
   >     },
   >     decode(data: Buffer) {
   >        return  ds.decode(data, `helloreplay 的path路径 `)
   >     }
   >   },
   >   Check: { 
   >     path: "/helloworld.Mvp/Check",
   >     encode(data: HealthCheckRequest) {
   >        return ds.encode(data, ` HealthCheckRequest在对象path路径 `)
   >     },
   >    decode(data: Buffer) {
   >        return  ds.decode(data, `helloreplay 的path路径 `)
   >     }
   >  },
   > };
   > 
   > 
   > // server 端
   > 
   > 
   > // 生成抽象类
   > export abstract class AbstractMvp {
   >   path = "/helloworld.Greeter";
   > 
   >   methods = {
   >     SayHello: this.SayHello.bind(this),
   >     Check: this.Check.bind(this),
   >   };
   > 
   >   abstract SayHello(req: HelloRequest): Promise<HelloReply>;
   >   abstract Check(req: HealthCheckRequest): Promise<HealthCheckResponse>;
   > }
   > 
   > // 生成实现类
   > export class MvpService extends AbstractMvp {
   >   SayHello(req: HelloRequest): Promise<HelloReply> {
   >     throw new Error("Method not implemented.");
   >   }
   >   Check(req: HealthCheckRequest): Promise<HealthCheckResponse> {
   >     throw new Error("Method not implemented.");
   >   }
   > }
   > ```
   
    client 里的 path 和 methods 的一一对应关系是不是要和 server 这边的逻辑一致?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org