serviceregistries.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "net"
  18. "strconv"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/etcd/models/base"
  21. )
  22. type SServiceRegistryManager struct {
  23. base.SEtcdBaseModelManager
  24. }
  25. type SServiceRegistry struct {
  26. base.SEtcdBaseModel
  27. Provider string
  28. Environment string
  29. Region string
  30. Zone string
  31. ServiceType string
  32. Address string
  33. Port int
  34. }
  35. var ServiceRegistryManager *SServiceRegistryManager
  36. func init() {
  37. ServiceRegistryManager = &SServiceRegistryManager{
  38. SEtcdBaseModelManager: base.NewEtcdBaseModelManager(
  39. &SServiceRegistry{},
  40. "service-registry",
  41. "service-registries",
  42. ),
  43. }
  44. ServiceRegistryManager.SetVirtualObject(ServiceRegistryManager)
  45. }
  46. func (manager *SServiceRegistryManager) Register(ctx context.Context, addr string, port int,
  47. provider string, env string, region string, zone string, serviceType string) error {
  48. log.Infof("register service %s %s:%d", serviceType, addr, port)
  49. sr := SServiceRegistry{}
  50. sr.ID = net.JoinHostPort(addr, strconv.Itoa(port))
  51. sr.Provider = provider
  52. sr.Environment = env
  53. sr.Region = region
  54. sr.Zone = zone
  55. sr.ServiceType = serviceType
  56. sr.Address = addr
  57. sr.Port = port
  58. return manager.Session(ctx, &sr)
  59. }