gsvc.go 4.6 KB

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