registry (we use zookeeper as service registry in dubbo)
address:(required) the zookeeper address
client
responseTimeout:(optional) number of milliseconds to wait for a response, if timeout will get an exception, the default value is 3000(ms)
server
namespace:(required) the default namespace to publish all services
port:(optional) the port which RPC server listening on, the default value is 12200
maxIdleTime:(optional) maximum idle time (in milliseconds) for a connection
codecType:(optional) the serialization type, default value is hessian2
selfPublish:(optional) if set to true (default), every worker process will listen on different ports
version:(optional) the service version, default value is 1.0.0
group:(optional) the service group, default value is DUBBO
autoServe:(optional) if set to true (default), will launce Dubbo RPC server automatically
3. Call Dubbo Services as Consumer
Configure the Interface in proxy.js
First, you need to put the JAR file (which contains the API interfaces) into {app_root}/assembly folder.
And then you need to config $app_root/config/proxy.js, which 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: org.eggjs.dubbo.UserService provided by dubbo application
version:(optional) service version, the global config
group:(optional) service group
errorAsNull:(optional) if set true, we are returning null instead of throwing an exception while error appears
services:(required) RPC services configuation
appName:(required) the name of RPC provider
api:(required) API details
interfaceName:(required) interface name
version:(optional) service version, it will overwrite the global one
group:(optional) service group, it will overwrite the global one
dependency:(required) like Maven pom config
groupId:(required) uniquely identifies your project across all projects
artifactId:(required) the name of the jar without version
version:(required) the jar version
Generate the Proxy
Run egg-rpc-generator to generate the proxy files. After running success, it will generate all proxy files under ${app_root}/app/proxy
install egg-rpc-generator
$ npm i egg-rpc-generator --save-dev
add rpc command into scripts of package.json
{
"scripts": {
"rpc": "egg-rpc-generator"
},
}
execute the rpc command
$ npm run rpc
Call Dubbo Service
You can call the Dubbo 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 UserService, and proxyName using lower camelcase, so it’s ctx.proxy.userService
egg-dubbo-rpc
Dubbo RPC plugin for Egg.js
Install
Usage
1. Enable the Plugin
enable egg-dubbo-rpc plugin in ${app_root}/config/plugin.js:
2. Configuration
all configuations is under
rpcpropertyaddress:(required)the zookeeper addressresponseTimeout:(optional)number of milliseconds to wait for a response, if timeout will get an exception, the default value is 3000(ms)namespace:(required)the default namespace to publish all servicesport:(optional)the port which RPC server listening on, the default value is 12200maxIdleTime:(optional)maximum idle time (in milliseconds) for a connectioncodecType:(optional)the serialization type, default value is hessian2selfPublish:(optional)if set to true (default), every worker process will listen on different portsversion:(optional)the service version, default value is 1.0.0group:(optional)the service group, default value is DUBBOautoServe:(optional)if set to true (default), will launce Dubbo RPC server automatically3. Call Dubbo Services as Consumer
Configure the Interface in proxy.js
First, you need to put the JAR file (which contains the API interfaces) into
{app_root}/assemblyfolder.And then you need to config
$app_root/config/proxy.js, which 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:
org.eggjs.dubbo.UserServiceprovided bydubboapplicationdetails as follows:
version:(optional)service version, the global configgroup:(optional)service grouperrorAsNull:(optional)if set true, we are returning null instead of throwing an exception while error appearsservices:(required)RPC services configuationappName:(required)the name of RPC providerapi:(required)API detailsinterfaceName:(required)interface nameversion:(optional)service version, it will overwrite the global onegroup:(optional)service group, it will overwrite the global onedependency:(required)like Maven pom configgroupId:(required)uniquely identifies your project across all projectsartifactId:(required)the name of the jar without versionversion:(required)the jar versionGenerate the Proxy
Run egg-rpc-generator to generate the proxy files. After running success, it will generate all proxy files under ${app_root}/app/proxy
install egg-rpc-generator
add rpc command into scripts of package.json
execute the rpc command
Call Dubbo Service
You can call the Dubbo RPC service by using
ctx.proxy.proxyName. The proxyName is key value of api object you configure in proxy.js. In our example, it’sUserService, and proxyName using lower camelcase, so it’sctx.proxy.userServiceUnittest of RPC Client in Egg.js
you can use
app.mockProxyto mock the RPC interfaceAs above, you can call remote service as a local method.
4. Expose Dubbo Services as Provider
Define the RPC Interface
create a JAR file that contains the API interface
Implemenation the RPC Interface
Put your implementation code under
${app_root}/app/rpcfolderUnittest of your RPC Server in Egg.js
For more details of app.rpcRequest, you can refer to this acticle
Reference List
Questions & Suggestions
Please open an issue here.
License
Apache License V2