Ejemplo n.º 1
0
func (r *ConsulAdapter) Register(service *bridge.Service) error {
	registration := new(consulapi.AgentServiceRegistration)
	registration.ID = service.ID
	registration.Name = service.Name
	registration.Port = service.Port
	registration.Tags = service.Tags
	registration.Address = service.IP
	registration.Check = r.buildCheck(service)
	if r.servicePrefix != "" {
		kv := r.client.KV()
		addr := service.IP + ":" + strconv.Itoa(service.Port)
		host_path := r.servicePrefix + "/" + "nodes/" + service.ID + "/"
		service_path := r.servicePrefix + "/" + "services/" + service.Name + "/" + service.ID + "/"
		_, err := kv.Put(&consulapi.KVPair{Key: host_path + "URI", Value: []byte(addr)}, nil)
		if err != nil {
			return (err)
		}
		_, err = kv.Put(&consulapi.KVPair{Key: service_path + "URI", Value: []byte(addr)}, nil)
		if err != nil {
			return (err)
		}
		for k, v := range service.Attrs {
			_, err := kv.Put(&consulapi.KVPair{Key: host_path + k, Value: []byte(v)}, nil)
			if err != nil {
				return (err)
			}
			_, err = kv.Put(&consulapi.KVPair{Key: service_path + k, Value: []byte(v)}, nil)
			if err != nil {
				return (err)
			}
		}
	}
	return r.client.Agent().ServiceRegister(registration)
}
Ejemplo n.º 2
0
// Register will register ConsulAdapter's interface with RegistryAdapter
func (r *ConsulAdapter) Register(service *bridge.Service) error {
	registration := new(consulapi.AgentServiceRegistration)
	registration.ID = service.ID
	registration.Name = service.Name
	registration.Port = service.Port
	registration.Tags = service.Tags
	registration.Address = service.IP
	registration.Check = r.buildCheck(service)
	return r.client.Agent().ServiceRegister(registration)
}
Ejemplo n.º 3
0
func (r *ConsulAdapter) Register(service *bridge.Service) error {
	registration := new(consulapi.AgentServiceRegistration)
	registration.ID = service.ID
	registration.Name = service.Name
	registration.Port = service.Port
	registration.Tags = service.Tags
	registration.Address = service.IP
	registration.Check = r.buildCheck(service)
	if r.servicePrefix != "" {
		kv := r.client.KV()
		for k, v := range service.Attrs {
			pair := &consulapi.KVPair{Key: r.servicePrefix + "/" + service.ID + "/" + k, Value: []byte(v)}
			_, err := kv.Put(pair, nil)
			if err != nil {
				panic(err)
			}
		}
	}
	return r.client.Agent().ServiceRegister(registration)
}
			Expect(err).NotTo(HaveOccurred())
			service, ok := services[registration.ID]
			Expect(ok).To(BeTrue())
			Expect(*service).To(Equal(api.AgentService{
				ID:      registration.ID,
				Service: registration.Name,
				Tags:    registration.Tags,
				Port:    registration.Port,
				Address: registration.Address,
			}))
		})

		Context("when using a TTL check", func() {
			BeforeEach(func() {
				registration.Check = &api.AgentServiceCheck{
					TTL: "10s",
				}
			})

			It("registers the service", func() {
				services, err := consulClient.Agent().Services()
				Expect(err).NotTo(HaveOccurred())
				service, ok := services[registration.ID]
				Expect(ok).To(BeTrue())
				Expect(*service).To(Equal(api.AgentService{
					ID:      registration.ID,
					Service: registration.Name,
					Tags:    registration.Tags,
					Port:    registration.Port,
					Address: registration.Address,
				}))