git.zd.zone/hrpc/hrpc@v0.0.12/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"git.zd.zone/hrpc/hrpc/option"
     7  	"git.zd.zone/hrpc/hrpc/server"
     8  	"github.com/hashicorp/consul/api"
     9  )
    10  
    11  // Service represents a service
    12  type Service struct {
    13  	ID   string
    14  	Name string
    15  	// Endpoint represents the IP address of this program is running
    16  	Endpoint string
    17  	Weight   int
    18  	Port     int
    19  }
    20  
    21  // String to string for connection with POD IP:Port
    22  func (s Service) String() string {
    23  	return fmt.Sprintf("%s:%d", s.Endpoint, s.Port)
    24  }
    25  
    26  // Target returns a string that should be used in TKE environment only
    27  // since we can use DNS feature to find out the service IP to viste in case of the changes of POD IP
    28  // !!! The namespace in QCloud must be prod (stands for production) OR dev (stands for development).
    29  // Otherwise, the request can be failed.
    30  func (s Service) Target() string {
    31  	var namespace = "prod"
    32  	if server.Environment() == option.Development {
    33  		namespace = "dev"
    34  	}
    35  	return fmt.Sprintf("%s.%s.svc.cluster.local:%d", s.Name, namespace, s.Port)
    36  }
    37  
    38  // Get will return the best service based on the name and tag
    39  // If the tag provides more than one, it will only pick the first one
    40  // If the tag does not provide, the service will be picked based on the current environment(prefix of configs)
    41  func Get(name string, tags ...Tag) (*Service, error) {
    42  	// var tag Tag
    43  	// if len(tags) != 0 {
    44  	// 	tag = tags[0]
    45  	// } else {
    46  	// 	tag = Tag(configs.Get().Prefix())
    47  	// }
    48  	// var ss []*api.ServiceEntry
    49  	// ss, _, err := configs.Client().Health().Service(name, tag.String(), true, nil)
    50  	// if err != nil {
    51  	// 	return nil, err
    52  	// }
    53  	// if len(ss) == 0 {
    54  	// 	return nil, fmt.Errorf("[%s] not found or no permission to read", name)
    55  	// }
    56  	var ss []*api.ServiceEntry
    57  	ss = append(ss, &api.ServiceEntry{
    58  		Service: &api.AgentService{
    59  			Port:    8888,
    60  			Service: name,
    61  			ID:      "1000000",
    62  			Address: name + ":8888",
    63  			Weights: api.AgentWeights{
    64  				Passing: 10,
    65  			},
    66  		},
    67  	})
    68  
    69  	var maxWeight int
    70  	var index int
    71  	for i, s := range ss {
    72  		if s.Service.Weights.Passing > maxWeight {
    73  			maxWeight = s.Service.Weights.Passing
    74  			index = i
    75  		}
    76  	}
    77  	return &Service{
    78  		ID:       ss[index].Service.ID,
    79  		Name:     ss[index].Service.Service,
    80  		Endpoint: ss[index].Service.Address,
    81  		Weight:   maxWeight,
    82  		Port:     ss[index].Service.Port,
    83  	}, nil
    84  }