static void InitRocketMq(IObjectContainer services, ITracer tracer) { // 引入 RocketMQ 生产者 var producer = new Producer { Topic = "nx_test", NameServerAddress = "127.0.0.1:9876", Tracer = tracer, Log = XTrace.Log, }; services.AddSingleton(producer); // 引入 RocketMQ 消费者 var consumer = new Consumer { Topic = "nx_test", Group = "test", NameServerAddress = "127.0.0.1:9876", FromLastOffset = true, SkipOverStoredMsgCount = 0, BatchSize = 20, Tracer = tracer, Log = XTrace.Log, }; services.AddSingleton(consumer); }
static void InitRedis(IObjectContainer services, ITracer tracer) { // 引入 Redis,用于消息队列和缓存,单例,带性能跟踪 var rds = new FullRedis { Tracer = tracer }; rds.Init("server=127.0.0.1:6379;password=;db=3;timeout=5000"); services.AddSingleton <ICache>(rds); services.AddSingleton(rds); }
static void InitMqtt(IObjectContainer services, ITracer tracer) { // 引入 MQTT var mqtt = new MqttClient { //Tracer = tracer, Log = XTrace.Log, Server = "tcp://127.0.0.1:1883", ClientId = Environment.MachineName, UserName = "******", Password = "******", }; services.AddSingleton(mqtt); }
static ITracer InitTracer(IObjectContainer services) { var server = Stardust.Setting.Current.Server; if (server.IsNullOrEmpty()) { server = "http://star.newlifex.com:6600"; } // 星尘监控,性能跟踪器 var tracer = new StarTracer(server) { Log = XTrace.Log }; DefaultTracer.Instance = tracer; ApiHelper.Tracer = tracer; DAL.GlobalTracer = tracer; services.AddSingleton <ITracer>(tracer); return(tracer); }
/// <summary>添加单实例,指定实例工厂</summary> /// <typeparam name="TService"></typeparam> /// <param name="container"></param> /// <param name="factory"></param> /// <returns></returns> public static IObjectContainer AddSingleton <TService>(this IObjectContainer container, Func <IServiceProvider, Object> factory) where TService : class => container.AddSingleton(typeof(TService), factory);
/// <summary>添加单实例,指定实现类型</summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImplementation"></typeparam> /// <param name="container"></param> /// <returns></returns> public static IObjectContainer AddSingleton <TService, TImplementation>(this IObjectContainer container) where TService : class where TImplementation : class, TService => container.AddSingleton(typeof(TService), typeof(TImplementation));
/// <summary>添加单实例,指定实例</summary> /// <typeparam name="TService"></typeparam> /// <param name="container"></param> /// <param name="instance"></param> /// <returns></returns> public static IObjectContainer AddSingleton <TService>(this IObjectContainer container, TService instance = null) where TService : class => container.AddSingleton(typeof(TService), instance);