Esempio n. 1
0
func newNginxConsulDriver(cluster *dcluster.Cluster) (*discovery.ServiceRegisterDriver, error) {
	driverConfig, _ := discovery.GetServiceRegistryDescription(NGINX_CONSUL_DRIVER_NAME, cluster)
	driverName, ok := driverConfig["driver"]
	if !ok || driverName != NGINX_CONSUL_DRIVER_NAME {
		return nil, errors.New(fmt.Sprintf("Driver name '%s' is expected, but get '%s'", NGINX_CONSUL_DRIVER_NAME, driverName))
	}
	us, ok := driverConfig["upstream"]
	if !ok {
		return nil, errors.New("Nginx driver option missed: 'upstream'")
	}

	transport := &dutils.Transport{
		ConnectTimeout:        3 * time.Second,
		RequestTimeout:        10 * time.Second,
		ResponseHeaderTimeout: 5 * time.Second,
	}
	httpClient := &http.Client{Transport: transport}

	consulAddress := cluster.ConsulCluster.Server.IPs[0]
	config := &consul.Config{
		Address:    fmt.Sprintf("%s:8500", consulAddress),
		Scheme:     "http",
		HttpClient: httpClient,
	}

	client, err := consul.NewClient(config)
	if err != nil {
		return nil, errors.New("Fail to build a consul client! ")
	}

	d := NginxConsulRegisterDriver{
		upstream:      us,
		client:        client,
		consulAddress: consulAddress,
	}
	var pi discovery.ServiceRegisterDriver = &d
	return &pi, nil
}
Esempio n. 2
0
func newNginxDriver(cluster *dcluster.Cluster) (*discovery.ServiceRegisterDriver, error) {
	driverConfig, _ := discovery.GetServiceRegistryDescription(NGINX_DRIVER_NAME, cluster)
	us, ok := driverConfig["upstream"]
	if !ok {
		return nil, errors.New("Nginx driver option missed: 'upstream'")
	}
	driverName, ok := driverConfig["driver"]
	if !ok || driverName != NGINX_DRIVER_NAME {
		return nil, errors.New(fmt.Sprintf("Driver name '%s' is expected, but get '%s'", NGINX_DRIVER_NAME, driverName))
	}

	transport := &dutils.Transport{
		ConnectTimeout:        3 * time.Second,
		RequestTimeout:        10 * time.Second,
		ResponseHeaderTimeout: 5 * time.Second,
	}
	d := NginxServiceRegisterDriver{
		upstream:   us,
		httpClient: &http.Client{Transport: transport},
	}
	var pi discovery.ServiceRegisterDriver = &d
	return &pi, nil
}
Esempio n. 3
0
func newHaproxyConsulRegisterDriver(cluster *dcluster.Cluster) (*discovery.ServiceRegisterDriver, error) {
	driverConfig, _ := discovery.GetServiceRegistryDescription(HAPROXY_CONSUL_DRIVER_NAME, cluster)
	driverName, ok := driverConfig["driver"]
	if !ok || driverName != HAPROXY_CONSUL_DRIVER_NAME {
		return nil, errors.New(fmt.Sprintf("Driver name '%s' is expected, but get '%s'", HAPROXY_CONSUL_DRIVER_NAME, driverName))
	}
	service, ok := driverConfig["service"]
	if !ok {
		return nil, errors.New("Haproxy driver option missed: 'upstream'")
	}

	config := consul.DefaultConfig()
	consulAddress := fmt.Sprintf("%s:8500", cluster.ConsulCluster.Server.IPs[0])
	config.Address = consulAddress
	client, err := consul.NewClient(config)
	if err != nil {
		return nil, err
	}

	d := HaproxyConsulRegisterDriver{serviceName: service, catalog: client.Catalog()}
	var pi discovery.ServiceRegisterDriver = &d
	return &pi, nil
}