486699c7097e186011422582309f923850a441f8.svn-base 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/longjoy/micro-go-course/section13/register/discovery"
  6. "log"
  7. )
  8. type Service interface {
  9. HealthCheck() string
  10. DiscoveryService(ctx context.Context, serviceName string) ([]*discovery.InstanceInfo, error)
  11. }
  12. var ErrNotServiceInstances = errors.New("instances are not existed")
  13. type RegisterServiceImpl struct {
  14. discoveryClient *discovery.DiscoveryClient
  15. }
  16. func NewRegisterServiceImpl(discoveryClient *discovery.DiscoveryClient) Service {
  17. return &RegisterServiceImpl{
  18. discoveryClient:discoveryClient,
  19. }
  20. }
  21. func (service *RegisterServiceImpl) DiscoveryService(ctx context.Context, serviceName string) ([]*discovery.InstanceInfo, error) {
  22. instances, err := service.discoveryClient.DiscoverServices(ctx, serviceName)
  23. if err != nil {
  24. log.Printf("get service info err: %s", err)
  25. }
  26. if instances == nil || len(instances) == 0 {
  27. return nil, ErrNotServiceInstances
  28. }
  29. return instances, nil
  30. }
  31. func (*RegisterServiceImpl) HealthCheck() string {
  32. return "OK"
  33. }