基于grpc-node封装的typescript rpc服务基础框架
发布于 6 年前 作者 xiaozhongliu 5409 次浏览 来自 分享

基于grpc-node封装了typescript rpc服务基础框架, 封装后使用起来很简便. repo: https://github.com/xiaozhongliu/ts-rpc-seed 下面是更新后的Todo List: [ √ ] TLS [ √ ] 异步中间件 [ √ ] 请求日志 [ √ ] proto => typescript代码生成器 [ ] 请求参数校验 (也是自动生成逻辑) [ ] 其他事项待定

cnpm i 装包后terminal npm run build启动ts热编译 vscode F5启动rpc server 然后node dist/client.js发起测试客户端请求: image.png server日志: image.png

写新的方法只需要更新proto接口定义, 生成代码, 并更新相应controller方法即可: image.png

image.png

7 回复

想法和设计上,给了一条思路

@HobaiRiku 哈哈看到你的tls的帖子了,有结果么?openssl生成的证书给这个怎么用,能用的话可否来个例子。 grpc-node貌似没有api文档,这个比较麻烦。

@xiaozhongliu 就是创建credentials的时候使用createSsl方法就好了,里面的参数都是可选的,直接空传参数就可以创建ssl连接,服务端的话我还没研究过,但是看文档是有三个参数,第一个跟证书可以null,第二个必须,第三个验证客户端回调也是可选,所以应该就是需要传中间的keycert对,关于证书那些也也还没完全理解,觉得如果做封装最好就直接把参数预留给用户就好了。不过grpc真的不支持ssl直接连接IP

@HobaiRiku 后面写到了试试, 话说你这头像跟一个前同事一样

@HobaiRiku tls/ssl可以用的, 我提交代码了, 你可以看看

// server
const credentials = grpc.ServerCredentials.createSsl(
    fs.readFileSync('./config/cert/ca.crt'),
    [{
        cert_chain: fs.readFileSync('./config/cert/server.crt'),
        private_key: fs.readFileSync('./config/cert/server.key')
    }],
    true
)
const serverAddress = `127.0.0.1:${config.API_PORT}`
server.bind(serverAddress, credentials)
server.start()



// client
const credentials = grpc.credentials.createSsl(
    fs.readFileSync('./config/cert/ca.crt'),
    fs.readFileSync('./config/cert/client.key'),
    fs.readFileSync('./config/cert/client.crt')
);
const clientAddress = `localhost:${config.API_PORT}`
const pack = proto.loadPackage('helloworld')
const client = new pack.Greeter(clientAddress, credentials)

@xiaozhongliu 🙂后来我也是才知道,这头像是宋仲基 基本就这样了吧…

@HobaiRiku proto => typescript 代码生成器也提交了, 看看有啥想法

codegen控制台输出

parsing package:  greeter
    parsing service:  Greeter
        parsing method:  sayHello
        parsing method:  sayGoodbye
    parsing message:  HelloRequest
        parsed property:  name
    parsing message:  HelloReply
        parsed property:  message

final AST:  {
    "services": [
        {
            "methods": [
                {
                    "name": "sayHello",
                    "request": {
                        "properties": [
                            {
                                "name": "name",
                                "type": "string",
                                "binaryId": 1
                            }
                        ],
                        "name": "HelloRequest"
                    },
                    "response": {
                        "properties": [
                            {
                                "name": "message",
                                "type": "string",
                                "binaryId": 1
                            }
                        ],
                        "name": "HelloReply"
                    }
                },
                {
                    "name": "sayGoodbye",
                    "request": {
                        "properties": [
                            {
                                "name": "name",
                                "type": "string",
                                "binaryId": 1
                            }
                        ],
                        "name": "HelloRequest"
                    },
                    "response": {
                        "properties": [
                            {
                                "name": "message",
                                "type": "string",
                                "binaryId": 1
                            }
                        ],
                        "name": "HelloReply"
                    }
                }
            ],
            "name": "Greeter",
            "package": "greeter"
        }
    ],
    "messages": [
        {
            "properties": [
                {
                    "name": "name",
                    "type": "string",
                    "binaryId": 1
                }
            ],
            "name": "HelloRequest"
        },
        {
            "properties": [
                {
                    "name": "message",
                    "type": "string",
                    "binaryId": 1
                }
            ],
            "name": "HelloReply"
        }
    ],
    "name": "greeter"
}

generated ctrl:  greeter-ctrl.ts
generated type:  HelloRequest.d.ts
generated class:  HelloReply.ts
回到顶部