You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by hu...@apache.org on 2021/06/24 23:53:18 UTC

[dubbo-js] branch master updated: enhancement: customize zkRootPath

This is an automated email from the ASF dual-hosted git repository.

hufeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-js.git


The following commit(s) were added to refs/heads/master by this push:
     new 355bee9  enhancement: customize zkRootPath
355bee9 is described below

commit 355bee9366b1f8c1c3ab905598fd7340a011518d
Author: hufeng <fe...@gmail.com>
AuthorDate: Fri Jun 25 07:51:42 2021 +0800

    enhancement: customize zkRootPath
---
 examples/fullstack/hello-api/server-zk.ts          |  1 +
 examples/fullstack/hello-service/server-nacos.ts   |  1 +
 examples/fullstack/hello-service/server-zk.ts      |  1 +
 .../src/__tests__/registry-zookeeper-test.ts       | 22 +++++++++++++++++++++-
 packages/dubbo-registry/src/registry-zookeeper.ts  | 14 ++++++++------
 packages/dubbo-registry/src/types.ts               |  1 +
 6 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/examples/fullstack/hello-api/server-zk.ts b/examples/fullstack/hello-api/server-zk.ts
index 9b84970..4183090 100644
--- a/examples/fullstack/hello-api/server-zk.ts
+++ b/examples/fullstack/hello-api/server-zk.ts
@@ -28,6 +28,7 @@ const dubbo = new Dubbo<typeof services>({
   services
 })
 
+// start server
 const server = new Koa()
 server.use(async (ctx) => {
   const { res, err } = await dubbo.service.helloService.hello('dubbo-js')
diff --git a/examples/fullstack/hello-service/server-nacos.ts b/examples/fullstack/hello-service/server-nacos.ts
index ca2f1d9..fc28205 100644
--- a/examples/fullstack/hello-service/server-nacos.ts
+++ b/examples/fullstack/hello-service/server-nacos.ts
@@ -19,6 +19,7 @@ import { DubboService } from '@apache/dubbo-service'
 import { Nacos } from '@apache/dubbo-registry'
 import services from './service'
 
+// start
 ;(async function main() {
   const dubbo = new DubboService({
     // registry: Zk({ connect: 'localhost:2181' }),
diff --git a/examples/fullstack/hello-service/server-zk.ts b/examples/fullstack/hello-service/server-zk.ts
index 231b273..cae494a 100644
--- a/examples/fullstack/hello-service/server-zk.ts
+++ b/examples/fullstack/hello-service/server-zk.ts
@@ -19,6 +19,7 @@ import { DubboService } from '@apache/dubbo-service'
 import { Zk } from '@apache/dubbo-registry'
 import services from './service'
 
+// start
 ;(async function main() {
   const dubbo = new DubboService({
     registry: Zk({ connect: 'localhost:2181' }),
diff --git a/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts b/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts
index 565b09b..9a12ed6 100644
--- a/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts
+++ b/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts
@@ -27,7 +27,27 @@ describe('test zookeeper registry', () => {
       connect: 'localhost:2181',
       timeout: 5000,
       debug_level: Zookeeper.constants.ZOO_LOG_LEVEL_WARN,
-      host_order_deterministic: false
+      host_order_deterministic: false,
+      zkRootPath: '/dubbo'
+    })
+    const res = await zk.ready()
+    expect(res).toBeUndefined()
+
+    zk.close()
+  })
+
+  it('test zk with zkRootPath and ready ok', async () => {
+    const zk = Zk({
+      connect: 'localhost:2181',
+      zkRootPath: '/test/com.demo.dubbo'
+    })
+
+    expect(zk.getProps()).toEqual({
+      connect: 'localhost:2181',
+      timeout: 5000,
+      debug_level: Zookeeper.constants.ZOO_LOG_LEVEL_WARN,
+      host_order_deterministic: false,
+      zkRootPath: '/test/com.demo.dubbo'
     })
     const res = await zk.ready()
     expect(res).toBeUndefined()
diff --git a/packages/dubbo-registry/src/registry-zookeeper.ts b/packages/dubbo-registry/src/registry-zookeeper.ts
index a24de05..1bb009a 100644
--- a/packages/dubbo-registry/src/registry-zookeeper.ts
+++ b/packages/dubbo-registry/src/registry-zookeeper.ts
@@ -33,8 +33,8 @@ const dlog = debug('dubbo:zookeeper~')
 export class ZookeeperRegistry
   extends BaseRegistry
   implements IRegistry<Zookeeper> {
-  private client: Zookeeper
   private readonly props: IZkClientConfig
+  private client: Zookeeper
   private timeout: Timeout
 
   private readonly readyPromise: Promise<void>
@@ -46,6 +46,8 @@ export class ZookeeperRegistry
     dlog(`init zookeeper with %O`, props)
     this.props = props
 
+    this.props.zkRootPath = this.props.zkRootPath || DUBBO_ZK_ROOT_PATH
+
     // init ready promise
     this.readyPromise = new Promise((resolve, reject) => {
       this.resolve = resolve
@@ -86,7 +88,7 @@ export class ZookeeperRegistry
 
       try {
         // create root node
-        await this.mkdirp(DUBBO_ZK_ROOT_PATH)
+        await this.mkdirp(this.props.zkRootPath)
         // trigger ready promise
         this.resolve()
       } catch (err) {
@@ -142,7 +144,7 @@ export class ZookeeperRegistry
   }
 
   private wrapWatch(dubboInterface: string) {
-    const servicePath = `${DUBBO_ZK_ROOT_PATH}/${dubboInterface}/providers`
+    const servicePath = `${this.props.zkRootPath}/${dubboInterface}/providers`
     return async (type: number, state: number) => {
       dlog('wrapWatch %s %d %d', servicePath, type, state)
       await this.findDubboServiceUrl(dubboInterface)
@@ -171,7 +173,7 @@ export class ZookeeperRegistry
   }
 
   async findDubboServiceUrl(dubboInterface: string) {
-    const servicePath = `${DUBBO_ZK_ROOT_PATH}/${dubboInterface}/providers`
+    const servicePath = `${this.props.zkRootPath}/${dubboInterface}/providers`
     const urls = (
       await this.client
         .w_get_children(servicePath, this.wrapWatch(dubboInterface))
@@ -198,7 +200,7 @@ export class ZookeeperRegistry
   ) {
     for (let { dubboServiceInterface, dubboServiceUrl } of services) {
       // create service root path
-      const serviceRootPath = `${DUBBO_ZK_ROOT_PATH}/${dubboServiceInterface}/providers`
+      const serviceRootPath = `${this.props.zkRootPath}/${dubboServiceInterface}/providers`
       await this.mkdirp(serviceRootPath)
       // create service node
       await this.createNode({
@@ -219,7 +221,7 @@ export class ZookeeperRegistry
     for (let { dubboServiceInterface, dubboServiceUrl } of consumers) {
       dubboInterfaces.add(dubboServiceInterface)
       // create consumer root path
-      const consumerRootPath = `${DUBBO_ZK_ROOT_PATH}/${dubboServiceInterface}/consumers`
+      const consumerRootPath = `${this.props.zkRootPath}/${dubboServiceInterface}/consumers`
       await this.mkdirp(consumerRootPath)
       // create service node
       await this.createNode({
diff --git a/packages/dubbo-registry/src/types.ts b/packages/dubbo-registry/src/types.ts
index 6411902..f3efb97 100644
--- a/packages/dubbo-registry/src/types.ts
+++ b/packages/dubbo-registry/src/types.ts
@@ -37,6 +37,7 @@ export interface IZkClientConfig {
   timeout?: number
   debug_level?: number
   host_order_deterministic?: boolean
+  zkRootPath?: string
 }
 
 export interface INodeProps {