From bbb96188a1468beede1997f9662d4e6b05c8156d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Jun 2022 11:42:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E9=85=8D=E7=BD=AE=E4=BB=A5=E5=8F=8AAPI=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 17 +++++++------ rpc.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/define.go b/define.go index dccb701..466ef40 100644 --- a/define.go +++ b/define.go @@ -13,14 +13,14 @@ package rpc // // Date : 14:08 2022/6/29 type Service struct { - Flag string `json:"flag" yaml:"flag"` // 服务标识, 全局唯一 - Domain string `json:"domain" yaml:"domain"` // 域名服务 - CodeField string `json:"code_field" yaml:"code_field"` // 状态码字段 - MessageField string `json:"message_field" yaml:"message_field"` // 消息字段 - DataField string `json:"data_field" yaml:"data_field"` // 数据字段 - SuccessCodeList []string `json:"success_code_list" yaml:"success_code_list"` // 成功的值 - ApiTable map[string]interface{} `json:"api_table" yaml:"api_table"` // api列表 - ApiRetry ApiRetry `json:"api_retry" yaml:"api_retry"` // 重试策略 + Flag string `json:"flag" yaml:"flag"` // 服务标识, 全局唯一 + Domain string `json:"domain" yaml:"domain"` // 域名服务 + CodeField string `json:"code_field" yaml:"code_field"` // 状态码字段 + MessageField string `json:"message_field" yaml:"message_field"` // 消息字段 + DataField string `json:"data_field" yaml:"data_field"` // 数据字段 + SuccessCodeList []string `json:"success_code_list" yaml:"success_code_list"` // 成功的值 + ApiTable map[string]*Api `json:"api_table" yaml:"api_table"` // api列表 + ApiRetry ApiRetry `json:"api_retry" yaml:"api_retry"` // 重试策略 } // Api 接口的数据结构 @@ -29,6 +29,7 @@ type Service struct { // // Date : 14:18 2022/6/29 type Api struct { + Flag string `json:"flag" yaml:"flag"` // URI标识 URI string `json:"uri" yaml:"uri"` // 接口地址 Method string `json:"method" yaml:"method"` // 请求方法 GET / POST / PUT 等 CodeField string `json:"code_field" yaml:"code_field"` // 状态码字段 diff --git a/rpc.go b/rpc.go index be4d3c2..8cfb46a 100644 --- a/rpc.go +++ b/rpc.go @@ -8,6 +8,7 @@ package rpc import ( + "errors" "sync" "go.uber.org/zap" @@ -23,7 +24,15 @@ var ( // Author : go_developer@163.com<白茶清欢> // // Date : 15:23 2022/6/29 -func InitRPC(serviceTable map[string]Service, loggerInstance *zap.Logger) error { +func InitRPC(serviceTable map[string]*Service, loggerInstance *zap.Logger) error { + if nil == serviceTable { + serviceTable = make(map[string]*Service) + } + for _, item := range serviceTable { + if item.ApiTable == nil { + item.ApiTable = make(map[string]*Api) + } + } Request = &request{ logger: loggerInstance, serviceTable: serviceTable, @@ -34,6 +43,66 @@ func InitRPC(serviceTable map[string]Service, loggerInstance *zap.Logger) error type request struct { logger *zap.Logger - serviceTable map[string]Service + serviceTable map[string]*Service lock *sync.RWMutex } + +// AddService 新增一个服务 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:13 2022/6/29 +func (r *request) AddService(serviceInfo *Service) error { + if nil == serviceInfo { + return errors.New("service info is nil") + } + r.lock.Lock() + defer r.lock.Unlock() + if _, exist := r.serviceTable[serviceInfo.Flag]; exist { + return errors.New(serviceInfo.Flag + " already exist") + } + r.serviceTable[serviceInfo.Flag] = serviceInfo + return nil +} + +// GetServiceInfo ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:32 2022/6/30 +func (r *request) GetServiceInfo(serviceFlag string) (*Service, error) { + var ( + serviceInfo *Service + exist bool + ) + + r.lock.RLock() + defer r.lock.Unlock() + if serviceInfo, exist = r.serviceTable[serviceFlag]; !exist { + return nil, errors.New(serviceFlag + " -> 服务不存在") + } + return serviceInfo, nil +} + +// AddServiceApi 向一个service中增加Api +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:26 2022/6/30 +func (r *request) AddServiceApi(serviceFlag string, apiConfig *Api) error { + var ( + serviceInfo *Service + err error + ) + + if serviceInfo, err = r.GetServiceInfo(serviceFlag); nil != err { + return err + } + r.lock.Lock() + defer r.lock.Unlock() + if nil == serviceInfo.ApiTable { + serviceInfo.ApiTable = make(map[string]*Api) + } + serviceInfo.ApiTable[apiConfig.Flag] = apiConfig + return nil +}