Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-micCenter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
hujiebin
hilo-micCenter
Commits
a37090f8
Commit
a37090f8
authored
Sep 02, 2023
by
JiebinHu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:redis CLuster
parent
00d7db96
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
23 additions
and
96 deletions
+23
-96
config.go
common/config/config.go
+4
-2
redis.go
common/redisCli/redis.go
+10
-35
util.go
common/redisCli/util.go
+0
-56
debug.ini
debug.ini
+2
-0
local.ini
local.ini
+2
-0
main.go
main.go
+3
-3
release.ini
release.ini
+2
-0
No files found.
common/config/config.go
View file @
a37090f8
...
@@ -26,8 +26,10 @@ type MysqlCodeConfig struct {
...
@@ -26,8 +26,10 @@ type MysqlCodeConfig struct {
//redis配置
//redis配置
type
RedisConfig
struct
{
type
RedisConfig
struct
{
REDIS_HOST
string
REDIS_HOST
string
REDIS_PASSWORD
string
REDIS_PASSWORD
string
REDIS_CLUSTER_HOST
string
REDIS_CLUSTER_PASSWORD
string
}
}
//jwt
//jwt
...
...
common/redisCli/redis.go
View file @
a37090f8
...
@@ -8,52 +8,27 @@ import (
...
@@ -8,52 +8,27 @@ import (
"log"
"log"
)
)
var
RedisClient
*
redis
.
Client
var
RedisCluster
*
redis
.
Client
var
RedisClient1
*
redis
.
Client
func
init
()
{
func
init
()
{
RedisClient
=
redis
.
NewClient
(
&
redis
.
Options
{
RedisCluster
=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
config
.
GetConfigRedis
()
.
REDIS_HOST
,
Addr
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_HOST
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
,
// no password set
Password
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_PASSWORD
,
// no password set
DB
:
0
,
// use default DB
DB
:
0
,
// use default DB
PoolSize
:
2000
,
MinIdleConns
:
200
,
})
mylogrus
.
MyLog
.
Infoln
(
config
.
GetConfigRedis
()
.
REDIS_HOST
)
mylogrus
.
MyLog
.
Infoln
(
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
)
pong
,
err
:=
RedisClient
.
Ping
(
context
.
Background
())
.
Result
()
if
err
!=
nil
{
mylogrus
.
MyLog
.
Warn
(
err
)
mylogrus
.
MyLog
.
Fatal
(
"redis db0 connect fail"
)
}
else
{
mylogrus
.
MyLog
.
Info
(
"redis db0 connection success - "
,
pong
)
}
RedisClient1
=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
config
.
GetConfigRedis
()
.
REDIS_HOST
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
,
// no password set
DB
:
1
,
// use default DB
PoolSize
:
200
,
PoolSize
:
200
,
MinIdleConns
:
20
,
MinIdleConns
:
20
,
})
})
mylogrus
.
MyLog
.
Infoln
(
config
.
GetConfigRedis
()
.
REDIS_HOST
)
pong
,
err
:=
RedisCluster
.
Ping
(
context
.
Background
())
.
Result
()
mylogrus
.
MyLog
.
Infoln
(
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
)
pong
,
err
=
RedisClient1
.
Ping
(
context
.
Background
())
.
Result
()
if
err
!=
nil
{
if
err
!=
nil
{
mylogrus
.
MyLog
.
Warn
(
err
)
mylogrus
.
MyLog
.
Warn
(
err
)
mylogrus
.
MyLog
.
Fatal
(
"redis db
1
connect fail"
)
mylogrus
.
MyLog
.
Fatal
(
"redis db
0
connect fail"
)
}
else
{
}
else
{
log
.
Println
(
"redis db
1
connection success - "
+
pong
)
log
.
Println
(
"redis db
0
connection success - "
+
pong
)
}
}
// log hook
// log hook
//RedisClient.AddHook(redisHook{})
//RedisClient.AddHook(redisHook{})
}
}
func
GetRedis
()
*
redis
.
Client
{
func
GetRedisCluster
()
*
redis
.
Client
{
return
RedisClient
return
RedisCluster
}
func
GetRedis1
()
*
redis
.
Client
{
return
RedisClient1
}
}
common/redisCli/util.go
deleted
100644 → 0
View file @
00d7db96
package
redisCli
import
(
"context"
"hilo-micCenter/common/mylogrus"
"strconv"
"time"
)
//这个用户避免多个服务器并发问题。
func
SetNX
(
key
string
,
value
interface
{},
expiration
time
.
Duration
,
callBack
func
())
{
flag
,
err
:=
RedisClient
.
SetNX
(
context
.
Background
(),
key
,
value
,
expiration
)
.
Result
()
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"key:%v lock start setNx err: %v"
,
key
,
err
)
}
if
!
flag
{
mylogrus
.
MyLog
.
Infof
(
"key:%v lock setNx has lock"
,
key
)
return
}
mylogrus
.
MyLog
.
Infof
(
"key:%v lock setNx begin"
,
key
)
callBack
()
//执行结束之后,移除key
//RedisClient.Del(context.Background(), key)
mylogrus
.
MyLog
.
Infof
(
"key:%v lock setNx end"
,
key
)
}
//setNx没有,结束后,没有移除
/*func SetNxNoDel(key string, value interface{}, expiration time.Duration, callBack func()) {
flag, err := RedisClient.SetNX(context.Background(), key, value, expiration).Result()
if err != nil {
mylogrus.MyLog.Errorf("key:%v lock start setNx err: %v", key, err)
}
if !flag {
mylogrus.MyLog.Infof("key:%v lock setNx has lock", key)
return
}
mylogrus.MyLog.Infof("key:%v lock setNx begin", key)
callBack()
mylogrus.MyLog.Infof("key:%v lock setNx end", key)
}*/
func
ClearExpired
(
key
string
,
expireSec
int64
)
error
{
return
GetRedis
()
.
ZRemRangeByScore
(
context
.
Background
(),
key
,
"0"
,
strconv
.
FormatInt
(
time
.
Now
()
.
Unix
()
-
expireSec
,
10
))
.
Err
()
}
func
Lock
(
key
string
,
expiration
time
.
Duration
)
bool
{
flag
,
err
:=
RedisClient
.
SetNX
(
context
.
Background
(),
key
,
1
,
expiration
)
.
Result
()
if
err
!=
nil
{
return
false
}
if
!
flag
{
return
false
}
return
true
}
debug.ini
View file @
a37090f8
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
[REDIS]
REDIS_HOST
=
47.244.34.27:6379
REDIS_HOST
=
47.244.34.27:6379
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST
=
47.244.34.27:6379
REDIS_CLUSTER_PASSWORD
=
8QZ9JD1zLvPR3yHf
[JWT]
[JWT]
SECRET
=
hilo1632
SECRET
=
hilo1632
ISSUER_API
=
hiloApi
ISSUER_API
=
hiloApi
...
...
local.ini
View file @
a37090f8
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
[REDIS]
REDIS_HOST
=
47.244.34.27:6379
REDIS_HOST
=
47.244.34.27:6379
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST
=
47.244.34.27:6379
REDIS_CLUSTER_PASSWORD
=
8QZ9JD1zLvPR3yHf
[JWT]
[JWT]
SECRET
=
hilo1632
SECRET
=
hilo1632
ISSUER_API
=
hiloApi
ISSUER_API
=
hiloApi
...
...
main.go
View file @
a37090f8
...
@@ -44,7 +44,7 @@ func check() {
...
@@ -44,7 +44,7 @@ func check() {
for
{
for
{
select
{
select
{
case
<-
tick
.
C
:
case
<-
tick
.
C
:
l
,
err
:=
redisCli
.
GetRedis
()
.
LLen
(
context
.
Background
(),
micInfoChange
)
.
Result
()
l
,
err
:=
redisCli
.
GetRedis
Cluster
()
.
LLen
(
context
.
Background
(),
micInfoChange
)
.
Result
()
if
err
!=
nil
{
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys msg error,left %v-%v"
,
l
,
err
)
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys msg error,left %v-%v"
,
l
,
err
)
}
}
...
@@ -65,7 +65,7 @@ func check() {
...
@@ -65,7 +65,7 @@ func check() {
func
deal
()
{
func
deal
()
{
for
true
{
for
true
{
//不需要加锁,注意,阻塞。
//不需要加锁,注意,阻塞。
strs
,
err
:=
redisCli
.
GetRedis
()
.
BLPop
(
context
.
Background
(),
time
.
Second
,
micInfoChange
)
.
Result
()
strs
,
err
:=
redisCli
.
GetRedis
Cluster
()
.
BLPop
(
context
.
Background
(),
time
.
Second
,
micInfoChange
)
.
Result
()
if
err
!=
nil
{
if
err
!=
nil
{
if
err
!=
redis
.
Nil
{
if
err
!=
redis
.
Nil
{
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys redisCli.GetRedis().BLPop err:+%v"
,
err
)
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys redisCli.GetRedis().BLPop err:+%v"
,
err
)
...
@@ -127,7 +127,7 @@ func SendSignalMsg(groupId string, msg GroupSystemMsg) {
...
@@ -127,7 +127,7 @@ func SendSignalMsg(groupId string, msg GroupSystemMsg) {
if
err
=
tencentyun
.
SendSystemMsg
(
logger
,
groupId
,
[]
string
{},
str
);
err
!=
nil
{
if
err
=
tencentyun
.
SendSystemMsg
(
logger
,
groupId
,
[]
string
{},
str
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys SendSignalMsg sync failed for %s, msgId = %d, content:%v, err:%+v"
,
groupId
,
msg
.
MsgId
,
str
,
err
)
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys SendSignalMsg sync failed for %s, msgId = %d, content:%v, err:%+v"
,
groupId
,
msg
.
MsgId
,
str
,
err
)
content
:=
fmt
.
Sprintf
(
"腾讯云推送失败,
content:%v,err:%v"
,
str
,
err
.
Error
())
content
:=
fmt
.
Sprintf
(
"腾讯云推送失败,
group:%v,msgId:%v,err:%v"
,
groupId
,
msg
.
MsgId
,
err
.
Error
())
if
err
:=
dingding
.
SendDingRobot
(
dingding
.
ROBOTWEBHOOK
,
content
,
true
);
err
!=
nil
{
if
err
:=
dingding
.
SendDingRobot
(
dingding
.
ROBOTWEBHOOK
,
content
,
true
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys SendSignalMsg send dingding fail%s"
,
err
.
Error
())
mylogrus
.
MyLog
.
Errorf
(
"cron micChangeSys SendSignalMsg send dingding fail%s"
,
err
.
Error
())
}
}
...
...
release.ini
View file @
a37090f8
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
[REDIS]
REDIS_HOST
=
r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379
REDIS_HOST
=
r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379
REDIS_PASSWORD
=
REDIS_PASSWORD
=
REDIS_CLUSTER_HOST
=
r-eb3yt6k8zgxs62kjjs.redis.dubai.rds.aliyuncs.com:6379
REDIS_CLUSTER_PASSWORD
=
[JWT]
[JWT]
SECRET
=
hilo1504
SECRET
=
hilo1504
ISSUER_API
=
hiloApi
ISSUER_API
=
hiloApi
...
...
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