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 858b4cc fixed dubbo-service registry meta info and refactor
dubbo-setting
858b4cc is described below
commit 858b4ccec011ab4ce5052bd6a44bdc62a16c8729
Author: hufeng <[email protected]>
AuthorDate: Mon Aug 2 11:11:06 2021 +0800
fixed dubbo-service registry meta info and refactor dubbo-setting
---
docs/middleware.md | 38 ++---
.../src/__tests__/dubbo-setting.test.ts | 155 ++++++++++++---------
packages/dubbo-consumer/src/dubbo-setting.ts | 64 +++++++--
packages/dubbo-consumer/src/dubbo.ts | 31 ++---
packages/dubbo-consumer/src/index.ts | 4 +-
.../src/__tests__/dubbo-setting.test.ts | 63 ++++++---
packages/dubbo-service/src/dubbo-service.ts | 39 +++---
packages/dubbo-service/src/dubbo-setting.ts | 75 ++++++++--
packages/dubbo-service/src/index.ts | 4 +-
9 files changed, 305 insertions(+), 168 deletions(-)
diff --git a/docs/middleware.md b/docs/middleware.md
index c665f30..2da61e4 100644
--- a/docs/middleware.md
+++ b/docs/middleware.md
@@ -20,14 +20,14 @@ middleware 是很多 web 框架设计的非常好的一个扩展方案如 expres
```typescript
const dubbo = new Dubbo({
/*..各种参数..*/
-});
+})
dubbo.use(async (ctx, next) => {
- const startTime = Date.now();
- await next();
- const endTime = Date.now();
- console.log('costtime: %d', endTime - startTime);
-});
+ const startTime = Date.now()
+ await next()
+ const endTime = Date.now()
+ console.log('costtime: %d', endTime - startTime)
+})
```
在这个基础上我们去实现 node 和 dubbo 日志的跟踪就变得很简单了,可以从 ctx 中获取调用链路上各种参数。
@@ -43,33 +43,33 @@ npm install dubbo-invoker
```
```javascript
-import {dubboInvoker, matcher} from 'dubbo-invoker';
+import { dubboInvoker, matcher } from 'dubbo-invoker'
//init
-const dubbo = Dubbo.from(/*....*/);
+const dubbo = Dubbo.from(/*....*/)
const matchRuler = matcher
//精确匹配接口
- .match('com.alibaba.demo.UserProvider', {
+ .service('com.alibaba.demo.UserProvider', {
version: '1.0.0',
- group: 'user',
+ group: 'user'
})
- //match thunk
- .match(ctx => {
+ //service thunk
+ .service((ctx) => {
if (ctx.dubboInterface === 'com.alibaba.demo.ProductProvider') {
- ctx.version = '2.0.0';
- ctx.group = 'product-center';
+ ctx.version = '2.0.0'
+ ctx.group = 'product-center'
//通知dubboInvoker匹配成功
- return true;
+ return true
}
})
//正则匹配
- .match(/^com.alibaba.dubbo/, {
+ .service(/^com.alibaba.dubbo/, {
version: '2.0.0',
- group: '',
- });
+ group: ''
+ })
-dubbo.use(dubboInvoke(matchRuler));
+dubbo.use(dubboInvoke(matchRuler))
```
<strong> 我想应该还有其他的花式玩法,㊗ ️😊 </strong>
diff --git a/packages/dubbo-consumer/src/__tests__/dubbo-setting.test.ts
b/packages/dubbo-consumer/src/__tests__/dubbo-setting.test.ts
index 59ad0d9..80eaea2 100644
--- a/packages/dubbo-consumer/src/__tests__/dubbo-setting.test.ts
+++ b/packages/dubbo-consumer/src/__tests__/dubbo-setting.test.ts
@@ -15,91 +15,114 @@
* limitations under the License.
*/
-import { dubboSetting } from '../dubbo-setting'
-
-it('test config string', () => {
- const cfg = dubboSetting
- .match('com.hello.a.service', { group: 'A', version: '1.0.0' })
- .match('com.hello.b.service', { group: 'b', version: '1.0.0' })
-
- expect(
- cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.a.service' })
- ).toEqual({
- group: 'A',
- version: '1.0.0'
- })
+import * as s from '../dubbo-setting'
+
+describe('dubbo-consumer setting test suite', () => {
+ it('test maxTimeout', () => {
+ const cfg = s.Setting()
+ expect(cfg.maxTimeout).toEqual(5 * 1000)
- expect(
- cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.b.service' })
- ).toEqual({
- group: 'b',
- version: '1.0.0'
+ const cfg1 = s.Setting(s.maxTimeout(10 * 1000))
+ expect(cfg1.maxTimeout).toEqual(10 * 1000)
})
- expect(
- cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.c.service' })
- ).toBeNull()
-})
+ it('test config string', () => {
+ const cfg = s.Setting(
+ s.service('com.hello.a.service', { group: 'A', version: '1.0.0' }),
+ s.service('com.hello.b.service', { group: 'b', version: '1.0.0' })
+ )
-it('test config regx', () => {
- const cfg = dubboSetting
- .match(/com.hello.service*/, {
- group: 'regx',
+ expect(
+ cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.a.service' })
+ ).toEqual({
+ group: 'A',
version: '1.0.0'
})
- .match(/com.foo.service*/, { group: 'foo', version: '1.0.0' })
- expect(
- cfg.getDubboSetting({
- dubboServiceInterface: 'com.hello.service.addservice'
+ expect(
+ cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.b.service' })
+ ).toEqual({
+ group: 'b',
+ version: '1.0.0'
})
- ).toEqual({
- group: 'regx',
- version: '1.0.0'
- })
- expect(
- cfg.getDubboSetting({
- dubboServiceInterface: 'com.foo.service.subservice'
+ expect(
+ cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.c.service' })
+ ).toEqual({
+ group: '',
+ version: '0.0.0'
})
- ).toEqual({
- group: 'foo',
- version: '1.0.0'
})
- expect(
- cfg.getDubboSetting({ dubboServiceInterface: 'com.other.service' })
- ).toBeNull()
-})
+ it('test config regx', () => {
+ const cfg = s.Setting(
+ s.service(/com.hello.service*/, {
+ group: 'regx',
+ version: '1.0.0'
+ }),
+ s.service(/com.foo.service*/, { group: 'foo', version: '1.0.0' })
+ )
-it('test config thunk', () => {
- const cfg = dubboSetting.matchThunk((shortName: string) => {
- if (shortName === 'helloServiceGroupA') {
- return { group: 'A', version: '1.0.0' }
- }
+ expect(
+ cfg.getDubboSetting({
+ dubboServiceInterface: 'com.hello.service.addservice'
+ })
+ ).toEqual({
+ group: 'regx',
+ version: '1.0.0'
+ })
- if (shortName === 'fooService2') {
- return { group: '2', version: '1.0.0' }
- }
+ expect(
+ cfg.getDubboSetting({
+ dubboServiceInterface: 'com.foo.service.subservice'
+ })
+ ).toEqual({
+ group: 'foo',
+ version: '1.0.0'
+ })
- return null
+ expect(
+ cfg.getDubboSetting({ dubboServiceInterface: 'com.other.service' })
+ ).toEqual({
+ group: '',
+ version: '0.0.0'
+ })
})
- expect(
- cfg.getDubboSetting({ dubboServiceShortName: 'helloServiceGroupA' })
- ).toEqual({
- group: 'A',
- version: '1.0.0'
- })
+ it('test config thunk', () => {
+ const cfg = s.Setting(
+ s.serviceThunk((shortName: string) => {
+ if (shortName === 'helloServiceGroupA') {
+ return { group: 'A', version: '1.0.0' }
+ }
- expect(cfg.getDubboSetting({ dubboServiceShortName: 'fooService2'
})).toEqual(
- {
+ if (shortName === 'fooService2') {
+ return { group: '2', version: '1.0.0' }
+ }
+
+ return null
+ })
+ )
+
+ expect(
+ cfg.getDubboSetting({ dubboServiceShortName: 'helloServiceGroupA' })
+ ).toEqual({
+ group: 'A',
+ version: '1.0.0'
+ })
+
+ expect(
+ cfg.getDubboSetting({ dubboServiceShortName: 'fooService2' })
+ ).toEqual({
group: '2',
version: '1.0.0'
- }
- )
+ })
- expect(
- cfg.getDubboSetting({ dubboServiceShortName: 'barService' })
- ).toBeNull()
+ expect(
+ cfg.getDubboSetting({ dubboServiceShortName: 'barService' })
+ ).toEqual({
+ group: '',
+ version: '0.0.0'
+ })
+ })
})
diff --git a/packages/dubbo-consumer/src/dubbo-setting.ts
b/packages/dubbo-consumer/src/dubbo-setting.ts
index 49ab3bd..f4663c5 100644
--- a/packages/dubbo-consumer/src/dubbo-setting.ts
+++ b/packages/dubbo-consumer/src/dubbo-setting.ts
@@ -23,7 +23,11 @@ import {
TMatchThunk
} from './types'
+export type TSettingFunctionOption = (setting: DubboSetting) => void
+
export class DubboSetting {
+ maxTimeout: number = 5 * 1000
+
private readonly matchDubboInterface: Map<TDubboInterface, IDubboSetting>
private readonly matchDubboRegx: Map<RegExp, IDubboSetting>
private readonly matchDubboThunk: Set<TMatchThunk>
@@ -34,7 +38,7 @@ export class DubboSetting {
this.matchDubboThunk = new Set()
}
- match(
+ service(
rule: TDubboInterface | Array<TDubboInterface> | RegExp,
meta: IDubboSetting
) {
@@ -45,12 +49,10 @@ export class DubboSetting {
} else if (rule instanceof RegExp) {
this.matchDubboRegx.set(rule, meta)
}
- return this
}
- matchThunk(thunk: TMatchThunk) {
+ serviceThunk(thunk: TMatchThunk) {
this.matchDubboThunk.add(thunk)
- return this
}
getDubboSetting({
@@ -60,29 +62,71 @@ export class DubboSetting {
dubboServiceShortName?: TDubboServiceShortName
dubboServiceInterface?: TDubboInterface
}) {
+ const defaultMeta = {
+ group: '',
+ version: '0.0.0'
+ }
// first, we search thunk
for (let thunk of this.matchDubboThunk) {
const meta = thunk(dubboServiceShortName)
if (meta) {
- return meta
+ return {
+ ...defaultMeta,
+ ...meta
+ }
}
}
// second, search from dubboInterface
if (this.matchDubboInterface.has(dubboServiceInterface)) {
- return this.matchDubboInterface.get(dubboServiceInterface)
+ const meta = this.matchDubboInterface.get(dubboServiceInterface)
+ return {
+ ...defaultMeta,
+ ...meta
+ }
}
// third, search from regx
for (let [r, meta] of this.matchDubboRegx) {
if (r.test(dubboServiceInterface)) {
- return meta
+ return {
+ ...defaultMeta,
+ ...meta
+ }
}
}
- // no match anything
- return null
+ // no service anything
+ return defaultMeta
}
}
-export const dubboSetting = new DubboSetting()
+// ~~~~~~~~~~~~~ factory method ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+export function Setting(...args: Array<TSettingFunctionOption>) {
+ const dubboSetting = new DubboSetting()
+ for (let arg of args) {
+ arg(dubboSetting)
+ }
+ return dubboSetting
+}
+
+export function maxTimeout(timeout: number) {
+ return (dubboSetting: DubboSetting) => {
+ dubboSetting.maxTimeout = timeout
+ }
+}
+
+export function service(
+ rule: TDubboInterface | Array<TDubboInterface> | RegExp,
+ meta: IDubboSetting
+) {
+ return (dubboSetting: DubboSetting) => {
+ return dubboSetting.service(rule, meta)
+ }
+}
+
+export function serviceThunk(thunk: TMatchThunk) {
+ return (dubboSetting: DubboSetting) => {
+ dubboSetting.serviceThunk(thunk)
+ }
+}
diff --git a/packages/dubbo-consumer/src/dubbo.ts
b/packages/dubbo-consumer/src/dubbo.ts
index a29b686..cdd62f1 100644
--- a/packages/dubbo-consumer/src/dubbo.ts
+++ b/packages/dubbo-consumer/src/dubbo.ts
@@ -24,7 +24,7 @@ import { go, util } from 'apache-dubbo-common'
import Scheduler from './scheduler'
import qs from 'querystring'
import ip from 'ip'
-import { DubboSetting } from './dubbo-setting'
+import * as s from './dubbo-setting'
import {
IDubboProps,
IDubboProvider,
@@ -53,7 +53,7 @@ log('dubbo-js version :=> %s', packageVersion)
*/
export default class Dubbo<TService = Object> {
private readonly queue: Queue
- private readonly dubboSetting: DubboSetting
+ private readonly dubboSetting: s.DubboSetting
private readonly props: IDubboProps
private readonly middlewares: Array<Middleware<Context>>
private readonly consumers: Array<{
@@ -74,7 +74,7 @@ export default class Dubbo<TService = Object> {
this.consumers = []
this.middlewares = []
this.queue = Queue.init()
- this.dubboSetting = props.dubboSetting
+ this.dubboSetting = props.dubboSetting || s.Setting()
// init service
this.service = <TDubboService<TService>>{}
@@ -86,7 +86,10 @@ export default class Dubbo<TService = Object> {
//For the consumer client, if the user sets the interface level timeout
time, the interface level is used
//If the user does not set the user level, the default is the maximum
timeout
const { dubboInvokeTimeout } = props
- config.dubboInvokeTimeout = dubboInvokeTimeout || config.dubboInvokeTimeout
+ config.dubboInvokeTimeout =
+ dubboInvokeTimeout ||
+ this.dubboSetting.maxTimeout ||
+ config.dubboInvokeTimeout
log('config:|> %O', config)
@@ -110,15 +113,10 @@ export default class Dubbo<TService = Object> {
private consumeService(services: Object) {
for (let [shortName, serviceProxy] of Object.entries(services)) {
const service = serviceProxy(this) as IDubboProvider
- const meta = this.dubboSetting
- ? this.dubboSetting.getDubboSetting({
- dubboServiceShortName: shortName,
- dubboServiceInterface: service.dubboInterface
- })
- : {
- group: '',
- version: '0.0.0'
- }
+ const meta = this.dubboSetting.getDubboSetting({
+ dubboServiceShortName: shortName,
+ dubboServiceInterface: service.dubboInterface
+ })
service.group = meta.group
service.version = meta.version
this.service[shortName] = this.composeService(service)
@@ -134,16 +132,17 @@ export default class Dubbo<TService = Object> {
dubboServiceInterface: dubboInterface,
dubboServiceUrl:
`consumer://${ip.address()}/${dubboInterface}?${qs.stringify(
{
- interface: dubboInterface,
application: this.props.application.name,
+ interface: dubboInterface,
category: 'consumers',
method: '',
- revision: '',
+ revision: version,
version: group,
group: version,
timeout: timeout,
side: 'consumer',
- check: false
+ check: false,
+ pid: process.pid
}
)}`
})
diff --git a/packages/dubbo-consumer/src/index.ts
b/packages/dubbo-consumer/src/index.ts
index 49b51aa..287ab78 100644
--- a/packages/dubbo-consumer/src/index.ts
+++ b/packages/dubbo-consumer/src/index.ts
@@ -17,8 +17,8 @@
import java from 'js-to-java'
import Dubbo from './dubbo'
-import { dubboSetting } from './dubbo-setting'
+import * as s from './dubbo-setting'
import DubboDirectlyInvoker from './dubbo-directly-invoker'
import { TDubboCallResult } from './types'
-export { java, Dubbo, DubboDirectlyInvoker, dubboSetting, TDubboCallResult }
+export { java, Dubbo, DubboDirectlyInvoker, s, TDubboCallResult }
diff --git a/packages/dubbo-service/src/__tests__/dubbo-setting.test.ts
b/packages/dubbo-service/src/__tests__/dubbo-setting.test.ts
index 97e3428..086dbe4 100644
--- a/packages/dubbo-service/src/__tests__/dubbo-setting.test.ts
+++ b/packages/dubbo-service/src/__tests__/dubbo-setting.test.ts
@@ -15,13 +15,24 @@
* limitations under the License.
*/
-import { dubboSetting } from '../dubbo-setting'
+import * as s from '../dubbo-setting'
describe('dubbo-service dubbo setting test suite', () => {
+ it('test group and version', () => {
+ const cfg = s.Setting()
+ expect(cfg.version).toEqual('0.0.0')
+ expect(cfg.group).toEqual('')
+
+ const cfg1 = s.Setting(s.group('a'), s.version('1.0.0'))
+ expect(cfg1.group).toEqual('a')
+ expect(cfg1.version).toEqual('1.0.0')
+ })
+
it('test config string', () => {
- const cfg = dubboSetting
- .match('com.hello.a.service', { group: 'A', version: '1.0.0' })
- .match('com.hello.b.service', { group: 'b', version: '1.0.0' })
+ const cfg = s.Setting(
+ s.service('com.hello.a.service', { group: 'A', version: '1.0.0' }),
+ s.service('com.hello.b.service', { group: 'b', version: '1.0.0' })
+ )
expect(
cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.a.service' })
@@ -39,16 +50,20 @@ describe('dubbo-service dubbo setting test suite', () => {
expect(
cfg.getDubboSetting({ dubboServiceInterface: 'com.hello.c.service' })
- ).toBeNull()
+ ).toEqual({
+ group: '',
+ version: '0.0.0'
+ })
})
it('test config regx', () => {
- const cfg = dubboSetting
- .match(/com.hello.service*/, {
+ const cfg = s.Setting(
+ s.service(/com.hello.service*/, {
group: 'regx',
version: '1.0.0'
- })
- .match(/com.foo.service*/, { group: 'foo', version: '1.0.0' })
+ }),
+ s.service(/com.foo.service*/, { group: 'foo', version: '1.0.0' })
+ )
expect(
cfg.getDubboSetting({
@@ -70,21 +85,26 @@ describe('dubbo-service dubbo setting test suite', () => {
expect(
cfg.getDubboSetting({ dubboServiceInterface: 'com.other.service' })
- ).toBeNull()
+ ).toEqual({
+ group: '',
+ version: '0.0.0'
+ })
})
it('test config thunk', () => {
- const cfg = dubboSetting.matchThunk((shortName: string) => {
- if (shortName === 'helloServiceGroupA') {
- return { group: 'A', version: '1.0.0' }
- }
+ const cfg = s.Setting(
+ s.serviceThunk((shortName: string) => {
+ if (shortName === 'helloServiceGroupA') {
+ return { group: 'A', version: '1.0.0' }
+ }
- if (shortName === 'fooService2') {
- return { group: '2', version: '1.0.0' }
- }
+ if (shortName === 'fooService2') {
+ return { group: '2', version: '1.0.0' }
+ }
- return null
- })
+ return null
+ })
+ )
expect(
cfg.getDubboSetting({ dubboServiceShortName: 'helloServiceGroupA' })
@@ -102,6 +122,9 @@ describe('dubbo-service dubbo setting test suite', () => {
expect(
cfg.getDubboSetting({ dubboServiceShortName: 'barService' })
- ).toBeNull()
+ ).toEqual({
+ version: '0.0.0',
+ group: ''
+ })
})
})
diff --git a/packages/dubbo-service/src/dubbo-service.ts
b/packages/dubbo-service/src/dubbo-service.ts
index ca5d455..1c7b6dc 100644
--- a/packages/dubbo-service/src/dubbo-service.ts
+++ b/packages/dubbo-service/src/dubbo-service.ts
@@ -31,7 +31,7 @@ import {
} from 'apache-dubbo-serialization'
import Context from './context'
import { randomPort } from './port'
-import { DubboSetting } from './dubbo-setting'
+import * as s from './dubbo-setting'
import {
DubboServiceClazzName,
IDubboServerProps,
@@ -41,6 +41,7 @@ import {
TDubboServiceUrl
} from './types'
+const ipAddr = ip.address()
const log = debug('dubbo-server ~')
/**
@@ -60,7 +61,7 @@ export default class DubboService {
private retry: Retry
private port: number
private server: net.Server
- private readonly dubboSetting: DubboSetting
+ private readonly dubboSetting: s.DubboSetting
private registry: IRegistry
private readonly services: { [name in string]: IDubboService }
private serviceRouter: Map<DubboServiceClazzName, Array<IDubboService>>
@@ -76,7 +77,7 @@ export default class DubboService {
})
// init dubbo setting
- this.dubboSetting = props.dubboSetting
+ this.dubboSetting = props.dubboSetting || s.Setting()
// init registry
this.registry = props.registry
@@ -174,6 +175,7 @@ export default class DubboService {
*/
private async invokeComposeChainRequest(data: Buffer) {
const request = decodeDubboRequest(data)
+ log('receive request %O', request)
const service = this.matchService(request)
const ctx = new Context(request)
@@ -184,6 +186,9 @@ export default class DubboService {
// service not found
if (!service) {
+ log(
+ `Service not found with ${path} and ${methodName}, group:${group},
version:${version}`
+ )
ctx.status = DUBBO_RESPONSE_STATUS.SERVICE_NOT_FOUND
ctx.body.err = new Error(
`Service not found with ${path} and ${methodName}, group:${group},
version:${version}`
@@ -197,10 +202,6 @@ export default class DubboService {
const method = service.methods[request.methodName]
ctx.status = DUBBO_RESPONSE_STATUS.OK
try {
- const res = await method.apply(service, [
- ...(request.args || []),
- ctx
- ])
// FIXEDME waiting dubbo/dj
// check hessian type
// if (!util.checkRetValHessian(res)) {
@@ -209,7 +210,10 @@ export default class DubboService {
// )
// return
// }
- ctx.body.res = res
+ ctx.body.res = await method.apply(service, [
+ ...(request.args || []),
+ ctx
+ ])
} catch (err) {
log(`handle request error %s`, err)
ctx.body.err = err
@@ -237,7 +241,7 @@ export default class DubboService {
private async registerServices() {
await this.registry.ready().catch((err) => {
log('registry service error %s', err)
- this.reject()
+ this.reject(err)
throw err
})
@@ -248,12 +252,10 @@ export default class DubboService {
for (let [dubboServiceShortName, service] of Object.entries(
this.services
)) {
- const meta = this.dubboSetting
- ? this.dubboSetting.getDubboSetting({
- dubboServiceShortName,
- dubboServiceInterface: service.dubboInterface
- })
- : { group: '', version: '0.0.0' }
+ const meta = this.dubboSetting.getDubboSetting({
+ dubboServiceShortName,
+ dubboServiceInterface: service.dubboInterface
+ })
service.group = meta.group
service.version = meta.version
@@ -286,22 +288,17 @@ export default class DubboService {
* @returns
*/
private buildUrl(service: IDubboService) {
- const ipAddr = ip.address()
const { dubboInterface, group, version, methods } = service
const methodName = Object.keys(methods).join()
-
return (
`dubbo://${ipAddr}:${this.port}/${dubboInterface}?` +
qs.stringify({
group,
version,
- method: methodName,
+ methods: methodName,
side: 'provider',
pid: process.pid,
- generic: false,
protocol: 'dubbo',
- dynamic: true,
- category: 'providers',
anyhost: true,
timestamp: Date.now()
})
diff --git a/packages/dubbo-service/src/dubbo-setting.ts
b/packages/dubbo-service/src/dubbo-setting.ts
index 2bcc38f..2a430cc 100644
--- a/packages/dubbo-service/src/dubbo-setting.ts
+++ b/packages/dubbo-service/src/dubbo-setting.ts
@@ -23,13 +23,18 @@ import {
TMatchThunk
} from './types'
+export type TSettingFunctionOption = (setting: DubboSetting) => void
+
export class DubboSetting {
+ group: string = ''
+ version: string = '0.0.0'
+
+ private readonly matchDubboRegx: Map<RegExp, IDubboServiceSetting>
+ private readonly matchDubboThunk: Set<TMatchThunk>
private readonly matchDubboInterface: Map<
TDubboServiceInterface,
IDubboServiceSetting
>
- private readonly matchDubboRegx: Map<RegExp, IDubboServiceSetting>
- private readonly matchDubboThunk: Set<TMatchThunk>
constructor() {
this.matchDubboInterface = new Map()
@@ -37,7 +42,7 @@ export class DubboSetting {
this.matchDubboThunk = new Set()
}
- match(
+ service(
rule: TDubboServiceInterface | Array<TDubboServiceInterface> | RegExp,
meta: IDubboServiceSetting
) {
@@ -48,12 +53,10 @@ export class DubboSetting {
} else if (rule instanceof RegExp) {
this.matchDubboRegx.set(rule, meta)
}
- return this
}
- matchThunk(thunk: TMatchThunk) {
+ serviceThunk(thunk: TMatchThunk) {
this.matchDubboThunk.add(thunk)
- return this
}
getDubboSetting({
@@ -63,29 +66,77 @@ export class DubboSetting {
dubboServiceShortName?: TDubboServiceShortName
dubboServiceInterface?: TDubboServiceInterface
}) {
+ const defaultMeta = {
+ group: this.group,
+ version: this.version
+ }
// first, we search thunk
for (let thunk of this.matchDubboThunk) {
const meta = thunk(dubboServiceShortName)
if (meta) {
- return meta
+ return {
+ ...defaultMeta,
+ ...meta
+ }
}
}
// second, search from dubboInterface
if (this.matchDubboInterface.has(dubboServiceInterface)) {
- return this.matchDubboInterface.get(dubboServiceInterface)
+ const meta = this.matchDubboInterface.get(dubboServiceInterface)
+ return {
+ ...defaultMeta,
+ ...meta
+ }
}
// third, search from regx
for (let [r, meta] of this.matchDubboRegx) {
if (r.test(dubboServiceInterface)) {
- return meta
+ return {
+ ...defaultMeta,
+ ...meta
+ }
}
}
- // no match anything
- return null
+ // no service anything
+ return defaultMeta
+ }
+}
+
+// ~~~~~~~~~~~~~ factory method ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+export function Setting(...args: Array<TSettingFunctionOption>) {
+ const dubboSetting = new DubboSetting()
+ for (let arg of args) {
+ arg(dubboSetting)
}
+ return dubboSetting
}
-export const dubboSetting = new DubboSetting()
+export function group(g: string) {
+ return (dubboSetting: DubboSetting) => {
+ dubboSetting.group = g
+ }
+}
+
+export function version(v: string) {
+ return (dubboSetting: DubboSetting) => {
+ dubboSetting.version = v
+ }
+}
+
+export function service(
+ rule: TDubboServiceInterface | Array<TDubboServiceInterface> | RegExp,
+ meta: IDubboServiceSetting
+) {
+ return (dubboSetting: DubboSetting) => {
+ return dubboSetting.service(rule, meta)
+ }
+}
+
+export function serviceThunk(thunk: TMatchThunk) {
+ return (dubboSetting: DubboSetting) => {
+ dubboSetting.serviceThunk(thunk)
+ }
+}
diff --git a/packages/dubbo-service/src/index.ts
b/packages/dubbo-service/src/index.ts
index 023b2ad..c514b6e 100644
--- a/packages/dubbo-service/src/index.ts
+++ b/packages/dubbo-service/src/index.ts
@@ -16,7 +16,7 @@
*/
import DubboService from './dubbo-service'
-import { dubboSetting } from './dubbo-setting'
+import * as s from './dubbo-setting'
import { IDubboService } from './types'
-export { DubboService, dubboSetting, IDubboService }
+export { DubboService, s, IDubboService }