Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-algoCenter
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-algoCenter
Commits
b35cfd91
Commit
b35cfd91
authored
Oct 23, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:consul redis
parent
7aca0b0f
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
41 deletions
+31
-41
config.go
common/config/config.go
+4
-2
debug.ini
debug.ini
+2
-0
local.ini
local.ini
+2
-0
main.go
main.go
+21
-39
release.ini
release.ini
+2
-0
No files found.
common/config/config.go
View file @
b35cfd91
...
...
@@ -28,6 +28,8 @@ type MysqlCodeConfig struct {
type
RedisConfig
struct
{
REDIS_HOST
string
REDIS_PASSWORD
string
REDIS_CLUSTER_HOST
string
REDIS_CLUSTER_PASSWORD
string
}
//jwt
...
...
debug.ini
View file @
b35cfd91
...
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
REDIS_HOST
=
47.244.34.27:6379
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST
=
47.244.34.27:6379
REDIS_CLUSTER_PASSWORD
=
8QZ9JD1zLvPR3yHf
[JWT]
SECRET
=
hilo1632
ISSUER_API
=
hiloApi
...
...
local.ini
View file @
b35cfd91
...
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
REDIS_HOST
=
47.244.34.27:6379
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST
=
47.244.34.27:6379
REDIS_CLUSTER_PASSWORD
=
8QZ9JD1zLvPR3yHf
[JWT]
SECRET
=
hilo1632
ISSUER_API
=
hiloApi
...
...
main.go
View file @
b35cfd91
...
...
@@ -4,13 +4,12 @@ import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
consulapi
"github.com/hashicorp/consul/api"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
"hilo-algoCenter/algo"
"hilo-algoCenter/common/con
sul
"
"hilo-algoCenter/common/con
fig
"
"hilo-algoCenter/common/mylogrus"
"hilo-algoCenter/protocol/userCenter"
"time"
...
...
@@ -29,36 +28,12 @@ var kacp = keepalive.ClientParameters{
}
func
main
()
{
client
,
err
:=
consulapi
.
NewClient
(
consulapi
.
DefaultConfig
())
//非默认情况下需要设置实际的参数
if
err
!=
nil
{
mylogrus
.
MyLog
.
Fatalln
(
err
)
}
if
client
==
nil
{
mylogrus
.
MyLog
.
Fatalln
(
"Fail to get consul client."
)
}
redisAddress
:=
default_redis_address
redisPassword
:=
default_redis_password
kv
:=
client
.
KV
()
if
kv
!=
nil
{
p
,
_
,
err
:=
kv
.
Get
(
"redis_address"
,
nil
)
if
err
==
nil
&&
p
!=
nil
{
redisAddress
=
string
(
p
.
Value
)
}
p
,
_
,
err
=
kv
.
Get
(
"redis_password"
,
nil
)
if
err
==
nil
&&
p
!=
nil
{
redisPassword
=
string
(
p
.
Value
)
}
}
// init redis
rdb
:=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
redisAddress
,
Password
:
redisPassword
,
Addr
:
config
.
GetConfigRedis
()
.
REDIS_HOST
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
,
DB
:
redis_section
,
})
if
rdb
==
nil
{
mylogrus
.
MyLog
.
Fatalf
(
"failed to connect redis %s
\n
"
,
redisAddress
)
}
result
,
err
:=
rdb
.
Ping
(
context
.
Background
())
.
Result
()
if
err
!=
nil
{
...
...
@@ -67,20 +42,27 @@ func main() {
mylogrus
.
MyLog
.
Fatalf
(
"Invalid ping response %s"
,
result
)
}
cataLog
:=
client
.
Catalog
()
if
cataLog
==
nil
{
mylogrus
.
MyLog
.
Fatalln
(
"No catalog."
)
}
addr
,
err
:=
consul
.
GetServices
(
cataLog
,
"userCenter"
)
// init redis cluster
rdbCluster
:=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_HOST
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_PASSWORD
,
})
redisKey
:=
fmt
.
Sprintf
(
"service:userCenter"
)
ipPorts
,
err
:=
rdbCluster
.
ZRangeByScore
(
context
.
Background
(),
redisKey
,
&
redis
.
ZRangeBy
{
Min
:
fmt
.
Sprintf
(
"%d"
,
time
.
Now
()
.
Add
(
-
time
.
Second
*
15
)
.
Unix
()),
// 3倍心跳
Max
:
"+inf"
,
})
.
Result
()
if
err
!=
nil
{
mylogrus
.
MyLog
.
Fatalln
(
err
)
failMsg
:=
fmt
.
Sprintf
(
"get service fail,svc:%v,err:%v"
,
"userCenter"
,
err
)
mylogrus
.
MyLog
.
Errorf
(
failMsg
)
}
else
if
len
(
ipPorts
)
>
0
{
}
if
len
(
addr
)
=
=
0
{
mylogrus
.
MyLog
.
Fatalln
(
"No userCenter available."
)
if
len
(
ipPorts
)
<
=
0
{
ipPorts
=
[]
string
{
"127.0.0.1:50040"
}
}
addresses
:=
make
([]
resolver
.
Address
,
len
(
addr
))
for
i
,
s
:=
range
addr
{
addresses
:=
make
([]
resolver
.
Address
,
len
(
ipPorts
))
for
i
,
s
:=
range
ipPorts
{
addresses
[
i
]
.
Addr
=
s
mylogrus
.
MyLog
.
Infof
(
"address : %s"
,
s
)
}
...
...
release.ini
View file @
b35cfd91
...
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
REDIS_HOST
=
r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379
REDIS_PASSWORD
=
REDIS_CLUSTER_HOST
=
r-eb3yt6k8zgxs62kjjs.redis.dubai.rds.aliyuncs.com:6379
REDIS_CLUSTER_PASSWORD
=
[JWT]
SECRET
=
hilo1504
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