-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): update kafka framework & add test demo (#2236)
- Loading branch information
1 parent
501772e
commit 5eae117
Showing
23 changed files
with
514 additions
and
99 deletions.
There are no files selected for viewing
65 changes: 21 additions & 44 deletions
65
packages/decorator/src/decorator/microservice/kafkaListener.ts
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
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
4 changes: 4 additions & 0 deletions
4
packages/kafka/test/fixtures/base-app-auto-commit/package.json
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,4 @@ | ||
{ | ||
"name": "ali-demo" | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
packages/kafka/test/fixtures/base-app-auto-commit/src/comsumer/userConsumer.ts
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,31 @@ | ||
import { Provide, Consumer, MSListenerType, Inject, App, KafkaListener } from '@midwayjs/decorator'; | ||
import { KafkaMessage } from 'kafkajs'; | ||
import { Context, Application } from '../../../../../src'; | ||
|
||
@Provide() | ||
@Consumer(MSListenerType.KAFKA) | ||
export class UserConsumer { | ||
|
||
@App() | ||
app: Application; | ||
|
||
@Inject() | ||
ctx: Context; | ||
|
||
@Inject() | ||
logger; | ||
|
||
@KafkaListener('topic-test', { | ||
subscription: { | ||
fromBeginning: false, | ||
}, | ||
runConfig: { | ||
autoCommit: true, | ||
} | ||
}) | ||
async gotData(message: KafkaMessage) { | ||
console.info('gotData info'); | ||
this.logger.info('test output =>', message.offset + ' ' + message.key + ' ' + message.value.toString('utf8')); | ||
this.app.setAttr('total', this.app.getAttr<number>('total') + 1); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/kafka/test/fixtures/base-app-auto-commit/src/configuration.ts
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 @@ | ||
import { Configuration, App } from '@midwayjs/decorator'; | ||
import { ILifeCycle } from '@midwayjs/core'; | ||
import { IMidwayKafkaApplication } from '../../../../src'; | ||
|
||
@Configuration({ | ||
importConfigs: [ | ||
{ | ||
default: { | ||
kafka: { | ||
kafkaConfig: { | ||
clientId: 'my-app', | ||
brokers: [process.env.KAFKA_URL || 'localhost:9092'], | ||
}, | ||
consumerConfig: { | ||
groupId: 'groupId-test', | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}) | ||
export class AutoConfiguration implements ILifeCycle { | ||
@App() | ||
app: IMidwayKafkaApplication; | ||
|
||
async onReady() { | ||
this.app.setAttr('total', 0); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/kafka/test/fixtures/base-app-auto-commit/src/service/user.ts
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,10 @@ | ||
import { Provide } from '@midwayjs/decorator'; | ||
|
||
@Provide() | ||
export class UserService { | ||
async hello(name) { | ||
return { | ||
name, | ||
}; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/kafka/test/fixtures/base-app-manual-committing/package.json
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,4 @@ | ||
{ | ||
"name": "ali-demo" | ||
} | ||
|
37 changes: 37 additions & 0 deletions
37
packages/kafka/test/fixtures/base-app-manual-committing/src/comsumer/userConsumer.ts
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,37 @@ | ||
import { Provide, Consumer, MSListenerType, Inject, App, KafkaListener } from '@midwayjs/decorator'; | ||
import { KafkaMessage } from 'kafkajs'; | ||
import { Context, Application } from '../../../../../src'; | ||
|
||
@Provide() | ||
@Consumer(MSListenerType.KAFKA) | ||
export class UserConsumer { | ||
|
||
@App() | ||
app: Application; | ||
|
||
@Inject() | ||
ctx: Context; | ||
|
||
@Inject() | ||
logger; | ||
|
||
@KafkaListener('topic-test0', { | ||
subscription: { | ||
fromBeginning: false, | ||
}, | ||
runConfig: { | ||
autoCommit: false, | ||
} | ||
}) | ||
async gotData(message: KafkaMessage) { | ||
console.info('gotData info'); | ||
this.logger.info('test output =>', message.offset + ' ' + message.key + ' ' + message.value.toString('utf8')); | ||
try { | ||
// 抛出异常,当出现异常的时候,需要设置commitOffsets偏移到异常的位置,用于重新执行消费,所以这里应该出现的消费是2次,total为2 | ||
throw new Error("error"); | ||
} catch (error) { | ||
this.ctx.commitOffsets(message.offset); | ||
} | ||
this.app.setAttr('total', this.app.getAttr<number>('total') + 1); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/kafka/test/fixtures/base-app-manual-committing/src/configuration.ts
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 @@ | ||
import { Configuration, App } from '@midwayjs/decorator'; | ||
import { ILifeCycle } from '@midwayjs/core'; | ||
import { IMidwayKafkaApplication } from '../../../../src'; | ||
|
||
@Configuration({ | ||
importConfigs: [ | ||
{ | ||
default: { | ||
kafka: { | ||
kafkaConfig: { | ||
clientId: 'my-app', | ||
brokers: [process.env.KAFKA_URL || 'localhost:9092'], | ||
}, | ||
consumerConfig: { | ||
groupId: 'groupId-test', | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}) | ||
export class AutoConfiguration implements ILifeCycle { | ||
@App() | ||
app: IMidwayKafkaApplication; | ||
|
||
async onReady() { | ||
this.app.setAttr('total', 0); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/kafka/test/fixtures/base-app-manual-committing/src/service/user.ts
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,10 @@ | ||
import { Provide } from '@midwayjs/decorator'; | ||
|
||
@Provide() | ||
export class UserService { | ||
async hello(name) { | ||
return { | ||
name, | ||
}; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/kafka/test/fixtures/base-app-multi-different-topic/package.json
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,4 @@ | ||
{ | ||
"name": "ali-demo" | ||
} | ||
|
Oops, something went wrong.