Skip to content

Commit

Permalink
feat(@142vip/egg-grpc-client): 支持客户端grpc建立连接
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Jan 9, 2025
1 parent 364e451 commit 9f96de5
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 3 deletions.
18 changes: 16 additions & 2 deletions packages/egg-grpc-client/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ const { name: pkgName } = require('../package.json')
module.exports = {
grpcClient: defaultPluginConfig(pkgName, {
default: {

loaderOptions: {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
},
},
client: {

connectUri: '127.0.0.1:50051',
protoPaths: ['./proto/helloworld.proto'],
loaderOptions: {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
},
},
}),
}
27 changes: 27 additions & 0 deletions packages/egg-grpc-client/core/grpc-client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
const { VipEggPluginLogger } = require('@142vip/egg')
const { GrpcClient, ProtoLoader } = require('@142vip/grpc')

/**
* 创建egg grpc连接实例
* @param pluginConfig
* @param app
*/
function createEggGrpcClientInstance(pluginConfig, app) {
const pluginLogger = VipEggPluginLogger.getInstance(pluginConfig, app)

const { connectUri, protoPaths, loaderOptions } = pluginConfig

const grpcClient = GrpcClient.getInstance()
// 加载proto
for (const protoPath of protoPaths) {
const protoLoader = ProtoLoader.getInstance(protoPath, loaderOptions)
const serviceClassDefinition = protoLoader.getServiceClassDefinition()

// 建立连接
grpcClient.connect(connectUri, serviceClassDefinition)
}

grpcClient.connect()

if (grpcClient.getConnectSize() === 0) {
pluginLogger.error('grpc client connect size is 0')
}

const clientService = grpcClient.getService()
pluginLogger.info('plugin init')

return clientService
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion packages/egg-grpc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
],
"dependencies": {
"@142vip/egg": "workspace:*",
"@142vip/utils": "workspace:*"
"@142vip/utils": "workspace:*",
"@142vip/grpc": "workspace:*"
},
"publishConfig": {
"access": "public",
Expand Down
118 changes: 118 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f96de5

Please sign in to comment.