我又来骗星了, grpc-client-egg插件, TS与非TS都适用
发布于 4 个月前 作者 xiaozhongliu 698 次浏览 来自 分享

说是骗星的, 目的不纯啊. 都忘放github地址了, 谢评论区提醒: https://github.com/xiaozhongliu/grpc-client-egg

这里给一下TS中的使用方法.

grpc-client-egg

npm version

An egg grpc client written in Typescript.

How to use

1 install

npm i -S grpc-client-egg

2 include

import { EggPlugin } from 'egg'

const plugin: EggPlugin = {

    routerPlus: {
        enable: true,
        package: 'egg-router-plus',
    },
    grpcClient: {
        enable: true,
        package: 'grpc-client-egg',
    },
}

export default plugin

3 config

import { EggAppConfig, PowerPartial } from 'egg'

export default () => {
    const config: PowerPartial<EggAppConfig> = {

        grpcClient: {
            clients: [
                {
                    name: 'main',
                    protoPath: 'app/proto/main',
                    host: '0.0.0.0',
                    port: 50051,
                },
            ],
        },
    }
    return config
}

4 invoke promisified grpc service methods

import { Service } from 'egg'

export default class Greeter extends Service {

    readonly greeter: GreeterService = this.app.grpcClient.main.greeter.Greeter

    public async sayHello(name: string) {
        return this.greeter.sayHello({ name })
    }

    public async sayGoodbye(name: string) {
        return this.greeter.sayGoodbye({ name })
    }
}

Default config from the plugin

    loaderOption: {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true,
    },

    clients: [
        {
            name: 'main',
            protoPath: 'app/proto/main',
            host: '0.0.0.0',
            port: 50051,
        },
    ],
回到顶部