|
| 1 | +package goconsul |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/gogf/gf/text/gstr" |
| 7 | + "github.com/hashicorp/consul/api" |
| 8 | +) |
| 9 | + |
| 10 | +type ServiceCheck struct { |
| 11 | + api.AgentServiceCheck |
| 12 | +} |
| 13 | + |
| 14 | +type IServiceInstance interface { |
| 15 | + GetName() string |
| 16 | + GetHost() string |
| 17 | + GetPort() int |
| 18 | + GetMetadata() map[string]string |
| 19 | + GetId() string |
| 20 | + GetTags() []string |
| 21 | + GetLanIps() []string |
| 22 | + GetAnnouncedIps() []string |
| 23 | + GetServiceCheck() *ServiceCheck |
| 24 | +} |
| 25 | + |
| 26 | +func (c *Client) Register(instance *ServiceInstance) error { |
| 27 | + |
| 28 | + // 创建注册到consul的服务到 |
| 29 | + registration := new(api.AgentServiceRegistration) |
| 30 | + registration.ID = instance.GetId() |
| 31 | + registration.Name = instance.GetName() |
| 32 | + registration.Port = instance.GetPort() |
| 33 | + registration.Tags = instance.GetTags() |
| 34 | + registration.Meta = instance.GetMetadata() |
| 35 | + registration.Address = instance.GetHost() |
| 36 | + registration.TaggedAddresses = make(map[string]api.ServiceAddress) |
| 37 | + for k, v := range instance.GetLanIps() { |
| 38 | + registration.TaggedAddresses[fmt.Sprintf("Lan%v", k)] = api.ServiceAddress{ |
| 39 | + Address: v, |
| 40 | + Port: instance.GetPort(), |
| 41 | + } |
| 42 | + } |
| 43 | + for k, v := range instance.GetAnnouncedIps() { |
| 44 | + registration.TaggedAddresses[fmt.Sprintf("Announced%v", k)] = api.ServiceAddress{ |
| 45 | + Address: v, |
| 46 | + Port: instance.GetPort(), |
| 47 | + } |
| 48 | + } |
| 49 | + registration.Check = &instance.GetServiceCheck().AgentServiceCheck |
| 50 | + |
| 51 | + // 注册服务到consul |
| 52 | + err := c.Agent().ServiceRegister(registration) |
| 53 | + if err != nil { |
| 54 | + fmt.Println(err) |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + c.insMap.Store(instance.GetId(), instance) |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Client) DeregisterAll() { |
| 64 | + |
| 65 | + c.insMap.Range(func(key, value interface{}) bool { |
| 66 | + c.Agent().ServiceDeregister(key.(string)) |
| 67 | + c.insMap.Delete(key) |
| 68 | + return true |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func (c *Client) Deregister(id string) { |
| 73 | + c.Agent().ServiceDeregister(id) |
| 74 | + c.insMap.Delete(id) |
| 75 | +} |
| 76 | + |
| 77 | +func (c *Client) DiscoverCatalogInstancesWithTags(serviceName string, tags []string) ([]*ServiceInstance, error) { |
| 78 | + |
| 79 | + catalogService, _, err := c.Catalog().ServiceMultipleTags(serviceName, tags, nil) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + if len(catalogService) == 0 { |
| 85 | + return nil, fmt.Errorf("not found") |
| 86 | + } |
| 87 | + |
| 88 | + result := make([]*ServiceInstance, len(catalogService)) |
| 89 | + for index, server := range catalogService { |
| 90 | + announcedIps := make([]string, 0) |
| 91 | + announcedIpStr, found := server.ServiceMeta["AnnouncedIp"] |
| 92 | + if found && announcedIpStr != "" { |
| 93 | + announcedIps = gstr.Split(announcedIpStr, ",") |
| 94 | + } |
| 95 | + |
| 96 | + lanIps := make([]string, 0) |
| 97 | + lanIpStr, found := server.ServiceMeta["LanIp"] |
| 98 | + if found && lanIpStr != "" { |
| 99 | + lanIps = gstr.Split(lanIpStr, ",") |
| 100 | + } |
| 101 | + |
| 102 | + dataRoot, found := server.ServiceMeta["DataRoot"] |
| 103 | + if !found { |
| 104 | + dataRoot = "" |
| 105 | + } |
| 106 | + |
| 107 | + s := NewInstance( |
| 108 | + server.ServiceName, |
| 109 | + server.Address, |
| 110 | + server.ServicePort, |
| 111 | + server.ServiceMeta, |
| 112 | + server.ServiceTags, |
| 113 | + server.ServiceID, |
| 114 | + lanIps, |
| 115 | + announcedIps, |
| 116 | + c, |
| 117 | + dataRoot, |
| 118 | + ) |
| 119 | + |
| 120 | + result[index] = s |
| 121 | + } |
| 122 | + |
| 123 | + return result, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (c *Client) DiscoverCatalogInstancesWithName(serviceName string) ([]*ServiceInstance, error) { |
| 127 | + return c.DiscoverCatalogInstancesWithTags(serviceName, nil) |
| 128 | +} |
| 129 | + |
| 130 | +func (c *Client) DiscoverCatalogInstanceWithId(name string, id string) (*ServiceInstance, error) { |
| 131 | + tags := []string{ |
| 132 | + fmt.Sprintf("serviceName:%v", name), |
| 133 | + fmt.Sprintf("instanceId:%v", id), |
| 134 | + } |
| 135 | + |
| 136 | + ins, err := c.DiscoverCatalogInstancesWithTags(name, tags) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + if len(ins) == 0 { |
| 142 | + return nil, fmt.Errorf("not found") |
| 143 | + } |
| 144 | + |
| 145 | + return ins[0], nil |
| 146 | +} |
| 147 | + |
| 148 | +func (c *Client) DiscoverHealthInstancesWithTags(name string, tags []string, passing bool) ([]*ServiceInstance, error) { |
| 149 | + |
| 150 | + servers, _, err := c.Health().ServiceMultipleTags(name, tags, passing, nil) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + if len(servers) == 0 { |
| 156 | + return nil, fmt.Errorf("not found") |
| 157 | + } |
| 158 | + |
| 159 | + result := make([]*ServiceInstance, len(servers)) |
| 160 | + for index, server := range servers { |
| 161 | + announcedIps := make([]string, 0) |
| 162 | + announcedIpStr, found := server.Service.Meta["AnnouncedIp"] |
| 163 | + if found && announcedIpStr != "" { |
| 164 | + announcedIps = gstr.Split(announcedIpStr, ",") |
| 165 | + } |
| 166 | + |
| 167 | + lanIps := make([]string, 0) |
| 168 | + lanIpStr, found := server.Service.Meta["LanIp"] |
| 169 | + if found && lanIpStr != "" { |
| 170 | + lanIps = gstr.Split(lanIpStr, ",") |
| 171 | + } |
| 172 | + |
| 173 | + dataRoot, found := server.Service.Meta["DataRoot"] |
| 174 | + if !found { |
| 175 | + dataRoot = "" |
| 176 | + } |
| 177 | + |
| 178 | + s := NewInstance( |
| 179 | + server.Service.Service, |
| 180 | + server.Service.Address, |
| 181 | + server.Service.Port, |
| 182 | + server.Service.Meta, |
| 183 | + server.Service.Tags, |
| 184 | + server.Service.ID, |
| 185 | + lanIps, |
| 186 | + announcedIps, |
| 187 | + c, |
| 188 | + dataRoot, |
| 189 | + ) |
| 190 | + |
| 191 | + result[index] = s |
| 192 | + } |
| 193 | + |
| 194 | + return result, nil |
| 195 | +} |
| 196 | + |
| 197 | +func (c *Client) DiscoverHealthInstanceWithId(name string, id string, passing bool) (*ServiceInstance, error) { |
| 198 | + tags := []string{ |
| 199 | + fmt.Sprintf("instanceId:%v", id), |
| 200 | + } |
| 201 | + |
| 202 | + ins, err := c.DiscoverHealthInstancesWithTags(name, tags, passing) |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + |
| 207 | + if len(ins) == 0 { |
| 208 | + return nil, fmt.Errorf("not found") |
| 209 | + } |
| 210 | + |
| 211 | + return ins[0], nil |
| 212 | +} |
| 213 | + |
| 214 | +func (c *Client) DiscoverInstancesWithName(name string, tags []string, passing int) ([]*ServiceInstance, error) { |
| 215 | + if passing == -1 { |
| 216 | + return c.DiscoverCatalogInstancesWithTags(name, tags) |
| 217 | + } else { |
| 218 | + return c.DiscoverHealthInstancesWithTags(name, tags, passing == 1) |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +func (c *Client) DiscoverInstanceWithId(name string, id string, passing int) (*ServiceInstance, error) { |
| 223 | + if passing == -1 { |
| 224 | + return c.DiscoverCatalogInstanceWithId(name, id) |
| 225 | + } else { |
| 226 | + return c.DiscoverHealthInstanceWithId(name, id, passing == 1) |
| 227 | + } |
| 228 | +} |
0 commit comments