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
0b544aec
Commit
0b544aec
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 @
0b544aec
...
@@ -28,6 +28,8 @@ type MysqlCodeConfig struct {
...
@@ -28,6 +28,8 @@ type MysqlCodeConfig struct {
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
...
...
debug.ini
View file @
0b544aec
...
@@ -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 @
0b544aec
...
@@ -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 @
0b544aec
...
@@ -4,13 +4,12 @@ import (
...
@@ -4,13 +4,12 @@ import (
"context"
"context"
"fmt"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/go-redis/redis/v8"
consulapi
"github.com/hashicorp/consul/api"
"google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
"google.golang.org/grpc/resolver/manual"
"hilo-algoCenter/algo"
"hilo-algoCenter/algo"
"hilo-algoCenter/common/con
sul
"
"hilo-algoCenter/common/con
fig
"
"hilo-algoCenter/common/mylogrus"
"hilo-algoCenter/common/mylogrus"
"hilo-algoCenter/protocol/userCenter"
"hilo-algoCenter/protocol/userCenter"
"time"
"time"
...
@@ -29,36 +28,12 @@ var kacp = keepalive.ClientParameters{
...
@@ -29,36 +28,12 @@ var kacp = keepalive.ClientParameters{
}
}
func
main
()
{
func
main
()
{
client
,
err
:=
consulapi
.
NewClient
(
consulapi
.
DefaultConfig
())
//非默认情况下需要设置实际的参数
// init redis
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
)
}
}
rdb
:=
redis
.
NewClient
(
&
redis
.
Options
{
rdb
:=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
redisAddress
,
Addr
:
config
.
GetConfigRedis
()
.
REDIS_HOST
,
Password
:
redisPassword
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
,
DB
:
redis_section
,
DB
:
redis_section
,
})
})
if
rdb
==
nil
{
mylogrus
.
MyLog
.
Fatalf
(
"failed to connect redis %s
\n
"
,
redisAddress
)
}
result
,
err
:=
rdb
.
Ping
(
context
.
Background
())
.
Result
()
result
,
err
:=
rdb
.
Ping
(
context
.
Background
())
.
Result
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -67,20 +42,27 @@ func main() {
...
@@ -67,20 +42,27 @@ func main() {
mylogrus
.
MyLog
.
Fatalf
(
"Invalid ping response %s"
,
result
)
mylogrus
.
MyLog
.
Fatalf
(
"Invalid ping response %s"
,
result
)
}
}
cataLog
:=
client
.
Catalog
()
// init redis cluster
if
cataLog
==
nil
{
rdbCluster
:=
redis
.
NewClient
(
&
redis
.
Options
{
mylogrus
.
MyLog
.
Fatalln
(
"No catalog."
)
Addr
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_HOST
,
}
Password
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_PASSWORD
,
addr
,
err
:=
consul
.
GetServices
(
cataLog
,
"userCenter"
)
})
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
{
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
{
if
len
(
ipPorts
)
<
=
0
{
mylogrus
.
MyLog
.
Fatalln
(
"No userCenter available."
)
ipPorts
=
[]
string
{
"127.0.0.1:50040"
}
}
}
addresses
:=
make
([]
resolver
.
Address
,
len
(
addr
))
addresses
:=
make
([]
resolver
.
Address
,
len
(
ipPorts
))
for
i
,
s
:=
range
addr
{
for
i
,
s
:=
range
ipPorts
{
addresses
[
i
]
.
Addr
=
s
addresses
[
i
]
.
Addr
=
s
mylogrus
.
MyLog
.
Infof
(
"address : %s"
,
s
)
mylogrus
.
MyLog
.
Infof
(
"address : %s"
,
s
)
}
}
...
...
release.ini
View file @
0b544aec
...
@@ -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