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

Commit

Permalink
2.x client (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
MI-cool authored Jul 14, 2023
1 parent 16dd4bd commit 43997e3
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 122 deletions.
2 changes: 1 addition & 1 deletion model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func ReplaceNacosService(service string) string {

func BuildServiceName(svcName, groupName string) string {
if groupName == DefaultServiceGroup {
groupName = ""
return svcName
}
return groupName + ReplaceNacosGroupConnectStr + svcName
}
19 changes: 13 additions & 6 deletions v2/pb/internal_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type InternalRequest struct {
// NewInternalRequest .
func NewInternalRequest() *InternalRequest {
request := Request{
Headers: make(map[string]string, 8),
Headers: make(map[string]string, 8),
RequestId: "",
}
return &InternalRequest{
Request: &request,
Expand All @@ -51,7 +52,7 @@ func NewHealthCheckRequest() *HealthCheckRequest {
}

func (r *HealthCheckRequest) GetRequestType() string {
return "HealthCheckRequest"
return TypeHealthCheckRequest
}

// ConnectResetRequest
Expand All @@ -61,8 +62,14 @@ type ConnectResetRequest struct {
ServerPort string
}

func NewConnectResetRequest() *ConnectResetRequest {
return &ConnectResetRequest{
InternalRequest: NewInternalRequest(),
}
}

func (r *ConnectResetRequest) GetRequestType() string {
return "ConnectResetRequest"
return TypeConnectResetRequest
}

// ClientDetectionRequest
Expand All @@ -77,7 +84,7 @@ func NewClientDetectionRequest() *ClientDetectionRequest {
}

func (r *ClientDetectionRequest) GetRequestType() string {
return "ClientDetectionRequest"
return TypeClientDetectionRequest
}

// ServerCheckRequest
Expand All @@ -93,7 +100,7 @@ func NewServerCheckRequest() *ServerCheckRequest {
}

func (r *ServerCheckRequest) GetRequestType() string {
return "ServerCheckRequest"
return TypeServerCheckRequest
}

// ConnectionSetupRequest
Expand All @@ -113,5 +120,5 @@ func NewConnectionSetupRequest() *ConnectionSetupRequest {
}

func (r *ConnectionSetupRequest) GetRequestType() string {
return "ConnectionSetupRequest"
return TypeConnectionSetupRequest
}
90 changes: 58 additions & 32 deletions v2/pb/naming_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@ type NamingRequest struct {
}

// NewNamingRequest
func NewNamingRequest(namespace, serviceName, groupName string) *NamingRequest {
request := Request{
Headers: make(map[string]string, 8),
func NewNamingRequest() *NamingRequest {
request := &Request{
Headers: make(map[string]string, 8),
RequestId: "",
}
return &NamingRequest{
Request: &request,
Request: request,
Namespace: model.DefaultNacosNamespace,
ServiceName: "",
GroupName: model.DefaultServiceGroup,
Module: "naming",
}
}

func NewBasicNamingRequest(requestId, namespace, serviceName, groupName string) *NamingRequest {
request := &Request{
Headers: make(map[string]string, 8),
RequestId: requestId,
}
return &NamingRequest{
Request: request,
Namespace: namespace,
ServiceName: serviceName,
GroupName: groupName,
Expand All @@ -64,17 +79,16 @@ type InstanceRequest struct {
}

// NewInstanceRequest
func NewInstanceRequest(namespace, serviceName, groupName, reqType string,
instance model.Instance) *InstanceRequest {
func NewInstanceRequest() *InstanceRequest {
return &InstanceRequest{
NamingRequest: NewNamingRequest(namespace, serviceName, groupName),
Type: reqType,
Instance: instance,
NamingRequest: NewNamingRequest(),
Type: TypeInstanceRequest,
Instance: model.Instance{},
}
}

func (r *InstanceRequest) GetRequestType() string {
return "InstanceRequest"
return TypeInstanceRequest
}

// BatchInstanceRequest .
Expand All @@ -84,8 +98,16 @@ type BatchInstanceRequest struct {
Instances []*model.Instance `json:"instances"`
}

func NewBatchInstanceRequest() *BatchInstanceRequest {
return &BatchInstanceRequest{
NamingRequest: NewNamingRequest(),
Type: TypeBatchInstanceRequest,
Instances: make([]*model.Instance, 0),
}
}

func (r *BatchInstanceRequest) GetRequestType() string {
return "BatchInstanceRequest"
return TypeBatchInstanceRequest
}

func (r *BatchInstanceRequest) Normalize() {
Expand All @@ -103,8 +125,15 @@ type NotifySubscriberRequest struct {
ServiceInfo *model.ServiceInfo `json:"serviceInfo"`
}

func NewNotifySubscriberRequest() *NotifySubscriberRequest {
return &NotifySubscriberRequest{
NamingRequest: NewNamingRequest(),
ServiceInfo: &model.ServiceInfo{},
}
}

func (r *NotifySubscriberRequest) GetRequestType() string {
return "NotifySubscriberRequest"
return TypeNotifySubscriberRequest
}

// SubscribeServiceRequest
Expand All @@ -115,17 +144,16 @@ type SubscribeServiceRequest struct {
}

// NewSubscribeServiceRequest .
func NewSubscribeServiceRequest(namespace, serviceName, groupName, clusters string,
subscribe bool) *SubscribeServiceRequest {
func NewSubscribeServiceRequest() *SubscribeServiceRequest {
return &SubscribeServiceRequest{
NamingRequest: NewNamingRequest(namespace, serviceName, groupName),
Subscribe: subscribe,
Clusters: clusters,
NamingRequest: NewNamingRequest(),
Subscribe: true,
Clusters: "",
}
}

func (r *SubscribeServiceRequest) GetRequestType() string {
return "SubscribeServiceRequest"
return TypeSubscribeServiceRequest
}

// ServiceListRequest
Expand All @@ -137,18 +165,17 @@ type ServiceListRequest struct {
}

// NewServiceListRequest .
func NewServiceListRequest(namespace, serviceName, groupName string, pageNo,
pageSize int, selector string) *ServiceListRequest {
func NewServiceListRequest() *ServiceListRequest {
return &ServiceListRequest{
NamingRequest: NewNamingRequest(namespace, serviceName, groupName),
PageNo: pageNo,
PageSize: pageSize,
Selector: selector,
NamingRequest: NewNamingRequest(),
PageNo: 0,
PageSize: 10,
Selector: "",
}
}

func (r *ServiceListRequest) GetRequestType() string {
return "ServiceListRequest"
return TypeServiceListRequest
}

// ServiceQueryRequest
Expand All @@ -160,16 +187,15 @@ type ServiceQueryRequest struct {
}

// NewServiceQueryRequest .
func NewServiceQueryRequest(namespace, serviceName, groupName, cluster string, healthyOnly bool,
udpPort int) *ServiceQueryRequest {
func NewServiceQueryRequest() *ServiceQueryRequest {
return &ServiceQueryRequest{
NamingRequest: NewNamingRequest(namespace, serviceName, groupName),
Cluster: cluster,
HealthyOnly: healthyOnly,
UdpPort: udpPort,
NamingRequest: NewNamingRequest(),
Cluster: "",
HealthyOnly: false,
UdpPort: 0,
}
}

func (r *ServiceQueryRequest) GetRequestType() string {
return "ServiceQueryRequest"
return TypeServiceQueryRequest
}
74 changes: 16 additions & 58 deletions v2/pb/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,20 @@ import (
"encoding/json"
)

var (
customerPayloadRegistry = map[string]func() BaseRequest{}
)

const (
// TypeConnectionSetupRequest
TypeConnectionSetupRequest = "ConnectionSetupRequest"
// TypeServerCheckRequest
TypeServerCheckRequest = "ServerCheckRequest"
// TypeInstanceRequest
TypeInstanceRequest = "InstanceRequest"
// TypeBatchInstanceRequest
TypeBatchInstanceRequest = "BatchInstanceRequest"
TypeConnectionSetupRequest = "ConnectionSetupRequest"
TypeConnectResetRequest = "ConnectResetRequest"
TypeClientDetectionRequest = "ClientDetectionRequest"
TypeHealthCheckRequest = "HealthCheckRequest"
TypeServerCheckRequest = "ServerCheckRequest"
TypeInstanceRequest = "InstanceRequest"
TypeBatchInstanceRequest = "BatchInstanceRequest"
TypeNotifySubscriberRequest = "NotifySubscriberRequest"
TypeSubscribeServiceRequest = "SubscribeServiceRequest"
TypeServiceListRequest = "ServiceListRequest"
TypeServiceQueryRequest = "ServiceQueryRequest"
)

func init() {
// system
registryCustomerPayload(func() BaseRequest {
return &ConnectionSetupRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &ConnectResetRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &ServerCheckRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &ClientDetectionRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &HealthCheckRequest{}
})

// discovery
registryCustomerPayload(func() BaseRequest {
return &InstanceRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &BatchInstanceRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &NotifySubscriberRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &SubscribeServiceRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &ServiceListRequest{}
})
registryCustomerPayload(func() BaseRequest {
return &ServiceQueryRequest{}
})
}

func registryCustomerPayload(builder func() BaseRequest) {
example := builder()
customerPayloadRegistry[example.GetRequestType()] = builder
}

// CustomerPayload
type CustomerPayload interface{}

Expand Down Expand Up @@ -108,6 +63,9 @@ type BaseRequest interface {
}

func (r *Request) PutAllHeaders(headers map[string]string) {
if r.Headers == nil {
r.Headers = make(map[string]string)
}
for k, v := range headers {
r.Headers[k] = v
}
Expand All @@ -118,8 +76,8 @@ func (r *Request) ClearHeaders() {
}

func (r *Request) GetHeaders() map[string]string {
if len(r.Headers) == 0 {
return map[string]string{}
if r.Headers == nil {
r.Headers = make(map[string]string)
}
return r.Headers
}
Expand Down
Loading

0 comments on commit 43997e3

Please sign in to comment.