sequence
In this paper, we mainly study the Domain of nacOS-coreDNS-plugin
Domain
nacos-coredns-plugin/nacos/nacos_domain.go
type Domain struct { Name string `json:"dom"` Clusters string CacheMillis int64 LastRefMillis int64 Instances []Instance `json:"hosts"` Env string TTL int } func (domain Domain) getInstances() ([]Instance) { return domain.Instances } func (domain Domain) String() string { b, _ := json.Marshal(domain) return string(b) } func (domain Domain) SrvInstances() []Instance { var result = make([]Instance, 0) hosts := domain.getInstances() for _, host := range hosts { if host.Valid && host.Weight > 0 { for i := 0; i < int(math.Ceil(host.Weight)); i++ { result = append(result, host) } } } if len(result) <= 0{ panic("no host to srv: " + domain.Name) } return result }Copy the code
Domain defines the properties of Name, Clusters, CacheMillis, LastRefMillis, Instances, Env, and TTL. It provides getInstances, String, SrvInstances methods; The SrvInstances method returns the corresponding number of instances according to the weight of instance
The instance
nacos-coredns-plugin/nacos/nacos_domain_test.go
func TestDomain_SrvInstances(t *testing.T) { domain := Domain{} domain.CacheMillis = 10000 domain.Clusters = "DEFAULT" Instances = []Instance{Instance{IP: "2.2.2.2", Port: 80, weight: 2, AppUseType: "publish", Valid: true, Site: "et2"}} instances := domain.SrvInstances() if len(instances) == 2 { t.Log("Domain.srvInstances weight passed.") } //test valid defer func() { if err := recover(); err ! = nil { if strings.HasPrefix(err.(string), "no host to srv: ") {t.log (" domain.srvinstances valid passed.")}}}() domain.instances = []Instance{Instance{IP: "2.2.2.2", Port: 80, Weight: 2, AppUseType: "publish", Valid: false, Site: "et2"}} domain.SrvInstances() }Copy the code
Set instances to an instance with a weight of 2, and return two instances using the SrvInstances method
summary
The Domain of nacos-CoreDNS-Plugin defines the properties of Name, Clusters, CacheMillis, LastRefMillis, Instances, Env, and TTL. It provides the getInstances, String, SrvInstances methods.
doc
- nacos-coredns-plugin