12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package service
- import (
- "context"
- "errors"
- "github.com/longjoy/micro-go-course/section13/register/discovery"
- "log"
- )
- type Service interface {
- HealthCheck() string
- DiscoveryService(ctx context.Context, serviceName string) ([]*discovery.InstanceInfo, 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) ([]*discovery.InstanceInfo, 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"
- }
|