Skip to content
Permalink
Browse files
Merge pull request #244 from tea-js/fixed-nacos
Fixed nacos
  • Loading branch information
hufeng committed Jul 4, 2021
2 parents 2783cc1 + dd3ef88 commit fbd1ec33f9f4cd5fc5f5dfce9bf46651bb92c86f
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 110 deletions.
@@ -60,4 +60,4 @@ es7
examples/hello-egg/typings
examples/hello-midway/src/typings

dubbo-java/nacos-docker/example/
dubbo-java/nacos-docker/example/standalone-logs/

This file was deleted.

This file was deleted.

@@ -22,6 +22,6 @@ cd dubbo-demo-provider-with-nacos
mvn clean package

# start nacos cluster
# docker-compose -f ../../nacos-docker/example/standalone-derby.yaml up &
docker-compose -f ../../nacos-docker/example/standalone-derby.yaml up &
# start java dubbo service
java -jar ./target/dubbo-demo-provider-with-nacos-2.7.4.1-jar-with-dependencies.jar
@@ -0,0 +1,18 @@
# 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.

#!/usr/bin/env sh
# start nacos cluster
docker-compose -f ./nacos-docker/example/standalone-derby.yaml up
@@ -24,7 +24,11 @@ const dubbo = new Dubbo<typeof services>({
application: {
name: 'hello-api'
},
registry: Nacos({ connect: 'nacos://localhost:8848' }),
registry: Nacos({
connect: 'localhost:8848',
namespace: 'public',
logger: console
}),
services
})

@@ -22,8 +22,11 @@ import services from './service'
// start
;(async function main() {
const dubbo = new DubboService({
// registry: Zk({ connect: 'localhost:2181' }),
registry: Nacos({ connect: 'nacos://localhost:8848' }),
registry: Nacos({
connect: 'localhost:8848',
namespace: 'public',
logger: console
}),
services
})
await dubbo.ready()
@@ -19,6 +19,16 @@ import Zookeeper from 'zookeeper'
import { Zk } from '../registry-zookeeper'

describe('test zookeeper registry', () => {
it('test throw connection url error', () => {
try {
Zk({
connect: ''
})
} catch (err) {
expect(err.message).toEqual('Please specify zookeeper connect url')
}
})

it('test zk props and ready ok', async () => {
const zk = Zk({
connect: 'localhost:2181'
@@ -20,6 +20,7 @@ import BaseRegistry from './registry-base'
import { IRegistry } from './registry'
import { INaocsClientProps, TDubboInterface, TDubboUrl } from './types'
import qs from 'querystring'
import { util } from '@apache/dubbo-common'

// log
const dlog = debug('dubbo:nacos~')
@@ -28,7 +29,8 @@ const NacosNamingClient = require('nacos').NacosNamingClient
// nacos debug
export class NacosRegistry
extends BaseRegistry
implements IRegistry<typeof NacosNamingClient> {
implements IRegistry<typeof NacosNamingClient>
{
// nacos props
private nacosProps: INaocsClientProps
private client: typeof NacosNamingClient
@@ -38,10 +40,11 @@ export class NacosRegistry
private reject: Function

constructor(nacosProps: INaocsClientProps) {
NacosRegistry.checkProps(nacosProps)
super()
dlog(`init nacos with %O`, nacosProps)
this.nacosProps = nacosProps
this.nacosProps.nacosRoot = this.nacosProps.nacosRoot || 'dubbo'
this.nacosProps.namespace = this.nacosProps.namespace || 'default'

// init ready promise
this.readyPromise = new Promise((resolve, reject) => {
@@ -57,13 +60,16 @@ export class NacosRegistry

// nacos connect
private async init() {
let registryUrl = this.nacosProps.connect.split('nacos://')[1]
dlog(`connecting nacosserver ${registryUrl}`)
// support nacos cluster
let serverList = this.nacosProps.connect.split(',')
let namespace = this.nacosProps.namespace || 'public'
let logger = this.nacosProps.logger || console
dlog(`connecting nacos server ${serverList}`)

this.client = new NacosNamingClient({
logger: console,
serverList: registryUrl,
namespace: 'public'
serverList,
namespace,
logger
})

try {
@@ -85,18 +91,20 @@ export class NacosRegistry
this.findDubboServiceUrl(dubboInterface)
)
)
this.emitData(this.dubboServiceUrlMap)
}

async findDubboServiceUrl(dubboInterface: string) {
const dubboServiceUrls = await this.client.getAllInstances(dubboInterface)
dlog('dubboServiceUrls => %O', dubboServiceUrls)
for (let { ip: hostname, port, metadata } of dubboServiceUrls) {
const url = `consumer://${hostname}:${port}/${dubboInterface}?${qs.stringify(
metadata
)}`
this.dubboServiceUrlMap.set(dubboInterface, [url])
}
this.client.subscribe(dubboInterface, (dubboServiceUrls) => {
dlog('dubboServiceUrls => %O', dubboServiceUrls)
const urls = dubboServiceUrls.map((item) => {
const { ip, port, serviceName, metadata } = item
const inf = serviceName.split('@@')[1]
return `beehive://${ip}:${port}/${inf}?${qs.stringify(metadata)}`
})
this.dubboServiceUrlMap.set(dubboInterface, urls)
dlog('urls => %O', urls)
this.emitData(this.dubboServiceUrlMap)
})
}

// 注册服务提供
@@ -108,16 +116,7 @@ export class NacosRegistry
) {
dlog('services => %O', services)
for (let { dubboServiceInterface, dubboServiceUrl } of services) {
let metadata = qs.parse(dubboServiceUrl.split('?')[1])
const ipAndHost = dubboServiceUrl.split('dubbo://')[1].split('/')[0]
const ip = ipAndHost.split(':')[0]
const port = ipAndHost.split(':')[1] || 80
dlog('metadata and ipAndHost => ', metadata, ipAndHost)
await this.client.registerInstance(dubboServiceInterface, {
ip,
port,
metadata
})
await this.registerInstance(dubboServiceInterface, dubboServiceUrl)
}
}

@@ -132,27 +131,57 @@ export class NacosRegistry
const dubboInterfaces = new Set<string>()
for (let { dubboServiceInterface, dubboServiceUrl } of consumers) {
dubboInterfaces.add(dubboServiceInterface)
let metadata = qs.parse(dubboServiceUrl.split('?')[1])
const ipAndHost = dubboServiceUrl.split('consumer://')[1].split('/')[0]
const ip = ipAndHost?.split(':')[0]
const port = ipAndHost?.split(':')[1] || 80
dlog('metadata and ipAndHost => ', metadata, ipAndHost)
await this.client.registerInstance(dubboServiceInterface, {
ip,
port,
metadata
})
await this.registerInstance(dubboServiceInterface, dubboServiceUrl)
}
await this.findDubboServiceUrls([...dubboInterfaces])
}

async registerInstance(
dubboServiceInterface: string,
dubboServiceUrl: string
) {
const metadata = {}
const urlObj = new URL(dubboServiceUrl)
dlog('urlObj => %O', urlObj)
const { hostname: ip, port, searchParams } = urlObj
for (const key of searchParams.keys()) {
metadata[key] = searchParams.get(key)
}
await this.client.registerInstance(dubboServiceInterface, {
ip,
port: port || 80,
metadata
})
}

close(): void {
this.client?.close()
}

getClient() {
return this.client
}

/**
* check nacos prop
* @param props
*/
private static checkProps(props: INaocsClientProps) {
if (!props.connect) {
throw new Error(`Please specify nacos props, connect is required`)
}
if (!util.isString(props.connect)) {
throw new Error(`Please specify nacos props, connect should be a string`)
}
if (props.namespace && !util.isString(props.namespace)) {
throw new Error(
`Please specify nacos props, namespace should be a string`
)
}
if (!props.logger) {
throw new Error(`Please specify nacos props, logger is required`)
}
}
}

export function Nacos(props: INaocsClientProps) {
@@ -44,6 +44,7 @@ export class ZookeeperRegistry
constructor(props: IZkClientConfig) {
super()
dlog(`init zookeeper with %O`, props)
this.checkProps(props)
this.props = props

this.props.zkRootPath = this.props.zkRootPath || DUBBO_ZK_ROOT_PATH
@@ -66,6 +67,12 @@ export class ZookeeperRegistry
}

// ~~~~~~~~~~~~~~~~ private ~~~~~~~~~~~~~~~~~~~~~~~~~~
private checkProps(props: IZkClientConfig) {
if (!props.connect) {
throw new Error(`Please specify zookeeper connect url`)
}
}

private init() {
// cache this.client
if (this.client) {
@@ -24,8 +24,9 @@ export interface ITimeoutProps {
}

export interface INaocsClientProps {
nacosRoot?: string
namespace?: string
connect: string
logger: Console
}
export interface IRegistrySubscriber {
onData: (map: Map<TDubboInterface, Array<TDubboUrl>>) => void

0 comments on commit fbd1ec3

Please sign in to comment.