|
| 1 | +package apollo |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/shima-park/agollo" |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | + "github.com/tietang/go-utils" |
| 9 | + "github.com/tietang/props/ini" |
| 10 | + "github.com/tietang/props/kvs" |
| 11 | + "github.com/tietang/props/yam" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +//通过key/value来组织,过滤root prefix后,替换/为.作为properties key |
| 19 | +type ApolloConfigSource struct { |
| 20 | + kvs.MapProperties |
| 21 | + Apollo agollo.Agollo |
| 22 | + name string |
| 23 | + address string |
| 24 | + appId string |
| 25 | + namespaces []string |
| 26 | + cluster string |
| 27 | + clientIP string |
| 28 | + contentType kvs.ContentType |
| 29 | +} |
| 30 | + |
| 31 | +func NewApolloConfigSource(address, appId string, namespaces []string) *ApolloConfigSource { |
| 32 | + s := &ApolloConfigSource{} |
| 33 | + s.name = "apollo:" + address |
| 34 | + s.appId = appId |
| 35 | + s.address = address |
| 36 | + s.namespaces = namespaces |
| 37 | + s.contentType = kvs.KeyValueContentType |
| 38 | + s.clientIP, _ = utils.GetExternalIP() |
| 39 | + s.cluster = "default" |
| 40 | + s.Values = make(map[string]string) |
| 41 | + s.init() |
| 42 | + |
| 43 | + return s |
| 44 | +} |
| 45 | + |
| 46 | +func NewApolloCompositeConfigSource(url, appId string, namespaces []string) *kvs.CompositeConfigSource { |
| 47 | + s := kvs.NewEmptyNoSystemEnvCompositeConfigSource() |
| 48 | + s.ConfName = "ApolloKevValue" |
| 49 | + c := NewApolloConfigSource(url, appId, namespaces) |
| 50 | + s.Add(c) |
| 51 | + return s |
| 52 | +} |
| 53 | +func (s *ApolloConfigSource) init() { |
| 54 | + for _, ns := range s.namespaces { |
| 55 | + kvs, err := s.GetConfigsFromCache(s.address, s.appId, s.cluster, ns) |
| 56 | + if err != nil { |
| 57 | + log.Error(err) |
| 58 | + continue |
| 59 | + } |
| 60 | + for key, value := range kvs { |
| 61 | + s.initValue(ns, key, value) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (s *ApolloConfigSource) initValue(namespace, key, value string) { |
| 67 | + contentType := s.getContentType(namespace) |
| 68 | + if contentType == kvs.KeyValueContentType { |
| 69 | + contentType = s.getContentType(key) |
| 70 | + } |
| 71 | + if contentType == kvs.KeyValueContentType { |
| 72 | + s.Set(key, value) |
| 73 | + } else if contentType == kvs.ContentProps || contentType == kvs.ContentProperties { |
| 74 | + s.findProperties(value) |
| 75 | + } else if contentType == kvs.ContentIni { |
| 76 | + s.findIni(value) |
| 77 | + } else if contentType == kvs.ContentYaml || contentType == kvs.ContentYam || contentType == kvs.ContentYml { |
| 78 | + s.findYaml(value) |
| 79 | + } else { |
| 80 | + s.Set(key, value) |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +func (s *ApolloConfigSource) getContentType(key string) (ctype kvs.ContentType) { |
| 86 | + idx := strings.LastIndex(key, ".") |
| 87 | + if idx == -1 || idx == len(key)-1 { |
| 88 | + ctype = kvs.KeyValueContentType |
| 89 | + } else { |
| 90 | + ctype = kvs.ContentType(key[idx+1:]) |
| 91 | + } |
| 92 | + return |
| 93 | +} |
| 94 | + |
| 95 | +func (s *ApolloConfigSource) watchContext() { |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +func (s *ApolloConfigSource) Close() { |
| 100 | +} |
| 101 | + |
| 102 | +func (s *ApolloConfigSource) findYaml(content string) { |
| 103 | + props := yam.ByYaml(content) |
| 104 | + s.SetAll(props.Values) |
| 105 | +} |
| 106 | + |
| 107 | +func (s *ApolloConfigSource) findIni(content string) { |
| 108 | + props := ini.ByIni(content) |
| 109 | + s.SetAll(props.Values) |
| 110 | +} |
| 111 | + |
| 112 | +func (s *ApolloConfigSource) findProperties(content string) { |
| 113 | + props := kvs.ByProperties(content) |
| 114 | + s.SetAll(props.Values) |
| 115 | +} |
| 116 | + |
| 117 | +func (s *ApolloConfigSource) registerProps(key, value string) { |
| 118 | + s.Set(strings.TrimSpace(key), strings.TrimSpace(value)) |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +func (s *ApolloConfigSource) Name() string { |
| 123 | + return s.name |
| 124 | +} |
| 125 | + |
| 126 | +func (c *ApolloConfigSource) GetConfigsFromCache(address, appID, cluster, namespace string) (kvs KeyValue, err error) { |
| 127 | + url := fmt.Sprintf("http://%s/configfiles/json/%s/%s/%s?ip=%s", |
| 128 | + address, |
| 129 | + url.QueryEscape(appID), |
| 130 | + url.QueryEscape(cluster), |
| 131 | + url.QueryEscape(namespace), |
| 132 | + c.clientIP, |
| 133 | + ) |
| 134 | + |
| 135 | + //调用请求 |
| 136 | + res, err := http.Get(url) |
| 137 | + |
| 138 | + if err != nil { |
| 139 | + log.Error(err) |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + // 如果出错就不需要close,因此defer语句放在err处理逻辑后面 |
| 143 | + defer res.Body.Close() |
| 144 | + //处理response,读取Response body |
| 145 | + respBody, err := ioutil.ReadAll(res.Body) |
| 146 | + |
| 147 | + // |
| 148 | + if err := res.Body.Close(); err != nil { |
| 149 | + log.Error(err) |
| 150 | + } |
| 151 | + kvs = KeyValue{} |
| 152 | + err = json.Unmarshal(respBody, &kvs) |
| 153 | + if err != nil { |
| 154 | + log.Error(err) |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + return kvs, err |
| 158 | + |
| 159 | +} |
| 160 | + |
| 161 | +type KeyValue map[string]string |
| 162 | + |
| 163 | +type ConfigRes struct { |
| 164 | + AppID string `json:"appId"` // appId: "AppTest", |
| 165 | + Cluster string `json:"cluster"` // cluster: "default", |
| 166 | + Namespace string `json:"namespaceName"` // namespaceName: "TEST.Namespace1", |
| 167 | + KeyValue KeyValue `json:"configurations"` // configurations: {Name: "Foo"}, |
| 168 | + ReleaseKey string `json:"releaseKey"` // releaseKey: "20181017110222-5ce3b2da895720e8" |
| 169 | +} |
0 commit comments