Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
nacos client 1.x compatible (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MI-cool committed Jul 10, 2023
1 parent a2e4d68 commit 14fe29a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 44 deletions.
2 changes: 1 addition & 1 deletion core/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func NoopSelectInstances(ctx *FilterContext, result *nacosmodel.ServiceInfo,
func SelectInstancesWithHealthyProtection(ctx *FilterContext, result *nacosmodel.ServiceInfo,
instances []*nacosmodel.Instance, healthCount int32) *nacosmodel.ServiceInfo {
protectThreshold := ctx.Service.ProtectionThreshold
if float64(healthCount)/float64(len(instances)) <= protectThreshold {
if len(instances) > 0 && float64(healthCount)/float64(len(instances)) <= protectThreshold {
ret := instances
if ctx.HealthyOnly {
healthyIns := make([]*nacosmodel.Instance, 0, len(instances))
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/zap v1.24.0
golang.org/x/net v0.10.0
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sync v0.2.0
golang.org/x/text v0.9.0 // indirect
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
Expand All @@ -35,7 +35,6 @@ require (
github.com/prometheus/procfs v0.10.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230525234025-438c736192d0 // indirect
)

require (
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tyl
github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.43.0 h1:iq+BVjvYLei5f27wiuNiB1DN6DYQkp1c8Bx0Vykh5us=
github.com/prometheus/common v0.43.0/go.mod h1:NCvr5cQIh3Y/gy73/RdVtC9r8xxrxwJnB+2lB3BxrFc=
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w=
Expand Down Expand Up @@ -122,10 +118,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
google.golang.org/genproto v0.0.0-20230525234025-438c736192d0 h1:x1vNwUhVOcsYoKyEGCZBH694SBmmBjA2EfauFVEI2+M=
google.golang.org/genproto v0.0.0-20230525234025-438c736192d0/go.mod h1:9ExIQyXL5hZrHzQceCwuSYwZZ5QZBazOcprJ5rgs3lY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag=
Expand Down
3 changes: 3 additions & 0 deletions model/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const (
ParamInstanceEnable = "enable"
ParamInstanceMetadata = "metadata"
ParamInstanceBeat = "beat"
ParamPageNo = "pageNo"
ParamPageSize = "pageSize"
ParamSelector = "selector"
)

const (
Expand Down
1 change: 1 addition & 0 deletions model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewEmptyServiceInfo(name, group string) *ServiceInfo {
return &ServiceInfo{
Name: name,
GroupName: group,
Hosts: []*Instance{},
}
}

Expand Down
42 changes: 42 additions & 0 deletions model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

package model

import (
"strings"

"github.com/polarismesh/polaris/service"
)

type ServiceKey struct {
Namespace string
Group string
Expand All @@ -29,3 +35,39 @@ type ServiceMetadata struct {
ProtectionThreshold float64
ExtendData map[string]string
}

func HandleServiceListRequest(discoverSvr service.DiscoverServer, namespace string, groupName string,
pageNo int, pageSize int) ([]string, int) {
_, services := discoverSvr.Cache().Service().ListServices(namespace)
offset := (pageNo - 1) * pageSize
limit := pageSize
if offset < 0 {
offset = 0
}
if offset > len(services) {
return []string{}, 0
}
groupPrefix := groupName + ReplaceNacosGroupConnectStr
if groupName == DefaultServiceGroup {
groupPrefix = ""
}
hasGroupPrefix := len(groupPrefix) != 0
temp := make([]string, 0, len(services))
for i := range services {
svc := services[i]
if !hasGroupPrefix {
temp = append(temp, svc.Name)
continue
}
if strings.HasPrefix(svc.Name, groupPrefix) {
temp = append(temp, GetServiceName(svc.Name))
}
}
var viewList []string
if offset+limit > len(services) {
viewList = temp[offset:]
} else {
viewList = temp[offset : offset+limit]
}
return viewList, len(services)
}
27 changes: 27 additions & 0 deletions v1/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (n *NacosV1Server) GetAuthServer() (*restful.WebService, error) {
return ws, nil
}

func (n *NacosV1Server) AddServiceAccess(ws *restful.WebService) {
ws.Route(ws.GET("/service/list").To(n.ListServices))
}

func (n *NacosV1Server) addInstanceAccess(ws *restful.WebService) {
ws.Route(ws.POST("/instance").To(n.RegisterInstance))
ws.Route(ws.DELETE("/instance").To(n.DeRegisterInstance))
Expand All @@ -67,6 +71,29 @@ func (n *NacosV1Server) Login(req *restful.Request, rsp *restful.Response) {
core.WrirteNacosResponse(data, rsp)
}

func (n *NacosV1Server) ListServices(req *restful.Request, rsp *restful.Response) {
pageNo, err := requiredInt(req, model.ParamPageNo)
if err != nil {
core.WrirteNacosErrorResponse(err, rsp)
return
}
pageSize, err := requiredInt(req, model.ParamPageSize)
if err != nil {
core.WrirteNacosErrorResponse(err, rsp)
return
}
namespace := optional(req, model.ParamNamespaceID, model.DefaultNacosNamespace)
namespace = model.ToPolarisNamespace(namespace)
groupName := optional(req, model.ParamGroupName, model.DefaultServiceGroup)
// selector := optional(req, model.ParamSelector, "")
serviceList, count := model.HandleServiceListRequest(n.discoverSvr, namespace, groupName, pageNo, pageSize)
resp := map[string]interface{}{
"count": count,
"doms": serviceList,
}
core.WrirteNacosResponse(resp, rsp)
}

func (n *NacosV1Server) RegisterInstance(req *restful.Request, rsp *restful.Response) {
handler := Handler{
Request: req,
Expand Down
12 changes: 12 additions & 0 deletions v1/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,15 @@ func required(req *restful.Request, key string) (string, error) {
}
return val, nil
}

func requiredInt(req *restful.Request, key string) (int, error) {
strValue, err := required(req, key)
if err != nil {
return 0, err
}
value, err := strconv.Atoi(strValue)
if err != nil {
return 0, fmt.Errorf("key: %s is not a number", key)
}
return value, nil
}
36 changes: 3 additions & 33 deletions v2/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package v2

import (
"context"
"strings"

"github.com/polaris-contrib/apiserver-nacos/model"
nacosmodel "github.com/polaris-contrib/apiserver-nacos/model"
Expand All @@ -43,38 +42,9 @@ func (h *NacosV2Server) handleServiceListRequest(ctx context.Context, req nacosp
}

namespace := nacosmodel.ToPolarisNamespace(svcListReq.Namespace)
_, services := h.discoverSvr.Cache().Service().ListServices(namespace)
offset := (svcListReq.PageNo - 1) * svcListReq.PageSize
limit := svcListReq.PageSize
if offset < 0 {
offset = 0
}
if offset > len(services) {
return resp, nil
}
groupPrefix := svcListReq.GroupName + model.ReplaceNacosGroupConnectStr
if svcListReq.GroupName == model.DefaultServiceGroup {
groupPrefix = ""
}
hasGroupPrefix := len(groupPrefix) != 0
temp := make([]string, 0, len(services))
for i := range services {
svc := services[i]
if !hasGroupPrefix {
temp = append(temp, svc.Name)
continue
}
if strings.HasPrefix(svc.Name, groupPrefix) {
temp = append(temp, model.GetServiceName(svc.Name))
}
}
var viewList []string
if offset+limit > len(services) {
viewList = temp[offset:]
} else {
viewList = temp[offset : offset+limit]
}
viewList, count := model.HandleServiceListRequest(h.discoverSvr, namespace, svcListReq.GroupName,
svcListReq.PageNo, svcListReq.PageSize)
resp.ServiceNames = viewList
resp.Count = len(temp)
resp.Count = count
return resp, nil
}

0 comments on commit 14fe29a

Please sign in to comment.