package main
import (
"context"
"github.com/segmentio/kafka-go"
"gotribe-admin/pkg/util"
"log"
)
func main() {
type DoOrderEvent struct {
ShopID int `json:"shop_id"`
Uid int `json:"uid"`
}
data := DoOrderEvent{
ShopID: 10086,
Uid: 44,
}
w := &kafka.Writer{
Addr: kafka.TCP("localhost:9092"),
Topic: "order_event",
MaxAttempts: 3,
Async: true, //异步发送消息 提高效率
BatchSize: 10, //批量发送消息
//WriteTimeout: 3,
RequiredAcks: kafka.RequireAll, // 表示所有副本节点都确认消息后才返回
// RequireNone (0) fire-and-forget, do not wait for acknowledgements from the
// RequireOne (1) wait for the leader to acknowledge the writes
// RequireAll (-1) wait for the full ISR to acknowledge the writes
//Completion: ,表示消息发送完成后的回调函数
AllowAutoTopicCreation: true, //否允许自动创建主题
}
err := w.WriteMessages(context.Background(), kafka.Message{
Key: []byte(util.UUID()),
Value: []byte(util.Struct2Json(data)),
WriterData: data,
})
if err != nil {
log.Fatal("failed to write messages:", err)
}
// 关闭生产者
if err := w.Close(); err != nil {
log.Fatal("failed to close writer:", err)
}
//启用消费者
go func() {
kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{})
r := kafka.NewReader(kafka.ReaderConfig{
Brokers: []string{"localhost:9092"},
Topic: "order_event",
GroupID: "consumer_order_sec",
MinBytes: 10e3, // 10KB
MaxBytes: 10e6, // 10MB
})
defer r.Close()
for {
//m, err := r.ReadMessage(c) //阻塞获取消息 如果是消费组消费 自动提交偏移量
m, err := r.FetchMessage(c) //获取消息 如果是消费组消费 不自动提交偏移量 需要手动提交 Consumer Lag
if err != nil {
log.Fatal("failed to read message:", err)
}
log.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))
if err := r.CommitMessages(c, m); err != nil {
log.Fatal("bad commit message: ", err)
}
}
}()
select {}
}
golang kafka 使用方法,秒杀案例(segmentio/kafka-go)
go学习笔记2
var policies []model.RoleAct
policies6 := &[]model.RoleAct{}
fmt.Printf("初始化 policies %+v", policies) //分配了零值+内存地址
fmt.Printf("初始化 policies %+v", policies6) //分配了零值+内存地址
var policies2 *[]model.RoleAct //nil没有被初始化 没有分配内存地址
fmt.Printf("没有初始化 policies2 %+v\n", policies2)
policies2 = &[]model.RoleAct{}
fmt.Printf("初始化 policies2 %+v\n", *policies2) //解引用看到 分配了零值+内存地址
//为什么要强调这个,因为有些方法 是必须给内存地址的,比如
//grom 中find方法必须给内存地址的
list:=&[]model.RoleAct{}
DB.Find(list).Error
go学习笔记1
1.打印多维数组
var data= [3][2]string{}
fmt.Printf("%#v\n", data))
[3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}
2.字母转int strconv.Atoi() alphabetic to int
int转字母 strconv.Itoa() int to alphabetic
3.time 常用方法
time.Now().Unix() 获取时间戳
time.Now().AddDate(years int, months int, days int) 增加时间
time.Now().Format("2006-01-02 15:04:05") //格式化时间
4.以下返回值是一样的
func getX2X3(a int) (int, int) {
return a * 2, a * 3
}
func getX2X32(a int) (a2 int, a3 int) {
a2 = a * 2
a3 = a * 3
return
}
mysql创建用户并授权
CREATE USER '用户名'@'localhost' IDENTIFIED BY '密码';#如果是本地用户可用localhost,如果想让该用户可以从任意 远程主机登陆,可以使用通配符%
GRANT all privileges ON 数据库.* TO '用户名'@'localhost'; #授权
FLUSH PRIVILEGES;#刷新权限
台式机配置推荐
1.办公主机
Intel(R) Core(TM) i5-10500 CPU @ 3.10GHz
CPU主频 3.1GHz
最高睿频 4.5GHz
核心数量 六核心
线程数量 十二线程
热设计功耗(TDP) 65W
插槽类型 LGA 1200