gsvc.go 5.8 KB

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