• .net framework 使用Apollo 配置中心


    参照了:https://www.cnblogs.com/xichji/p/11324893.html

    Apollo默认有一个“SampleApp”应用,“DEV”环境 和 “timeout” KEY。

    环境:VS2015,.net framework 4.5。

    1.nuget 下载 :Com.Ctrip.Framework.Apollo.ConfigurationManager   2.3.0。最新的2.6.2 我没安装成功。

    2.修改web.config 增加:

    <add key="Apollo.AppID" value="SampleApp" />
    <add key="Apollo.Env" value="DEV" />
    <add key="Apollo.MetaServer" value="http://192.168.192.100:8080" />

    3.新建IApolloConfiguration

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MyCompany.MyProject.Infrastructure
    {
        /// <summary>
        /// 配置抽象接口。
        /// </summary>
        public interface IApolloConfiguration
        {
            /// <summary>
            /// 配置更改回调事件。
            /// </summary>
            //event EventHandler<ConfigurationChangeEventArgs> ConfigChanged;
    
            /// <summary>
            /// 获取配置项。
            /// </summary>
            /// <param name="key"></param>
            /// <param name="namespaces">命名空间集合</param>
            /// <returns></returns>
            string GetValue(string key, params string[] namespaces);
    
            /// <summary>
            /// 获取配置项。
            /// </summary>
            /// <typeparam name="TValue">值类型</typeparam>
            /// <param name="key"></param>
            /// <param name="namespaces">命名空间集合</param>
            /// <returns></returns>
            TValue GetValue<TValue>(string key, params string[] namespaces);
    
            /// <summary>
            /// 获取配置项,如果值为 <see cref="null"/> 则取参数 <see cref="defaultValue"/> 值。
            /// </summary>
            /// <param name="key"></param>
            /// <param name="defaultValue">默认值</param>
            /// <param name="namespaces">命名空间集合</param>
            /// <returns></returns>
            string GetDefaultValue(string key, string defaultValue, params string[] namespaces);
    
            /// <summary>
            /// 获取配置项,如果值为 <see cref="null"/> 则取参数 <see cref="defaultValue"/> 值。
            /// </summary>
            /// <typeparam name="TValue">值类型</typeparam>
            /// <param name="key"></param>
            /// <param name="defaultValue">默认值</param>
            /// <param name="namespaces">命名空间集合</param>
            /// <returns></returns>
            TValue GetDefaultValue<TValue>(string key, TValue defaultValue, params string[] namespaces);
        }
    
    }

    4.新建ApolloConfiguration

    using Com.Ctrip.Framework.Apollo;
    using Com.Ctrip.Framework.Apollo.Model;
    using System;
    using System.Globalization;
    
    namespace MyCompany.MyProject.Infrastructure
    {
        public class ApolloConfiguration : IApolloConfiguration
        {
            private readonly string _defaultValue = null;
    
            //public event EventHandler<ConfigurationChangeEventArgs> ConfigChanged;
    
            private IConfig GetConfig(params string[] namespaces)
            {
                var config = namespaces == null || namespaces.Length == 0 ?
                    ApolloConfigurationManager.GetAppConfig().GetAwaiter().GetResult() :
                    ApolloConfigurationManager.GetConfig(namespaces).GetAwaiter().GetResult();
    
                config.ConfigChanged += Config_ConfigChanged;
    
                return config;
            }
    
            private void Config_ConfigChanged(IConfig sender, ConfigChangeEventArgs args)
            {
                foreach (string key in args.ChangedKeys)
                {
                    ConfigChange change = args.GetChange(key);
                    // Console.WriteLine("Change - key: {0}, oldValue: {1}, newValue: {2}, changeType: {3}", change.PropertyName, change.OldValue, change.NewValue, change.ChangeType);
                }
            }
    
            public string GetValue(string key, params string[] namespaces)
            {
                if (string.IsNullOrWhiteSpace(key))
                    throw new ArgumentNullException(nameof(key));
                
                var config = GetConfig(namespaces);
                return config.GetProperty(key, _defaultValue);
            }
    
            public TValue GetValue<TValue>(string key, params string[] namespaces)
            {
                var value = GetValue(key, namespaces);
                return value == null ?
                    default(TValue) :
                    (TValue)Convert.ChangeType(value, typeof(TValue), CultureInfo.InvariantCulture);
            }
    
            public string GetDefaultValue(string key, string defaultValue, params string[] namespaces)
            {
                if (string.IsNullOrWhiteSpace(key))
                    throw new ArgumentNullException(nameof(key));
    
                var config = GetConfig(namespaces);
                return config.GetProperty(key, defaultValue);
            }
    
            public TValue GetDefaultValue<TValue>(string key, TValue defaultValue, params string[] namespaces)
            {
                var value = GetDefaultValue(key, defaultValue, namespaces);
                return value == null ?
                    default(TValue) :
                    (TValue)Convert.ChangeType(value, typeof(TValue), CultureInfo.InvariantCulture);
            }
        }
    }

    5.使用:

    IApolloConfiguration config = new ApolloConfiguration();
                string aa = config.GetValue("timeout");

    .

  • 相关阅读:
    【DL】如何使用MMSegmentation训练数据集
    【python基础】Python错误:AttributeError: module 'json' has no attribute 'loads'解决办法
    【python基础】如何理解Python装饰器?
    【DL】如何生成用于训练的数据集
    【pytorch基础】基于训练的pytorch模型转换为onnx模型并测试
    【python基础】JupyterNotebook配置远程登录
    【工具使用】标注工具Labelme的安装以及使用
    【leetcode_easy_math】892. Surface Area of 3D Shapes
    【leetcode_easy】1636. Sort Array by Increasing Frequency
    【leetcode_easy】1640. Check Array Formation Through Concatenation
  • 原文地址:https://www.cnblogs.com/runliuv/p/15913785.html
Copyright © 2020-2023  润新知