/
endpoint.go
executable file
·50 lines (43 loc) · 1.42 KB
/
endpoint.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
package endpoint
import (
"errors"
"fmt"
"strings"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/core/registry"
"github.com/go-chassis/go-chassis/pkg/runtime"
"github.com/go-chassis/go-chassis/pkg/util/tags"
)
// GetEndpointFromServiceCenter is used to get the endpoint based on appID, microservice and version
func GetEndpointFromServiceCenter(appID, microService, version string) (string, error) {
var endPoint string
tags := utiltags.NewDefaultTag(version, appID)
instances, err := registry.DefaultServiceDiscoveryService.FindMicroServiceInstances(runtime.ServiceID, microService, tags)
if err != nil {
lager.Logger.Warnf("Get service instance failed, for key: %s:%s:%s",
appID, microService, version)
return "", err
}
if len(instances) == 0 {
lager.Logger.Errorf(nil, "No available instance, key: %s:%s:%s",
appID, microService, version)
instanceError := fmt.Sprintf("No available instance, key: %s:%s:%s",
appID, microService, version)
return "", errors.New(instanceError)
}
for _, instance := range instances {
for _, value := range instance.EndpointsMap {
if strings.Contains(value, "?") {
separation := strings.Split(value, "?")
if separation[1] == "sslEnabled=true" {
endPoint = "https://" + separation[0]
} else {
endPoint = "http://" + separation[0]
}
} else {
endPoint = "http://" + value
}
}
}
return endPoint, nil
}