github.com/gogf/gf/v2@v2.7.4/net/gsvc/gsvc.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gsvc provides service registry and discovery definition. 8 package gsvc 9 10 import ( 11 "context" 12 "time" 13 14 "github.com/gogf/gf/v2/errors/gerror" 15 ) 16 17 // Registry interface for service. 18 type Registry interface { 19 Registrar 20 Discovery 21 } 22 23 // Registrar interface for service registrar. 24 type Registrar interface { 25 // Register registers `service` to Registry. 26 // Note that it returns a new Service if it changes the input Service with custom one. 27 Register(ctx context.Context, service Service) (registered Service, err error) 28 29 // Deregister off-lines and removes `service` from the Registry. 30 Deregister(ctx context.Context, service Service) error 31 } 32 33 // Discovery interface for service discovery. 34 type Discovery interface { 35 // Search searches and returns services with specified condition. 36 Search(ctx context.Context, in SearchInput) (result []Service, err error) 37 38 // Watch watches specified condition changes. 39 // The `key` is the prefix of service key. 40 Watch(ctx context.Context, key string) (watcher Watcher, err error) 41 } 42 43 // Watcher interface for service. 44 type Watcher interface { 45 // Proceed proceeds watch in blocking way. 46 // It returns all complete services that watched by `key` if any change. 47 Proceed() (services []Service, err error) 48 49 // Close closes the watcher. 50 Close() error 51 } 52 53 // Service interface for service definition. 54 type Service interface { 55 // GetName returns the name of the service. 56 // The name is necessary for a service, and should be unique among services. 57 GetName() string 58 59 // GetVersion returns the version of the service. 60 // It is suggested using GNU version naming like: v1.0.0, v2.0.1, v2.1.0-rc. 61 // A service can have multiple versions deployed at once. 62 // If no version set in service, the default version of service is "latest". 63 GetVersion() string 64 65 // GetKey formats and returns a unique key string for service. 66 // The result key is commonly used for key-value registrar server. 67 GetKey() string 68 69 // GetValue formats and returns the value of the service. 70 // The result value is commonly used for key-value registrar server. 71 GetValue() string 72 73 // GetPrefix formats and returns the key prefix string. 74 // The result prefix string is commonly used in key-value registrar server 75 // for service searching. 76 // 77 // Take etcd server for example, the prefix string is used like: 78 // `etcdctl get /services/prod/hello.svc --prefix` 79 GetPrefix() string 80 81 // GetMetadata returns the Metadata map of service. 82 // The Metadata is key-value pair map specifying extra attributes of a service. 83 GetMetadata() Metadata 84 85 // GetEndpoints returns the Endpoints of service. 86 // The Endpoints contain multiple host/port information of service. 87 GetEndpoints() Endpoints 88 } 89 90 // Endpoint interface for service. 91 type Endpoint interface { 92 // Host returns the IPv4/IPv6 address of a service. 93 Host() string 94 95 // Port returns the port of a service. 96 Port() int 97 98 // String formats and returns the Endpoint as a string. 99 String() string 100 } 101 102 // Endpoints are composed by multiple Endpoint. 103 type Endpoints []Endpoint 104 105 // Metadata stores custom key-value pairs. 106 type Metadata map[string]interface{} 107 108 // SearchInput is the input for service searching. 109 type SearchInput struct { 110 Prefix string // Search by key prefix. 111 Name string // Search by service name. 112 Version string // Search by service version. 113 Metadata Metadata // Filter by metadata if there are multiple result. 114 } 115 116 const ( 117 Schema = `service` // Schema is the schema of service. 118 DefaultHead = `service` // DefaultHead is the default head of service. 119 DefaultDeployment = `default` // DefaultDeployment is the default deployment of service. 120 DefaultNamespace = `default` // DefaultNamespace is the default namespace of service. 121 DefaultVersion = `latest` // DefaultVersion is the default version of service. 122 EnvPrefix = `GF_GSVC_PREFIX` // EnvPrefix is the environment variable prefix. 123 EnvDeployment = `GF_GSVC_DEPLOYMENT` // EnvDeployment is the environment variable deployment. 124 EnvNamespace = `GF_GSVC_NAMESPACE` // EnvNamespace is the environment variable namespace. 125 EnvName = `GF_GSVC_Name` // EnvName is the environment variable name. 126 EnvVersion = `GF_GSVC_VERSION` // EnvVersion is the environment variable version. 127 MDProtocol = `protocol` // MDProtocol is the metadata key for protocol. 128 MDInsecure = `insecure` // MDInsecure is the metadata key for insecure. 129 MDWeight = `weight` // MDWeight is the metadata key for weight. 130 DefaultProtocol = `http` // DefaultProtocol is the default protocol of service. 131 DefaultSeparator = "/" // DefaultSeparator is the default separator of service. 132 EndpointHostPortDelimiter = ":" // EndpointHostPortDelimiter is the delimiter of host and port. 133 defaultTimeout = 5 * time.Second // defaultTimeout is the default timeout for service registry. 134 EndpointsDelimiter = "," // EndpointsDelimiter is the delimiter of endpoints. 135 ) 136 137 var defaultRegistry Registry 138 139 // SetRegistry sets the default Registry implements as your own implemented interface. 140 func SetRegistry(registry Registry) { 141 if registry == nil { 142 panic(gerror.New(`invalid Registry value "nil" given`)) 143 } 144 defaultRegistry = registry 145 } 146 147 // GetRegistry returns the default Registry that is previously set. 148 // It returns nil if no Registry is set. 149 func GetRegistry() Registry { 150 return defaultRegistry 151 }