|
| 1 | +'use strict'; |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const protoLoader = require('@grpc/proto-loader'); |
| 5 | +const grpc = require('@grpc/grpc-js'); |
| 6 | + |
| 7 | +class loadGrpcProto { |
| 8 | + constructor(app) { |
| 9 | + this.app = app; |
| 10 | + this.config = app.config; |
| 11 | + this.logger = app.logger; |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * 获取proto文件路径列表 |
| 16 | + * @param protoDir |
| 17 | + */ |
| 18 | + async getProtoFileList(protoDir) { |
| 19 | + const fileStat = fs.statSync(protoDir); |
| 20 | + if (!fileStat.isDirectory()) { |
| 21 | + this.logger.error(`[egg-grpc-client] isn't dir: ${protoDir} `); |
| 22 | + return; |
| 23 | + } |
| 24 | + const filePathNameList = fs.readdirSync(protoDir); |
| 25 | + // 过滤 |
| 26 | + return filePathNameList |
| 27 | + .filter(name => name.endsWith('.proto')) |
| 28 | + .map(filePathName => path.join(protoDir, filePathName)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 获取grpc client实例 |
| 33 | + * @param protoDir |
| 34 | + * @param config |
| 35 | + */ |
| 36 | + async getGrpcClient(protoDir, config) { |
| 37 | + const { host, port, loaderOptions } = config; |
| 38 | + const protoList = await this.getProtoFileList(protoDir); |
| 39 | + const grpcClient = {}; |
| 40 | + for (const protoTargetPath of protoList) { |
| 41 | + const packageDefinition = protoLoader.loadSync( |
| 42 | + protoTargetPath, |
| 43 | + loaderOptions |
| 44 | + ); |
| 45 | + const grpcObject = grpc.loadPackageDefinition(packageDefinition); |
| 46 | + // grpc对象遍历处理 |
| 47 | + Object.values(grpcObject).forEach(packageObj => { |
| 48 | + for (const serviceClass of Object.values(packageObj)) { |
| 49 | + if (serviceClass.service != null) { |
| 50 | + const grpcClientInstance = new serviceClass( |
| 51 | + `${host}:${port}`, |
| 52 | + grpc.credentials.createInsecure() |
| 53 | + ); |
| 54 | + // console.log(Object.getOwnPropertyNames(serviceClass.prototype)); |
| 55 | + // 遍历方法 serviceClass.prototype 替换 grpcClientInstance.__proto__ |
| 56 | + Object.getOwnPropertyNames(serviceClass.prototype).forEach( |
| 57 | + name => { |
| 58 | + if (name !== 'constructor') { |
| 59 | + grpcClient[name] = async requestData => |
| 60 | + await this.handleGrpcClientMethod( |
| 61 | + grpcClientInstance, |
| 62 | + name, |
| 63 | + requestData |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + return grpcClient; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * |
| 77 | + * 对grpcClient客户端方法,进行封装处理 |
| 78 | + * 支持async/await调用 |
| 79 | + * @param clientInStance grpc客户端对象实例 |
| 80 | + * @param name 方法名 |
| 81 | + * @param requestData 请求数据 |
| 82 | + */ |
| 83 | + async handleGrpcClientMethod(clientInStance, name, requestData) { |
| 84 | + const { app } = this; |
| 85 | + if (typeof requestData !== 'object') { |
| 86 | + app.logger.error( |
| 87 | + '[egg-grpc-client] grpc client method params error ,please look at docs' |
| 88 | + ); |
| 89 | + return; |
| 90 | + } |
| 91 | + return new Promise((resolve, reject) => { |
| 92 | + // todo 支持grpc传输字段 自定义 ,默认 data |
| 93 | + clientInStance[name]( |
| 94 | + { data: JSON.stringify(requestData) }, |
| 95 | + (err, result) => { |
| 96 | + // 将结果反序列化, |
| 97 | + if (err == null) { |
| 98 | + const { data } = result; |
| 99 | + if (data == null) { |
| 100 | + app.logger.info( |
| 101 | + '[egg-grpc-client] no key-word data in rpc message struct ,please check .proto file' |
| 102 | + ); |
| 103 | + return; |
| 104 | + } |
| 105 | + resolve(JSON.parse(data)); |
| 106 | + } |
| 107 | + reject(err, result); |
| 108 | + } |
| 109 | + ); |
| 110 | + }); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = loadGrpcProto; |
0 commit comments