forked from apache/dubbo-go
/
registry.go
100 lines (80 loc) · 2.78 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package registry
import (
"fmt"
"time"
)
import (
"github.com/AlexStocks/goext/net"
)
//////////////////////////////////////////////
// Registry Interface
//////////////////////////////////////////////
// for service discovery/registry
type Registry interface {
Register(conf interface{}) error
Close()
}
//////////////////////////////////////////////
// application config
//////////////////////////////////////////////
type ApplicationConfig struct {
Organization string `yaml:"organization" json:"organization,omitempty"`
Name string `yaml:"name" json:"name,omitempty"`
Module string `yaml:"module" json:"module,omitempty"`
Version string `yaml:"version" json:"version,omitempty"`
Owner string `yaml:"owner" json:"owner,omitempty"`
Environment string `yaml:"environment" json:"environment,omitempty"`
}
func (c *ApplicationConfig) ToString() string {
return fmt.Sprintf("ApplicationConfig is {name:%s, version:%s, owner:%s, module:%s, organization:%s}",
c.Name, c.Version, c.Owner, c.Module, c.Organization)
}
type RegistryConfig struct {
Address []string `required:"true" yaml:"address" json:"address,omitempty"`
UserName string `yaml:"user_name" json:"user_name,omitempty"`
Password string `yaml:"password" json:"password,omitempty"`
TimeoutStr string `yaml:"timeout" default:"5s" json:"timeout,omitempty"` // unit: second
Timeout time.Duration `yaml:"-" json:"-"`
}
//////////////////////////////////////////////
// service config
//////////////////////////////////////////////
type ServiceConfigIf interface {
String() string
ServiceEqual(url *ServiceURL) bool
}
type ServiceConfig struct {
Protocol string `required:"true",default:"dubbo" yaml:"protocol" json:"protocol,omitempty"`
Service string `required:"true" yaml:"service" json:"service,omitempty"`
Group string `yaml:"group" json:"group,omitempty"`
Version string `yaml:"version" json:"version,omitempty"`
}
func (c ServiceConfig) Key() string {
return fmt.Sprintf("%s@%s", c.Service, c.Protocol)
}
func (c ServiceConfig) String() string {
return fmt.Sprintf("%s@%s-%s-%s", c.Service, c.Protocol, c.Group, c.Version)
}
func (c ServiceConfig) ServiceEqual(url *ServiceURL) bool {
if c.Protocol != url.Protocol {
return false
}
if c.Service != url.Query.Get("interface") {
return false
}
if c.Group != url.Group {
return false
}
if c.Version != url.Version {
return false
}
return true
}
type ServerConfig struct {
Protocol string `required:"true",default:"dubbo" yaml:"protocol" json:"protocol,omitempty"` // codec string, jsonrpc etc
IP string `yaml:"ip" json:"ip,omitempty"`
Port int `required:"true" yaml:"port" json:"port,omitempty"`
}
func (c *ServerConfig) Address() string {
return gxnet.HostAddress(c.IP, c.Port)
}