Exemple #1
0
        public void TestApollo()
        {
            var provider = new HttpConfigProvider
            {
                Server = _server,
                AppId  = "testapi"
            };

            provider.SetApollo("application");
            //provider.LoadAll();

            var url = provider["appapiurl"];

            Assert.NotEmpty(url);

            var keys = provider.Keys.ToArray();

            Assert.NotNull(keys);

            var model = provider.Load <Model>();

            Assert.NotNull(model);
            Assert.NotEmpty(model.AppApiUrl);
            Assert.Equal(url, model.AppApiUrl);
            Assert.True(model.Radius > 0);
            Assert.NotEmpty(model.MySqlServer);

            var model2 = new Model();

            provider.Bind(model2);
            Assert.Equal(url, model2.AppApiUrl);
            Assert.True(model2.Radius > 0);
            Assert.NotEmpty(model2.MySqlServer);
        }
Exemple #2
0
        private void DoApolloSync(Object state)
        {
            foreach (var item in AppConfig.FindAllWithCache())
            {
                if (!item.Enable || !item.EnableApollo || item.ApolloMetaServer.IsNullOrEmpty())
                {
                    continue;
                }

                var httpConfig = new HttpConfigProvider
                {
                    Server = item.ApolloMetaServer,
                    AppId  = item.ApolloAppId
                };

                var nameSpace = item.ApolloNameSpace;
                if (nameSpace.IsNullOrEmpty())
                {
                    nameSpace = "application";
                }
                httpConfig.SetApollo(nameSpace);

                try
                {
                    // 一次性加载所有配置
                    httpConfig.LoadAll();

                    if (httpConfig.Keys.Count > 0)
                    {
                        // 配置匹配到本地
                        var ds = ConfigData.FindAllByApp(item.Id);
                        foreach (var elm in httpConfig.Keys)
                        {
                            var cfg = ds.FirstOrDefault(e => e.Key.EqualIgnoreCase(elm));
                            if (cfg == null)
                            {
                                cfg = new ConfigData {
                                    AppId = item.Id, Key = elm, Enable = true,
                                };
                                ds.Add(cfg);
                            }

                            cfg.Value = httpConfig[elm];

                            if (cfg is IEntity entity && entity.HasDirty)
                            {
                                cfg.Version = item.AcquireNewVersion();
                            }
                        }
                        ds.Save(true);
                    }
                }
                catch (Exception ex)
                {
                    XTrace.WriteException(ex);
                }
            }
        }