Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
im-microservice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Li Feifei
im-microservice
Commits
1db452e8
Commit
1db452e8
authored
Jul 02, 2020
by
Li Feifei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IM健康检测
parent
91228c9a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
34 deletions
+95
-34
main.go
main.go
+74
-34
health.pb.go
pb/health.pb.go
+0
-0
health.proto
u-proto/health.proto
+21
-0
No files found.
main.go
View file @
1db452e8
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/hex"
"errors"
"errors"
"fmt"
"fmt"
"sync"
"log"
"log"
"net"
"net"
...
@@ -23,11 +24,45 @@ import (
...
@@ -23,11 +24,45 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
)
type
Health
interface
{
// Check returns if server is healthy or not
Check
(
c
context
.
Context
)
(
bool
,
error
)
}
// 健康检测
type
Server
struct
{
mu
sync
.
Mutex
statusMap
map
[
string
]
pb
.
HealthCheckResponse_ServingStatus
}
func
NewServer
()
*
Server
{
return
&
Server
{
statusMap
:
make
(
map
[
string
]
pb
.
HealthCheckResponse_ServingStatus
),
}
}
func
(
s
*
Server
)
Check
(
ctx
context
.
Context
,
in
*
pb
.
HealthCheckRequest
)
(
*
pb
.
HealthCheckResponse
,
error
)
{
s
.
mu
.
Lock
()
defer
s
.
mu
.
Unlock
()
if
in
.
Service
==
""
{
return
&
pb
.
HealthCheckResponse
{
Status
:
pb
.
HealthCheckResponse_SERVING
,},
nil
}
if
status
,
ok
:=
s
.
statusMap
[
in
.
Service
];
ok
{
return
&
pb
.
HealthCheckResponse
{
Status
:
status
,},
nil
}
return
nil
,
status
.
Error
(
codes
.
NotFound
,
"unkonw service"
)
}
const
(
const
(
port
=
":50051"
port
=
":50051"
health_path
=
"/pb.Health/Check"
)
)
func
checksum
(
req
map
[
string
]
string
)
error
{
func
checksum
(
req
map
[
string
]
string
)
error
{
...
@@ -60,40 +95,40 @@ func auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, hand
...
@@ -60,40 +95,40 @@ func auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, hand
if
!
ok
{
if
!
ok
{
return
nil
,
grpc
.
Errorf
(
codes
.
Unauthenticated
,
"无Token认证信息"
)
return
nil
,
grpc
.
Errorf
(
codes
.
Unauthenticated
,
"无Token认证信息"
)
}
}
if
info
.
FullMethod
!=
health_path
{
request_map
:=
make
(
map
[
string
]
string
)
request_map
:=
make
(
map
[
string
]
string
)
s
:=
fmt
.
Sprintf
(
"%v"
,
req
)
s
:=
fmt
.
Sprintf
(
"%v"
,
req
)
key_r
,
_
:=
regexp
.
Compile
(
`Appkey:"(.*?)"`
)
key_r
,
_
:=
regexp
.
Compile
(
`Appkey:"(.*?)"`
)
nonce_r
,
_
:=
regexp
.
Compile
(
`Nonce:"(.*?)"`
)
nonce_r
,
_
:=
regexp
.
Compile
(
`Nonce:"(.*?)"`
)
c_r
,
_
:=
regexp
.
Compile
(
`Curtime:"(.*?)"`
)
c_r
,
_
:=
regexp
.
Compile
(
`Curtime:"(.*?)"`
)
cs_r
,
_
:=
regexp
.
Compile
(
`Checksum:"(.*?)"`
)
cs_r
,
_
:=
regexp
.
Compile
(
`Checksum:"(.*?)"`
)
appkey
:=
key_r
.
FindAllStringSubmatch
(
s
,
-
1
)
appkey
:=
key_r
.
FindAllStringSubmatch
(
s
,
-
1
)
nonce
:=
nonce_r
.
FindAllStringSubmatch
(
s
,
-
1
)
nonce
:=
nonce_r
.
FindAllStringSubmatch
(
s
,
-
1
)
curtime
:=
c_r
.
FindAllStringSubmatch
(
s
,
-
1
)
curtime
:=
c_r
.
FindAllStringSubmatch
(
s
,
-
1
)
check_sum
:=
cs_r
.
FindAllStringSubmatch
(
s
,
-
1
)
check_sum
:=
cs_r
.
FindAllStringSubmatch
(
s
,
-
1
)
if
len
(
appkey
)
<
1
{
if
len
(
appkey
)
<
1
{
return
nil
,
errors
.
New
(
"缺少appkey"
)
return
nil
,
errors
.
New
(
"缺少appkey"
)
}
}
if
len
(
nonce
)
<
1
{
if
len
(
nonce
)
<
1
{
return
nil
,
errors
.
New
(
"缺少nonce"
)
return
nil
,
errors
.
New
(
"缺少nonce"
)
}
}
if
len
(
curtime
)
<
1
{
if
len
(
curtime
)
<
1
{
return
nil
,
errors
.
New
(
"缺少curtime"
)
return
nil
,
errors
.
New
(
"缺少curtime"
)
}
}
if
len
(
check_sum
)
<
1
{
if
len
(
check_sum
)
<
1
{
return
nil
,
errors
.
New
(
"缺少checksum"
)
return
nil
,
errors
.
New
(
"缺少checksum"
)
}
}
request_map
[
"Appkey"
]
=
appkey
[
0
][
1
]
request_map
[
"Appkey"
]
=
appkey
[
0
][
1
]
request_map
[
"Nonce"
]
=
nonce
[
0
][
1
]
request_map
[
"Nonce"
]
=
nonc
e
[
0
][
1
]
request_map
[
"Curtime"
]
=
curtim
e
[
0
][
1
]
request_map
[
"Curtime"
]
=
curtime
[
0
][
1
]
request_map
[
"Checksum"
]
=
check_sum
[
0
][
1
]
request_map
[
"Checksum"
]
=
check_sum
[
0
][
1
]
if
err
:=
checksum
(
request_map
);
err
!=
nil
{
if
err
:=
checksum
(
request_map
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
}
return
handler
(
ctx
,
req
)
return
handler
(
ctx
,
req
)
...
@@ -110,10 +145,15 @@ func main() {
...
@@ -110,10 +145,15 @@ func main() {
opts
=
append
(
opts
,
grpc
.
UnaryInterceptor
(
auth
))
opts
=
append
(
opts
,
grpc
.
UnaryInterceptor
(
auth
))
s
:=
grpc
.
NewServer
(
opts
...
)
s
:=
grpc
.
NewServer
(
opts
...
)
// s := grpc.NewServer()
// s := grpc.NewServer()
srv
:=
NewServer
()
pb
.
RegisterHealthServer
(
s
,
srv
)
pb
.
RegisterConfigureSeviceServer
(
s
,
&
ic
.
ConfigureSevice
{})
pb
.
RegisterConfigureSeviceServer
(
s
,
&
ic
.
ConfigureSevice
{})
pb
.
RegisterChatRoomServiceServer
(
s
,
&
im_chat_room
.
ImChatRoomService
{})
pb
.
RegisterChatRoomServiceServer
(
s
,
&
im_chat_room
.
ImChatRoomService
{})
pb
.
RegisterImUserServer
(
s
,
&
im_user
.
ImUserServer
{})
pb
.
RegisterImUserServer
(
s
,
&
im_user
.
ImUserServer
{})
pb
.
RegisterUserRelationshipServiceServer
(
s
,
&
iur
.
UserRelationshipService
{})
pb
.
RegisterUserRelationshipServiceServer
(
s
,
&
iur
.
UserRelationshipService
{})
reflection
.
Register
(
s
)
log
.
Println
(
"gRPC server is running on "
+
port
+
" port."
)
if
err
:=
s
.
Serve
(
lis
);
err
!=
nil
{
if
err
:=
s
.
Serve
(
lis
);
err
!=
nil
{
log
.
Fatalf
(
"failed to serve: %v"
,
err
)
log
.
Fatalf
(
"failed to serve: %v"
,
err
)
}
}
...
...
pb/health.pb.go
0 → 100644
View file @
1db452e8
This diff is collapsed.
Click to expand it.
u-proto/health.proto
0 → 100644
View file @
1db452e8
syntax
=
"proto3"
;
package
pb
;
message
HealthCheckRequest
{
string
service
=
1
;
}
message
HealthCheckResponse
{
enum
ServingStatus
{
UNKNOWN
=
0
;
SERVING
=
1
;
NOT_SERVING
=
2
;
}
ServingStatus
status
=
1
;
}
service
Health
{
rpc
Check
(
HealthCheckRequest
)
returns
(
HealthCheckResponse
);
}
\ No newline at end of file
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