-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
487 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const SdkBase = require('sdk-base'); | ||
const { URL, URLSearchParams } = require('url'); | ||
const NacosNamingClient = require('nacos').NacosNamingClient; | ||
const RegistryBase = require('sofa-rpc-node/lib/registry/base'); | ||
|
||
const localIp = require('address').ip(); | ||
|
||
class NacosRegistryClient extends SdkBase { | ||
constructor(options = {}) { | ||
assert(options.logger, '[NacosRegistryClient] options.logger is required'); | ||
assert(options.address, '[NacosRegistryClient] options.address is required'); | ||
super({ initMethod: '_init' }); | ||
this.options = options; | ||
this._client = null; | ||
this._subscribeMap = new Map(); // <serviceKey, addressList> | ||
} | ||
|
||
get logger() { | ||
return this.options.logger; | ||
} | ||
|
||
async _init() { | ||
let serverList = this.options.address; | ||
if (typeof serverList === 'string') { | ||
serverList = serverList.split(','); | ||
} | ||
this._client = new NacosNamingClient({ | ||
logger: this.logger, | ||
serverList, | ||
}); | ||
await this._client.ready(); | ||
} | ||
|
||
async register(config) { | ||
assert(config && config.interfaceName, '[NacosRegistry] register(config) config.interfaceName is required'); | ||
assert(config.url, '[NacosRegistry] register(config) config.url is required'); | ||
const serviceName = this._buildProviderPath(config); | ||
const url = new URL(config.url); | ||
const searchParams = url.searchParams; | ||
const metadata = {}; | ||
for (const key of searchParams.keys()) { | ||
metadata[key] = searchParams.get(key); | ||
} | ||
await this._client.registerInstance(serviceName, { | ||
ip: url.hostname, | ||
port: Number(url.port), | ||
metadata, | ||
}); | ||
} | ||
|
||
async unRegister(config) { | ||
assert(config && config.interfaceName, '[NacosRegistry] register(config) config.interfaceName is required'); | ||
assert(config.url, '[NacosRegistry] register(config) config.url is required'); | ||
const serviceName = this._buildProviderPath(config); | ||
const url = new URL(config.url); | ||
await this._client.deregisterInstance(serviceName, url.hostname, Number(url.port)); | ||
} | ||
|
||
async _doSubscribe(consumerKey, config) { | ||
this._subscribeMap.set(consumerKey, null); | ||
await this._client.registerInstance(consumerKey, localIp, 0); | ||
const providerKey = this._buildProviderPath(config); | ||
this._client.subscribe(providerKey, hosts => { | ||
const addressList = hosts.filter(host => host.enabled).map(host => { | ||
const params = new URLSearchParams(); | ||
const metadata = host.metadata || {}; | ||
for (const key in metadata) { | ||
params.append(key, metadata[key]); | ||
} | ||
return `dubbo://${host.ip}:${host.port}?${params.toString()}`; | ||
}); | ||
this._subscribeMap.set(consumerKey, addressList); | ||
this.emit(consumerKey, addressList); | ||
}); | ||
} | ||
|
||
subscribe(config, listener) { | ||
assert(config && config.interfaceName, '[NacosRegistry] subscribe(config, listener) config.interfaceName is required'); | ||
const consumerKey = this._buildConsumerPath(config); | ||
|
||
if (!this._subscribeMap.has(consumerKey)) { | ||
this._doSubscribe(consumerKey, config) | ||
.catch(err => { this.emit('error', err); }); | ||
} else { | ||
const addressList = this._subscribeMap.get(consumerKey); | ||
if (addressList) { | ||
setImmediate(() => { listener(addressList); }); | ||
} | ||
} | ||
this.on(consumerKey, listener); | ||
} | ||
|
||
unSubscribe(config, listener) { | ||
assert(config && config.interfaceName, '[NacosRegistry] unSubscribe(config, listener) config.interfaceName is required'); | ||
const consumerKey = this._buildConsumerPath(config); | ||
|
||
if (listener) { | ||
this.removeListener(consumerKey, listener); | ||
} else { | ||
this.removeAllListeners(consumerKey); | ||
} | ||
if (this.listenerCount(consumerKey) === 0) { | ||
const providerKey = this._buildProviderPath(config); | ||
this._client.unSubscribe(providerKey); | ||
this._subscribeMap.delete(consumerKey); | ||
} | ||
} | ||
|
||
_buildProviderPath(config) { | ||
return this._getServiceName(config, 'providers'); | ||
} | ||
|
||
_buildConsumerPath(config) { | ||
return this._getServiceName(config, 'consumers'); | ||
} | ||
|
||
_getServiceName(config, category = 'providers') { | ||
let serviceName = category + ':' + config.interfaceName; | ||
if (config.version) { | ||
serviceName += ':' + config.version; | ||
} | ||
if (config.group) { | ||
serviceName += ':' + config.group; | ||
} | ||
return serviceName; | ||
} | ||
|
||
close() { | ||
this._client.close(); | ||
this._subscribeMap.clear(); | ||
} | ||
} | ||
|
||
class DubboNacosRegistry extends RegistryBase { | ||
get DataClient() { | ||
return NacosRegistryClient; | ||
} | ||
|
||
static get DataClient() { | ||
return NacosRegistryClient; | ||
} | ||
} | ||
|
||
module.exports = DubboNacosRegistry; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Controller = require('egg').Controller; | ||
|
||
class HomeController extends Controller { | ||
async index() { | ||
this.ctx.body = await this.ctx.proxy.demoService.sayHello('gxcsoccer'); | ||
} | ||
} | ||
|
||
module.exports = HomeController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = app => { | ||
const { router, controller } = app; | ||
|
||
router.get('/', controller.home.index); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
exports.sayHello = async function(name) { | ||
return 'hello ' + name; | ||
}; | ||
|
||
exports.echoPerson = async function(p) { | ||
return { | ||
$class: 'eggjs.demo.Person', | ||
$: p, | ||
}; | ||
}; |
Binary file added
BIN
+2.3 KB
test/fixtures/apps/dubbo-nacos/assembly/dubbo-demo-api-1.0-SNAPSHOT-sources.jar
Binary file not shown.
Binary file added
BIN
+2.54 KB
test/fixtures/apps/dubbo-nacos/assembly/dubbo-demo-api-1.0-SNAPSHOT.jar
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
exports.keys = '123456'; | ||
|
||
exports.rpc = { | ||
registry: { | ||
type: 'nacos', | ||
address: '127.0.0.1:8848', | ||
}, | ||
server: { | ||
version: '1.0.0', | ||
group: 'HSF', | ||
codecType: 'hessian2', | ||
namespace: 'eggjs.demo', | ||
}, | ||
}; |
Oops, something went wrong.