Skip to content

Commit e399a48

Browse files
committed
modify redis single client
1 parent 87c3eb2 commit e399a48

File tree

7 files changed

+113
-72
lines changed

7 files changed

+113
-72
lines changed

ctxs/component/component.mq.go

+30-18
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ type IComponentMQ interface {
1515
}
1616

1717
type ComponentMQ struct {
18-
client *redis.Client
1918
}
2019

2120
var (
2221
componentMQ *ComponentMQ
2322
lockMQ sync.Mutex
23+
client *redis.Client
24+
l sync.Mutex
2425
)
2526

2627
func NewComponentMQ() *ComponentMQ {
@@ -37,38 +38,49 @@ func NewComponentMQ() *ComponentMQ {
3738
return componentMQ
3839
}
3940

40-
func (m *ComponentMQ) getClient() {
41-
if m.client != nil {
42-
return
41+
func (m *ComponentMQ) Produce(queueName, value string) error {
42+
if client == nil {
43+
GetSingleClient()
44+
}
45+
46+
cmd := client.LPush(context.TODO(), queueName, value)
47+
_, err := cmd.Result()
48+
if err != nil {
49+
return fmt.Errorf("lpush %s %s fail:err:%+v", queueName, value, err)
50+
}
51+
return nil
52+
}
53+
54+
func GetSingleClient() *redis.Client {
55+
if client != nil {
56+
return client
4357
}
4458

45-
client := redis.NewClient(&redis.Options{
59+
l.Lock()
60+
defer l.Unlock()
61+
62+
if client != nil {
63+
return client
64+
}
65+
cli := redis.NewClient(&redis.Options{
4666
Addr: configs.MQServer.Addr,
4767
Password: configs.MQServer.Password,
4868
DB: configs.MQServer.DB,
4969
})
5070
// secret auth
5171
if configs.MQServer.Auth != "" {
52-
err := client.Do(context.TODO(), "AUTH", configs.MQServer.Auth).Err()
72+
err := cli.Do(context.TODO(), "AUTH", configs.MQServer.Auth).Err()
5373
if err != nil {
5474
panic("config component mq do auth failed:" + err.Error())
5575
}
5676
}
57-
_, err := client.Ping(context.TODO()).Result()
77+
_, err := cli.Ping(context.TODO()).Result()
5878
if err != nil {
5979
panic("config mqserver reture err:" + err.Error())
6080
}
61-
klog.Infof("connect redis succ.")
62-
m.client = client
63-
}
6481

65-
func (m *ComponentMQ) Produce(queueName, value string) error {
66-
m.getClient()
82+
klog.Infof("connect redis {%s} succ.", configs.MQServer.Addr)
6783

68-
cmd := m.client.LPush(context.TODO(), queueName, value)
69-
_, err := cmd.Result()
70-
if err != nil {
71-
return fmt.Errorf("lpush %s %s fail:err:%+v", queueName, value, err)
72-
}
73-
return nil
84+
client = cli
85+
return client
7486
}

example/config.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ cronserver:
5151

5252
# mq server config
5353
mqserver:
54-
addr: "10.13.3.2:6379"
54+
addr: "localhost:6379"
5555
password: ""
5656
db: 0
57-

example/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ func (a *App) register() {
2222
a.API("/api/demo", api.Api)
2323

2424
a.Cron("auto.demo", cron.NewCron)
25+
26+
a.MQ("mq.test", cron.Receive)
2527
}

example/services/cron/auto.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package cron
22

3-
import "github.com/champly/hercules/ctxs"
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/champly/hercules/ctxs"
8+
)
49

510
type Cron struct{}
611

@@ -16,5 +21,15 @@ func (c *Cron) Handler(ctx *ctxs.Context) (err error) {
1621
ctx.Log.Debug("debug")
1722
ctx.Log.Warn("warn")
1823

24+
ctx.ToolBox.Produce("mq.test", fmt.Sprintf(`{"time":"%s"}`, time.Now().Format("2006-01-02 15:04:05")))
25+
26+
return nil
27+
}
28+
29+
func Receive(ctx *ctxs.Context) (err error) {
30+
31+
ctx.Log.Info("==========Receive Mq Info=========")
32+
ctx.Log.Info(ctx.GetBody())
33+
1934
return nil
2035
}

go.mod

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ go 1.15
55
require (
66
github.com/champly/lib4go v0.0.0-20201011013828-08c00d776af2
77
github.com/gin-gonic/gin v1.6.3
8-
github.com/go-redis/redis/v8 v8.3.1
9-
github.com/google/gops v0.3.8
8+
github.com/go-redis/redis/v8 v8.3.3-0.20201022064020-38caa12762e7
9+
github.com/google/gops v0.3.13-0.20200910081257-5d514cabbb21
10+
github.com/magiconair/properties v1.8.4 // indirect
11+
github.com/pelletier/go-toml v1.8.1 // indirect
1012
github.com/robfig/cron v1.2.0
11-
github.com/sirupsen/logrus v1.6.0
12-
github.com/spf13/viper v1.7.0
13+
github.com/sirupsen/logrus v1.7.1-0.20200930112625-d131c24e23ba
14+
github.com/spf13/afero v1.4.1 // indirect
15+
github.com/spf13/cast v1.3.1 // indirect
16+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
17+
github.com/spf13/pflag v1.0.5 // indirect
18+
github.com/spf13/viper v1.7.2-0.20201012074520-4938331709c1
19+
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
1320
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
1421
gopkg.in/go-playground/validator.v9 v9.31.0
22+
gopkg.in/ini.v1 v1.62.0 // indirect
1523
k8s.io/klog/v2 v2.3.0
1624
)

0 commit comments

Comments
 (0)