Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-userCenter
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-userCenter
Compare Revisions
master...feature/consul-redis
Source
feature/consul-redis
Select Git revision
...
Target
master
Select Git revision
Compare
Commits (2)
feat:注册到redis
· b7ad676a
iamhujiebin
authored
Oct 22, 2023
b7ad676a
本地不注册
· a1a9b2c5
hujiebin
authored
Oct 23, 2023
a1a9b2c5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
81 deletions
+47
-81
config.go
common/config/config.go
+4
-0
main.go
main.go
+43
-81
No files found.
common/config/config.go
View file @
a1a9b2c5
...
@@ -17,6 +17,10 @@ func AppIsRelease() bool {
...
@@ -17,6 +17,10 @@ func AppIsRelease() bool {
return
GetMode
()
==
RELEASE
return
GetMode
()
==
RELEASE
}
}
func
AppIsLocal
()
bool
{
return
GetMode
()
==
LOCAL
}
//数据库的配置
//数据库的配置
type
MysqlConfig
struct
{
type
MysqlConfig
struct
{
MYSQL_HOST
string
MYSQL_HOST
string
...
...
main.go
View file @
a1a9b2c5
...
@@ -6,14 +6,11 @@ import (
...
@@ -6,14 +6,11 @@ import (
"fmt"
"fmt"
"gorm.io/gorm/schema"
"gorm.io/gorm/schema"
"net"
"net"
"net/http"
"net/url"
"net/url"
"os"
"strconv"
"strconv"
"time"
"time"
"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"
"gorm.io/driver/mysql"
"gorm.io/driver/mysql"
...
@@ -502,84 +499,50 @@ func sendKickMessage(c userCenter.RouterClient, msg *userCenter.KickMessage) err
...
@@ -502,84 +499,50 @@ func sendKickMessage(c userCenter.RouterClient, msg *userCenter.KickMessage) err
return
err
return
err
}
}
func
consulCheck
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintln
(
w
,
"consulCheck"
)
}
const
(
const
(
RegisterName
=
"userCenter"
RegisterName
=
"userCenter"
RegisterTag
=
"用户中心"
)
)
func
registerToConsul
(
client
*
consulapi
.
Client
,
retry
bool
)
{
func
RegisterToRedis
(
RedisClusterClient
*
redis
.
Client
,
port
int
,
init
bool
)
{
checkPort
:=
port
+
1000
// 本地不注册
registration
:=
new
(
consulapi
.
AgentServiceRegistration
)
if
appConfig
.
AppIsLocal
()
{
hostName
,
_
:=
os
.
Hostname
()
return
registration
.
ID
=
fmt
.
Sprintf
(
"%s-%s"
,
RegisterName
,
hostName
)
registration
.
Name
=
RegisterName
registration
.
Port
=
port
registration
.
Tags
=
[]
string
{
RegisterTag
}
myIp
,
myNodeName
:=
""
,
""
if
localIp
,
err
:=
common
.
GetClientIpV2
();
err
!=
nil
{
mylogrus
.
MyLog
.
Fatalln
(
"local ip not found"
,
err
)
}
else
{
myIp
=
localIp
}
}
mylogrus
.
MyLog
.
Infof
(
"My ip is %s, nodeName: %s
\n
"
,
myIp
,
myNodeName
)
if
RedisClusterClient
==
nil
{
failMsg
:=
fmt
.
Sprintf
(
"RegisterToRedis fail,redisClusterNotInit,serviceName:%v"
,
RegisterName
)
registration
.
Address
=
myIp
_
=
dingding
.
SendDingRobot
(
dingding
.
ROBOTWEBHOOK
,
failMsg
,
true
)
registration
.
Check
=
&
consulapi
.
AgentServiceCheck
{
mylogrus
.
MyLog
.
Errorf
(
failMsg
)
HTTP
:
fmt
.
Sprintf
(
"http://localhost:%d%s"
,
checkPort
,
"/check"
),
return
Timeout
:
"3s"
,
Interval
:
"5s"
,
DeregisterCriticalServiceAfter
:
"30s"
,
//check失败后30秒删除本服务
}
}
err
:=
client
.
Agent
()
.
ServiceRegister
(
registration
)
redisKey
:=
"service:"
+
RegisterName
ip
,
err
:=
common
.
GetClientIpV2
()
if
err
!=
nil
{
if
err
!=
nil
{
mylogrus
.
MyLog
.
Fatal
(
"register server error : "
,
err
)
failMsg
:=
fmt
.
Sprintf
(
"RegisterToRedis fail,ip fail,err:%v,serviceName:%v"
,
err
,
RegisterName
)
}
mylogrus
.
MyLog
.
Errorf
(
failMsg
)
if
!
retry
{
_
=
dingding
.
SendDingRobot
(
dingding
.
ROBOTWEBHOOK
,
failMsg
,
true
)
http
.
HandleFunc
(
"/check"
,
consulCheck
)
return
http
.
ListenAndServe
(
fmt
.
Sprintf
(
":%d"
,
checkPort
),
nil
)
}
}
ipPort
:=
fmt
.
Sprintf
(
"%s:%d"
,
ip
,
port
)
}
if
err
:=
RedisClusterClient
.
ZAdd
(
context
.
Background
(),
redisKey
,
&
redis
.
Z
{
Score
:
float64
(
time
.
Now
()
.
Unix
()),
// 自愈检查
Member
:
ipPort
,
// 启动后每一分钟检查一次
})
.
Err
();
err
!=
nil
{
// 首次启动不执行
failMsg
:=
fmt
.
Sprintf
(
"RegisterToRedis fail,redis fail,err:%v,serviceName:%v"
,
err
,
RegisterName
)
func
selfCheck
()
{
mylogrus
.
MyLog
.
Errorf
(
failMsg
)
ticker
:=
time
.
NewTicker
(
time
.
Minute
)
_
=
dingding
.
SendDingRobot
(
dingding
.
ROBOTWEBHOOK
,
failMsg
,
true
)
}
// 初始化注册自我检查 selfCheck
if
init
{
go
func
()
{
ticker
:=
time
.
NewTicker
(
time
.
Second
*
5
)
defer
ticker
.
Stop
()
defer
ticker
.
Stop
()
for
{
for
{
select
{
select
{
case
<-
ticker
.
C
:
case
<-
ticker
.
C
:
client
,
err
:=
consulapi
.
NewClient
(
consulapi
.
DefaultConfig
())
//非默认情况下需要设置实际的参数
RegisterToRedis
(
RedisClusterClient
,
port
,
false
)
// 刷新注册
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"RegisterToConsul Fail:%v"
,
err
)
break
}
if
client
==
nil
{
mylogrus
.
MyLog
.
Errorf
(
"Fail to get consul client."
)
break
}
cataLog
:=
client
.
Catalog
()
if
cataLog
==
nil
{
mylogrus
.
MyLog
.
Errorf
(
"No catalog."
)
break
}
services
,
_
,
err
:=
cataLog
.
Service
(
RegisterName
,
""
,
nil
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"%v"
,
err
)
break
}
if
len
(
services
)
==
0
{
mylogrus
.
MyLog
.
Errorf
(
"%s not found."
,
RegisterName
)
go
registerToConsul
(
client
,
true
)
// 重新注册
}
else
{
mylogrus
.
MyLog
.
Infof
(
"%s check success %v"
,
RegisterName
,
services
[
0
])
}
}
}
}
}()
}
}
}
}
...
@@ -591,14 +554,13 @@ type HiloConfigs struct {
...
@@ -591,14 +554,13 @@ type HiloConfigs struct {
func
main
()
{
func
main
()
{
flag
.
Parse
()
flag
.
Parse
()
config
:=
consulapi
.
DefaultConfig
()
// init redis cluster
rdbCluster
:=
redis
.
NewClient
(
&
redis
.
Options
{
client
,
err
:=
consulapi
.
NewClient
(
config
)
Addr
:
appConfig
.
GetConfigRedis
()
.
REDIS_CLUSTER_HOST
,
if
err
!=
nil
{
Password
:
appConfig
.
GetConfigRedis
()
.
REDIS_CLUSTER_PASSWORD
,
mylogrus
.
MyLog
.
Fatal
(
"consul client error : "
,
err
)
})
}
// 注册到redis
go
registerToConsul
(
client
,
false
)
RegisterToRedis
(
rdbCluster
,
port
,
true
)
go
selfCheck
()
// init redis
// init redis
rdb
:=
redis
.
NewClient
(
&
redis
.
Options
{
rdb
:=
redis
.
NewClient
(
&
redis
.
Options
{
...
...