| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 | 
							- package service
 
- import (
 
- 	"context"
 
- 	"errors"
 
- 	"github.com/hashicorp/consul/api"
 
- 	"github.com/longjoy/micro-go-course/section14/register/discovery"
 
- 	"log"
 
- )
 
- type Service interface {
 
- 	HealthCheck() string
 
- 	DiscoveryService(ctx context.Context, serviceName string) ([]*api.AgentService, error)
 
- }
 
- var ErrNotServiceInstances = errors.New("instances are not existed")
 
- type RegisterServiceImpl struct {
 
- 	discoveryClient *discovery.DiscoveryClient
 
- }
 
- func NewRegisterServiceImpl(discoveryClient *discovery.DiscoveryClient) Service  {
 
- 	return &RegisterServiceImpl{
 
- 		discoveryClient:discoveryClient,
 
- 	}
 
- }
 
- func (service *RegisterServiceImpl) DiscoveryService(ctx context.Context, serviceName string) ([]*api.AgentService, error)  {
 
- 	instances, err := service.discoveryClient.DiscoverServices(ctx, serviceName)
 
- 	if err != nil {
 
- 		log.Printf("get service info err: %s", err)
 
- 	}
 
- 	if instances == nil || len(instances) == 0 {
 
- 		return nil, ErrNotServiceInstances
 
- 	}
 
- 	return instances, nil
 
- }
 
- func (*RegisterServiceImpl) HealthCheck() string {
 
- 	return "OK"
 
- }
 
 
  |