Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
logtransfer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
谢宇轩
logtransfer
Commits
42d45662
Commit
42d45662
authored
Dec 28, 2021
by
谢宇轩
😅
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加多个消费者协程
parent
5f738b70
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
66 deletions
+84
-66
customer.go
source/customer.go
+62
-51
kafka.go
source/kafka.go
+21
-14
kernel.go
transfer/kernel.go
+1
-1
No files found.
source/customer.go
View file @
42d45662
...
...
@@ -18,7 +18,8 @@ var (
// 消费处理器
type
Customer
struct
{
Reader
*
kafka
.
Reader
// 特定消费者的专属Kafka Reader (我从哪里来)
Readers
[]
*
kafka
.
Reader
// 特定消费者的专属Kafka Reader Slice (我从哪里来)
Topic
*
Topic
HandlePipeline
*
plugin
.
PipeLine
// 从Topic中构建的Piepline (要到那里去)
Format
entity
.
Formater
// 解析元数据的格式器 (变形记。。)
done
chan
struct
{}
// 结束标志
...
...
@@ -26,9 +27,7 @@ type Customer struct {
// 结束一个消费处理器
func
(
c
Customer
)
Exit
()
{
go
func
()
{
c
.
done
<-
struct
{}{}
}()
}
// 结束信号监听
...
...
@@ -37,17 +36,17 @@ func (c Customer) Listen() chan struct{} {
}
// 初始化一个消费处理器
func
InitCustomer
(
topic
*
Topic
)
*
Customer
{
func
InitCustomer
(
topic
*
Topic
)
*
Customer
{
GroupID
:=
topic
.
Name
+
"_group"
r
:=
InitReader
(
topic
.
Name
,
GroupID
)
log
.
Printf
(
"Check Customer group of [%s] success!"
,
GroupID
)
return
&
Customer
{
Reader
:
r
,
done
:
make
(
chan
struct
{}),
HandlePipeline
:
topic
.
PipeLine
,
Format
:
topic
.
Format
}
return
&
Customer
{
Topic
:
topic
,
Readers
:
r
,
done
:
make
(
chan
struct
{}),
HandlePipeline
:
topic
.
PipeLine
,
Format
:
topic
.
Format
}
}
// 全局的注册当前工作的消费处理器
func
RegisterManger
(
c
*
Customer
)
{
mu
.
Lock
()
CustomerManger
[
c
.
Reader
.
Config
()
.
Topic
]
=
c
CustomerManger
[
c
.
Topic
.
Name
]
=
c
mu
.
Unlock
()
}
...
...
@@ -59,6 +58,7 @@ func GetCustomer(topic string) (customer *Customer, ok bool) {
return
customer
,
ok
}
// 获取全部的注册过的消费处理器 所使用的的Topic名字
func
GetRegisterTopics
()
(
topics
[]
string
)
{
mu
.
Lock
()
...
...
@@ -69,33 +69,30 @@ func GetRegisterTopics() (topics []string) {
return
topics
}
// 從Kafka中消費消息,注意这里会提交commit offset
func
ReadingMessage
(
ctx
context
.
Context
,
c
*
Customer
)
{
defer
c
.
Reader
.
Close
()
log
.
Printf
(
"Start Customer Group[%s] success!"
,
c
.
Reader
.
Config
()
.
GroupID
)
readyToRead
:=
make
(
chan
*
kafka
.
Reader
)
// var trycount int
// var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海时区
go
func
(
ctx
context
.
Context
,
c
*
Customer
)
{
for
{
select
{
case
<-
c
.
Listen
()
:
return
case
<-
ctx
.
Done
()
:
c
.
Exit
()
case
reader
:=
<-
readyToRead
:
defer
reader
.
Close
()
go
func
(
ctx
context
.
Context
,
c
*
Customer
)
{
var
errMessage
strings
.
Builder
var
matedata
entity
.
Matedata
for
{
select
{
case
<-
c
.
Listen
()
:
// 监听需要关闭的信号
case
<-
ctx
.
Done
()
:
c
.
Exit
()
log
.
Println
(
"Close customer of Topic :"
,
c
.
Reader
.
Config
()
.
Topic
)
return
default
:
// // 使用超时上下文, 但是这样是非阻塞,超过deadline就报错了
// // timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
// // 这里使用阻塞的上下文
m
,
err
:=
c
.
Reader
.
ReadMessage
(
ctx
)
m
,
err
:=
reader
.
ReadMessage
(
ctx
)
if
err
!=
nil
{
// 退出
...
...
@@ -108,7 +105,7 @@ func ReadingMessage(ctx context.Context, c *Customer) {
continue
}
matedata
,
err
=
c
.
Format
(
string
(
c
.
R
eader
.
Config
()
.
Topic
),
string
(
m
.
Value
))
matedata
,
err
=
c
.
Format
(
string
(
r
eader
.
Config
()
.
Topic
),
string
(
m
.
Value
))
if
err
!=
nil
{
errMessage
.
Reset
()
errMessage
.
WriteString
(
"Format Error"
)
...
...
@@ -120,4 +117,18 @@ func ReadingMessage(ctx context.Context, c *Customer) {
c
.
HandlePipeline
.
Enter
(
matedata
)
}
}
}(
ctx
,
c
)
}
}
}(
ctx
,
c
)
for
_
,
p
:=
range
c
.
Readers
{
log
.
Printf
(
"Start Customer Group[%s][%d] success!"
,
p
.
Config
()
.
GroupID
,
p
.
Config
()
.
Partition
)
readyToRead
<-
p
}
}
source/kafka.go
View file @
42d45662
...
...
@@ -8,21 +8,29 @@ import (
"github.com/y7ut/logtransfer/conf"
)
func
InitReader
(
topic
string
,
groupId
string
)
*
kafka
.
Reader
{
func
InitReader
(
topic
string
,
groupId
string
)
[]
*
kafka
.
Reader
{
// // 先去创建一下这个分组
// CreateCustomerGroup(topic, groupId)
// make a writer that produces to topic-A, using the least-bytes distribution
r
:=
kafka
.
NewReader
(
kafka
.
ReaderConfig
{
var
readers
[]
*
kafka
.
Reader
for
i
:=
0
;
i
<
10
;
i
++
{
readers
=
append
(
readers
,
kafka
.
NewReader
(
kafka
.
ReaderConfig
{
Brokers
:
strings
.
Split
(
conf
.
APPConfig
.
Kafka
.
Address
,
","
),
Topic
:
topic
,
GroupID
:
groupId
,
Partition
:
0
,
//
Partition: 0,
MinBytes
:
10e3
,
// 10KB
MaxBytes
:
10e6
,
// 10MB
})
return
r
}))
}
// readers = append(readers, kafka.NewReader(kafka.ReaderConfig{
// Brokers: strings.Split(conf.APPConfig.Kafka.Address, ","),
// Topic: topic,
// GroupID: groupId,
// Partition: 0,
// MinBytes: 10e3, // 10KB
// MaxBytes: 10e6, // 10MB
// }))
return
readers
}
func
CreateCustomerGroup
(
topic
string
,
groupId
string
)
{
...
...
@@ -37,4 +45,3 @@ func CreateCustomerGroup(topic string, groupId string) {
log
.
Println
(
"create CustomerGroup error:"
,
err
)
}
}
transfer/kernel.go
View file @
42d45662
...
...
@@ -88,7 +88,7 @@ func Run(confPath string) {
closeWg
.
Wait
()
}
entity
.
CloseMessageChan
()
cancel
()
log
.
Printf
(
" Success unstall %d Transfer"
,
len
(
currentTopics
))
os
.
Exit
(
0
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment