By default, We use zookeeper for service discovery. Therefore you need to configure the zk address:
// ${app_root}/config/config.${env}.js
config.rpc = {
registry: {
address: '127.0.0.1:2181', // configure your real zk address
},
};
We plan to provide more implementations of service discovery. And also you can implement it by yourself, you can follow this article
RPC Client
By using the plugin, you can call rpc services provided by other system.
1. Get the RPC Interface Definition
We use protobuf interface definition lanaguge to describe the RPC service. So you need to get the *.proto files and put them into ${app_root}/proto folder
2. Global RPC Client Configuration
Configure RPC Client information in ${app_root}/config/config.{env}.js:
responseTimeout(optional): RPC timeout in milliseconds, default value is 3000
3. Configure the Interface in proxy.js
${app_root}/config/proxy.js is a very important config file for rpc client, you should configure the services you needed, then executing the egg-rpc-generator tool to generate the proxy files.
Let’s see a simple example of proxy.js. It declare a interface named: com.alipay.sofa.rpc.test.ProtoService provided by sofarpc application
Run egg-rpc-generator to generate the proxy files. After running success, it will generate a file: ProtoService.js under ${app_root}/app/proxy
$ egg-rpc-generator
[EggRpcGenerator] framework: /egg-rpc-demo/node_modules/egg, baseDir: /egg-rpc-demo
[ProtoRPCPlugin] found "com.alipay.sofa.rpc.test.ProtoService" in proto file
[ProtoRPCPlugin] save all proto info into "/egg-rpc-demo/run/proto.json"
5. Call the Service
You can call the RPC service by using ctx.proxy.proxyName. The proxyName is key value of api object you configure in proxy.js. In our example, it’s ProtoService, and proxyName using lower camelcase, so it’s ctx.proxy.protoService
egg-sofa-rpc
SOFARPC plugin for egg framework
Install
Usage
Enable the plugin
Change
${app_root}/config/plugin.jsto enable SOFARPC plugin:RPC Service Discovery
By default, We use zookeeper for service discovery. Therefore you need to configure the zk address:
We plan to provide more implementations of service discovery. And also you can implement it by yourself, you can follow this article
RPC Client
By using the plugin, you can call rpc services provided by other system.
1. Get the RPC Interface Definition
We use protobuf interface definition lanaguge to describe the RPC service. So you need to get the *.proto files and put them into
${app_root}/protofolder2. Global RPC Client Configuration
Configure RPC Client information in ${app_root}/config/config.{env}.js:
responseTimeout(optional): RPC timeout in milliseconds, default value is 30003. Configure the Interface in proxy.js
${app_root}/config/proxy.jsis a very important config file for rpc client, you should configure the services you needed, then executing theegg-rpc-generatortool to generate the proxy files.Let’s see a simple example of proxy.js. It declare a interface named:
com.alipay.sofa.rpc.test.ProtoServiceprovided bysofarpcapplicationRefer this acticle for more details
4. Generate the Proxy
Run
egg-rpc-generatorto generate the proxy files. After running success, it will generate a file:ProtoService.jsunder${app_root}/app/proxy5. Call the Service
You can call the RPC service by using
ctx.proxy.proxyName. The proxyName iskeyvalue of api object you configure in proxy.js. In our example, it’s ProtoService, and proxyName using lower camelcase, so it’sctx.proxy.protoServiceAs above, you can call remote service as a local method.
RPC Server
By using the plugin, you also can publish your own RPC service to other system.
1. Define the RPC Interface
Writing the *.proto files, and put them into
${app_root}/protofolder2. Global RPC Server Configuration
Configure RPC Server information in ${app_root}/config/config.{env}.js:
namespace(required): the default namespace of all rpc serviceselfPublish(optional): whether every node process publish service independent, default is trueport(optional): the TCP port will be listen onmaxIdleTime(optional): server will disconnect the socket if idle for long timeresponseTimeout(optional): Number of milliseconds to wait for a response to begin arriving back from the remote system after sending a requestcodecType(optional): recommended serialization method,default is protobuf3. Implemenation the RPC Interface
Put your implementation code under
${app_root}/app/rpcfolder4. RPC Unittest in Eggjs
For more details of
app.rpcRequest, you can refer to this acticleDocs
How to Contribute
Please let us know how can we help. Do check out issues for bug reports or suggestions first.
To become a contributor, please follow our contributing guide.
License
MIT