|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package agollo |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/zouyx/agollo/v4/agcache/memory" |
| 22 | + "github.com/zouyx/agollo/v4/cluster/roundrobin" |
| 23 | + "github.com/zouyx/agollo/v4/component" |
| 24 | + "github.com/zouyx/agollo/v4/component/log" |
| 25 | + "github.com/zouyx/agollo/v4/component/notify" |
| 26 | + "github.com/zouyx/agollo/v4/component/serverlist" |
| 27 | + "github.com/zouyx/agollo/v4/env" |
| 28 | + "github.com/zouyx/agollo/v4/env/config" |
| 29 | + jsonFile "github.com/zouyx/agollo/v4/env/file/json" |
| 30 | + "github.com/zouyx/agollo/v4/extension" |
| 31 | + "github.com/zouyx/agollo/v4/protocol/auth/sign" |
| 32 | + "github.com/zouyx/agollo/v4/storage" |
| 33 | +) |
| 34 | + |
| 35 | +type Client struct { |
| 36 | + initAppConfigFunc func() (*config.AppConfig, error) |
| 37 | +} |
| 38 | + |
| 39 | +func Create() *Client { |
| 40 | + extension.SetCacheFactory(&memory.DefaultCacheFactory{}) |
| 41 | + extension.SetLoadBalance(&roundrobin.RoundRobin{}) |
| 42 | + extension.SetFileHandler(&jsonFile.FileHandler{}) |
| 43 | + extension.SetHTTPAuth(&sign.AuthSignature{}) |
| 44 | + storage.InitConfigCache() |
| 45 | + return &Client{} |
| 46 | +} |
| 47 | + |
| 48 | +func (c *Client) Start() error { |
| 49 | + return c.StartWithConfig(nil) |
| 50 | +} |
| 51 | + |
| 52 | +func (c *Client) StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) error { |
| 53 | + // 有了配置之后才能进行初始化 |
| 54 | + if err := env.InitConfig(loadAppConfig); err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + notify.InitAllNotifications(nil) |
| 59 | + serverlist.InitSyncServerIPList() |
| 60 | + |
| 61 | + //first sync |
| 62 | + if err := notify.SyncConfigs(); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + log.Debug("init notifySyncConfigServices finished") |
| 66 | + |
| 67 | + //start long poll sync config |
| 68 | + go component.StartRefreshConfig(¬ify.ConfigComponent{}) |
| 69 | + |
| 70 | + log.Info("agollo start finished ! ") |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
0 commit comments