@ -59,10 +59,12 @@ func ProvideLow(topic string, datas []DataEX.KafkaData) (bool, string) {
var client * kafka . Conn
clients , flag := kafkaPool [ topic ]
//add by zhangjun 2020-08-17
var err error
if flag == false {
client , _ = kafka . DialLeader ( context . Background ( ) , "tcp" , ConfigUtil . KafkaBrokers [ 0 ] , topic , 0 )
client , err = CreateConnection ( topic , 0 )
clients = make ( map [ int ] * kafka . Conn )
clients [ 0 ] = client
@ -78,7 +80,7 @@ func ProvideLow(topic string, datas []DataEX.KafkaData) (bool, string) {
//CreateTopic(topic)
CreateTopic2 ( topic )
client , _ = kafka . DialLeader ( context . Background ( ) , "tcp" , ConfigUtil . KafkaBrokers [ 0 ] , topic , 0 )
client , _ = CreateConnection ( topic , 0 )
clients = make ( map [ int ] * kafka . Conn )
clients [ 0 ] = client
@ -104,14 +106,17 @@ func ProvideLow(topic string, datas []DataEX.KafkaData) (bool, string) {
//add by zhangjun 2020-08-02 判断链接超时,异常关闭
_ , err := client . ReadLastOffset ( )
if err != nil {
client , _ = kafka . DialLeader ( context . Background ( ) , "tcp" , ConfigUtil . KafkaBrokers [ 0 ] , topic , num )
//client, _ = kafka.DialLeader(context.Background(), "tcp", ConfigUtil.KafkaBrokers[0], topic, num)
client , _ = CreateConnection ( topic , num )
kafkaPool [ topic ] [ num ] = client
}
} else {
client , _ = kafka . DialLeader ( context . Background ( ) , "tcp" , ConfigUtil . KafkaBrokers [ 0 ] , topic , num )
//client, _ = kafka.DialLeader(context.Background(), "tcp", ConfigUtil.KafkaBrokers[0], topic, num)
client , _ = CreateConnection ( topic , num )
kafkaPool [ topic ] [ num ] = client
}
@ -132,7 +137,7 @@ func ProvideLow(topic string, datas []DataEX.KafkaData) (bool, string) {
client . SetWriteDeadline ( time . Now ( ) . Add ( 10 * time . Second ) )
_ , err : = client . WriteMessages ( messages ... )
_ , err = client . WriteMessages ( messages ... )
//client.Close()
@ -146,6 +151,31 @@ func ProvideLow(topic string, datas []DataEX.KafkaData) (bool, string) {
}
}
/ * *
* @ Author zhangjun
* @ Description 初 始 化 Kafka 连 接
* @ Date 2020 - 0 8 - 17 02 : 37
* @ Param
* @ return
* * /
func CreateConnection ( topic string , part int ) ( * kafka . Conn , error ) {
//change by zhangjun 2020-08-17 for kafka cluster fail-over
index := 0
client , err := kafka . DialLeader ( context . Background ( ) , "tcp" , ConfigUtil . KafkaBrokers [ index ] , topic , part )
if err != nil {
for no := 1 ; no < len ( ConfigUtil . KafkaBrokers ) ; no ++ {
client , err = kafka . DialLeader ( context . Background ( ) , "tcp" , ConfigUtil . KafkaBrokers [ index + 1 ] , topic , part )
if err == nil {
break
}
}
}
return client , err
}
/ * *
* @ Author zhangjun
* @ Description
@ -343,7 +373,7 @@ func InitTopic(topic string) {
time . Sleep ( 100 * time . Millisecond )
CreateTopic ( topic )
CreateTopic 2 ( topic )
}
}
@ -392,6 +422,19 @@ func CreateTopic2(topic string) {
if err != nil {
fmt . Println ( err . Error ( ) )
conn2 , _ := net . Dial ( "tcp" , ConfigUtil . KafkaBrokers [ 1 ] )
client2 := kafka . NewConn ( conn2 , "__consumer_offsets" , 0 )
err2 := client2 . CreateTopics ( kafka . TopicConfig {
NumPartitions : int ( ConfigUtil . KafkaParts ) ,
ReplicationFactor : int ( ConfigUtil . KafkaReply ) ,
Topic : topic ,
} )
if err2 != nil {
fmt . Println ( err2 . Error ( ) )
}
}
}