Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
service-registry
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
Odysseus
service-registry
Commits
66301fb8
Commit
66301fb8
authored
Feb 27, 2024
by
luxq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
ed600923
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
51 additions
and
179 deletions
+51
-179
apigateway.go
query/apigateway.go
+1
-28
backend.go
query/backend.go
+1
-28
baseservice.go
query/baseservice.go
+5
-13
interface.go
query/interface.go
+2
-6
nm.go
query/nm.go
+1
-31
schedule.go
query/schedule.go
+1
-29
worker.go
query/worker.go
+2
-30
registry.go
registry/registry.go
+28
-10
registry_test.go
registry/registry_test.go
+10
-4
No files found.
query/apigateway.go
View file @
66301fb8
package
query
import
(
"encoding/json"
"github.com/odysseus/service-registry/common"
"github.com/odysseus/service-registry/registry"
"github.com/redis/go-redis/v9"
)
type
GatewayInfo
struct
{
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Endpoint
string
`redis:"endpoint" json:"endpoint"`
}
func
(
g
GatewayInfo
)
TimeStamp
()
int64
{
return
g
.
Timestamp
}
func
(
g
GatewayInfo
)
Message
()
json
.
RawMessage
{
d
,
_
:=
json
.
Marshal
(
g
)
return
d
}
func
(
g
GatewayInfo
)
Parse
(
res
*
redis
.
MapStringStringCmd
)
ServiceInfo
{
var
info
GatewayInfo
var
regInfo
registry
.
RegistryInfo
if
err
:=
res
.
Scan
(
&
regInfo
);
err
!=
nil
{
return
nil
}
if
err
:=
json
.
Unmarshal
([]
byte
(
regInfo
.
Detail
),
&
info
);
err
!=
nil
{
return
nil
}
info
.
Timestamp
=
regInfo
.
Timestamp
info
.
Endpoint
=
regInfo
.
Endpoint
return
info
// gateway specific info
}
type
gatewayQuery
struct
{
...
...
query/backend.go
View file @
66301fb8
package
query
import
(
"encoding/json"
"github.com/odysseus/service-registry/common"
"github.com/odysseus/service-registry/registry"
"github.com/redis/go-redis/v9"
)
type
BackendInfo
struct
{
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Endpoint
string
`redis:"endpoint" json:"endpoint"`
}
func
(
g
BackendInfo
)
TimeStamp
()
int64
{
return
g
.
Timestamp
}
func
(
g
BackendInfo
)
Message
()
json
.
RawMessage
{
d
,
_
:=
json
.
Marshal
(
g
)
return
d
}
func
(
g
BackendInfo
)
Parse
(
res
*
redis
.
MapStringStringCmd
)
ServiceInfo
{
var
info
BackendInfo
var
regInfo
registry
.
RegistryInfo
if
err
:=
res
.
Scan
(
&
regInfo
);
err
!=
nil
{
return
nil
}
if
err
:=
json
.
Unmarshal
([]
byte
(
regInfo
.
Detail
),
&
info
);
err
!=
nil
{
return
nil
}
info
.
Timestamp
=
regInfo
.
Timestamp
info
.
Endpoint
=
regInfo
.
Endpoint
return
info
// backend specific info
}
type
backendQuery
struct
{
...
...
query/baseservice.go
View file @
66301fb8
...
...
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/odysseus/service-registry/common"
"github.com/odysseus/service-registry/registry"
"github.com/redis/go-redis/v9"
"sort"
)
...
...
@@ -39,20 +40,11 @@ func (g baseService) ServiceInfo(serviceid string) (string, error) {
}
func
parseInfo
(
stype
common
.
ServiceType
,
res
*
redis
.
MapStringStringCmd
)
ServiceInfo
{
var
ret
ServiceInfo
switch
stype
{
case
common
.
SERVICE_NODE_MANAGER
:
ret
=
NodeManagerInfo
{}
.
Parse
(
res
)
case
common
.
SERVICE_API_GATEWAY
:
ret
=
GatewayInfo
{}
.
Parse
(
res
)
case
common
.
SERVICE_BACKEND
:
ret
=
BackendInfo
{}
.
Parse
(
res
)
case
common
.
SERVICE_SCHEDULER
:
ret
=
SchedulerInfo
{}
.
Parse
(
res
)
case
common
.
SERVICE_WORKER
:
ret
=
WorkerInfo
{}
.
Parse
(
res
)
var
regInfo
registry
.
RegistryInfo
if
err
:=
res
.
Scan
(
&
regInfo
);
err
!=
nil
{
return
nil
}
return
re
t
return
re
gInfo
}
func
getAll
(
rdb
*
redis
.
Client
,
serviceType
common
.
ServiceType
)
(
ServiceInfoList
,
error
)
{
...
...
query/interface.go
View file @
66301fb8
package
query
import
(
"encoding/json"
"github.com/redis/go-redis/v9"
)
import
"encoding/json"
type
ServiceQuery
interface
{
ModuleName
()
string
List
()
([]
string
,
error
)
ServiceInfo
(
serviceid
string
)
(
string
,
error
)
ServiceInfo
(
instance
string
)
(
string
,
error
)
}
type
ServiceInfo
interface
{
TimeStamp
()
int64
Message
()
json
.
RawMessage
Parse
(
*
redis
.
MapStringStringCmd
)
ServiceInfo
}
type
ServiceInfoList
[]
ServiceInfo
...
...
query/nm.go
View file @
66301fb8
package
query
import
(
"encoding/json"
"github.com/odysseus/service-registry/common"
"github.com/odysseus/service-registry/registry"
"github.com/redis/go-redis/v9"
)
type
NodeManagerInfo
struct
{
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Endpoint
string
`redis:"endpoint" json:"endpoint"`
Status
string
`redis:"status" json:"status"`
}
func
(
g
NodeManagerInfo
)
TimeStamp
()
int64
{
return
g
.
Timestamp
}
func
(
g
NodeManagerInfo
)
Message
()
json
.
RawMessage
{
d
,
_
:=
json
.
Marshal
(
g
)
return
d
}
func
(
g
NodeManagerInfo
)
Parse
(
res
*
redis
.
MapStringStringCmd
)
ServiceInfo
{
var
info
NodeManagerInfo
var
regInfo
registry
.
RegistryInfo
if
err
:=
res
.
Scan
(
&
regInfo
);
err
!=
nil
{
return
nil
}
if
err
:=
json
.
Unmarshal
([]
byte
(
regInfo
.
Detail
),
&
info
);
err
!=
nil
{
return
nil
}
info
.
Timestamp
=
regInfo
.
Timestamp
info
.
Endpoint
=
regInfo
.
Endpoint
return
info
// node manager specific info
}
type
nodeManagerQuery
struct
{
...
...
query/schedule.go
View file @
66301fb8
package
query
import
(
"encoding/json"
"github.com/odysseus/service-registry/common"
"github.com/odysseus/service-registry/registry"
"github.com/redis/go-redis/v9"
)
type
SchedulerInfo
struct
{
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Endpoint
string
`redis:"endpoint" json:"endpoint"`
Status
string
`redis:"status" json:"status"`
}
func
(
g
SchedulerInfo
)
TimeStamp
()
int64
{
return
g
.
Timestamp
}
func
(
g
SchedulerInfo
)
Message
()
json
.
RawMessage
{
d
,
_
:=
json
.
Marshal
(
g
)
return
d
}
func
(
g
SchedulerInfo
)
Parse
(
res
*
redis
.
MapStringStringCmd
)
ServiceInfo
{
var
info
SchedulerInfo
var
regInfo
registry
.
RegistryInfo
if
err
:=
res
.
Scan
(
&
regInfo
);
err
!=
nil
{
return
nil
}
if
err
:=
json
.
Unmarshal
([]
byte
(
regInfo
.
Detail
),
&
info
);
err
!=
nil
{
return
nil
}
info
.
Timestamp
=
regInfo
.
Timestamp
info
.
Endpoint
=
regInfo
.
Endpoint
return
info
// scheduler specific info
}
type
schedulerQuery
struct
{
...
...
query/worker.go
View file @
66301fb8
package
query
import
(
"encoding/json"
"github.com/odysseus/service-registry/common"
"github.com/odysseus/service-registry/registry"
"github.com/redis/go-redis/v9"
)
type
WorkerInfo
struct
{
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Endpoint
string
`redis:"endpoint" json:"endpoint"`
// worker specific info
HearBeat
int64
`redis:"heartbeat" json:"heartbeat"`
ActiveNM
[]
string
`redis:"active_nm" json:"active_nm"`
MinerAddress
string
`redis:"miner_address" json:"miner_address"`
BenefitAddress
string
`redis:"benefit_address" json:"benefit_address"`
IPs
[]
string
`redis:"ip_list" json:"ip_list"`
Status
string
`redis:"status" json:"status"`
}
func
(
g
WorkerInfo
)
TimeStamp
()
int64
{
return
g
.
Timestamp
}
func
(
g
WorkerInfo
)
Message
()
json
.
RawMessage
{
d
,
_
:=
json
.
Marshal
(
g
)
return
d
}
func
(
g
WorkerInfo
)
Parse
(
res
*
redis
.
MapStringStringCmd
)
ServiceInfo
{
var
info
WorkerInfo
var
regInfo
registry
.
RegistryInfo
if
err
:=
res
.
Scan
(
&
regInfo
);
err
!=
nil
{
return
nil
}
if
err
:=
json
.
Unmarshal
([]
byte
(
regInfo
.
Detail
),
&
info
);
err
!=
nil
{
return
nil
}
info
.
Timestamp
=
regInfo
.
Timestamp
info
.
Endpoint
=
regInfo
.
Endpoint
return
info
IP
string
`redis:"ip" json:"ip"`
}
type
workerQuery
struct
{
...
...
registry/registry.go
View file @
66301fb8
...
...
@@ -12,15 +12,25 @@ import (
)
type
RegistryInfo
struct
{
ServiceName
string
`redis:"service_name" json:"service_name"`
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Endpoint
string
`redis:"endpoint" json:"endpoint"`
Detail
string
`redis:"detail" json:"detail"`
Timestamp
int64
`redis:"timestamp" json:"timestamp"`
Instance
string
`redis:"instance" json:"instance"`
// example: hostip + hostname
Status
string
`redis:"status" json:"status"`
// example: kafka failed or running.
Detail
string
`redis:"detail" json:"detail"`
}
func
(
g
RegistryInfo
)
TimeStamp
()
int64
{
return
g
.
Timestamp
}
func
(
g
RegistryInfo
)
Message
()
json
.
RawMessage
{
d
,
_
:=
json
.
Marshal
(
g
)
return
d
}
type
Register
interface
{
ServiceType
()
common
.
ServiceType
Endpoint
()
string
Instance
()
string
Status
()
string
DetailInfo
()
(
json
.
RawMessage
,
error
)
}
...
...
@@ -38,6 +48,13 @@ type RedisConnParam struct {
}
func
NewRegistry
(
redisParam
RedisConnParam
,
register
Register
)
*
Registry
{
switch
register
.
ServiceType
()
{
case
common
.
SERVICE_NODE_MANAGER
,
common
.
SERVICE_API_GATEWAY
,
common
.
SERVICE_BACKEND
,
common
.
SERVICE_SCHEDULER
,
common
.
SERVICE_WORKER
:
//nothing
default
:
log
.
WithField
(
"service"
,
register
.
ServiceType
())
.
Error
(
"not support service type"
)
return
nil
}
rdb
:=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
redisParam
.
Addr
,
Password
:
redisParam
.
Password
,
...
...
@@ -74,18 +91,19 @@ func (s *Registry) Stop() {
}
func
(
s
*
Registry
)
registry
(
rdb
*
redis
.
Client
)
error
{
k
:=
fmt
.
Sprintf
(
"%s%s"
,
common
.
GetServiceKeyPrefix
(
s
.
register
.
ServiceType
()),
s
.
register
.
Endpoint
())
k
:=
fmt
.
Sprintf
(
"%s%s"
,
common
.
GetServiceKeyPrefix
(
s
.
register
.
ServiceType
()),
s
.
register
.
Instance
())
detail
,
err
:=
s
.
register
.
DetailInfo
()
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get detail info failed"
)
return
err
}
status
:=
s
.
register
.
Status
()
err
=
rdb
.
HSet
(
context
.
Background
(),
k
,
RegistryInfo
{
ServiceName
:
s
.
register
.
ServiceType
()
.
String
(),
Timestamp
:
time
.
Now
()
.
Unix
()
,
Endpoint
:
s
.
register
.
Endpoint
(),
Detail
:
string
(
detail
),
Timestamp
:
time
.
Now
()
.
Unix
(),
Status
:
status
,
Instance
:
s
.
register
.
Instance
(),
Detail
:
string
(
detail
),
})
.
Err
()
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"set register info failed"
)
...
...
registry/registry_test.go
View file @
66301fb8
...
...
@@ -3,6 +3,7 @@ package registry
import
(
"encoding/json"
"github.com/odysseus/service-registry/common"
"os"
"testing"
"time"
)
...
...
@@ -10,12 +11,17 @@ import (
type
demoService
struct
{
}
func
(
d
demoService
)
ServiceType
()
common
.
ServiceType
{
return
common
.
SERVICE_NODE_MANAGER
func
(
d
demoService
)
Instance
()
string
{
hname
,
_
:=
os
.
Hostname
()
return
"demo"
+
hname
}
func
(
d
demoService
)
Status
()
string
{
return
"running"
}
func
(
d
demoService
)
Endpoint
()
string
{
return
"http://127.0.0.1:10001"
func
(
d
demoService
)
ServiceType
()
common
.
ServiceType
{
return
common
.
SERVICE_NODE_MANAGER
}
func
(
d
demoService
)
DetailInfo
()
(
json
.
RawMessage
,
error
)
{
...
...
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