/// <summary> /// 添加缓存 /// </summary> /// <param name="hostBuilder"></param> /// <param name="action"></param> /// <returns></returns> public static IFlashHostBuilder AddCache(this IFlashHostBuilder hostBuilder, Action <IFlashCacheBuilder> action) { var builder = new FlashCacheBuilder(hostBuilder.Services, hostBuilder); action(builder); return(hostBuilder); }
public static IFlashHostBuilder AddOpenTracing(this IFlashHostBuilder hostBuilder, Action <IFlashTractingBuilder> action) { var builder = new FlashTractingBuilder(hostBuilder.Services); action(builder); return(hostBuilder); }
/// <summary> /// 注册分布式锁,实例对象IDistributedLock /// </summary> /// <param name="hostBuilder"></param> /// <param name="action"></param> /// <returns></returns> public static IFlashHostBuilder AddRedisDistributedLock(this IFlashHostBuilder hostBuilder, Action <RedisCacheConfig> action) { action = action ?? throw new ArgumentNullException(nameof(action)); var config = new RedisCacheConfig(); action(config); hostBuilder.Services.TryAddSingleton <IDistributedLock>(new DistributedLock(CacheFactory.Build(config))); return(hostBuilder); }
public static IFlashHostBuilder AddSecurity3DES(this IFlashHostBuilder hostBuilder, Action <SecurityOption> setup) { var option = new SecurityOption(); setup(option); hostBuilder.Services.AddSingleton <ISecurity3DES>(sp => { return(new Security3DES(option.SecretKey, option.Encoding)); }); return(hostBuilder); }
/// <summary> /// 添加唯一Id生成器 /// </summary> /// <param name="hostBuilder"></param> /// <param name="setup"></param> /// <returns></returns> public static IFlashHostBuilder AddUniqueIdGenerator(this IFlashHostBuilder hostBuilder, Action <IdGeneratorOption> setup) { var option = new IdGeneratorOption(); setup(option); hostBuilder.Services.AddSingleton <IUniqueIdGenerator>(sp => { var workId = option.WorkIdCreateStrategy.NextId(); return(new SnowflakeUniqueIdGenerator(workId, option.CenterId)); }); return(hostBuilder); }
/// <summary> /// 添加事件总线 /// </summary> /// <param name="hostBuilder"></param> /// <param name="setup"></param> /// <returns></returns> public static IFlashHostBuilder AddEventBus(this IFlashHostBuilder hostBuilder, Action <IEventBusHostBuilder> setup) { var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes().Where(type => Array.Exists(type.GetInterfaces(), t => t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(IProcessMessageHandler <>))))) .ToArray(); foreach (var type in types) { hostBuilder.Services.AddSingleton(type); } hostBuilder.Services.AddSingleton <ITracerFactory, TracerFactory>(); var builder = new EventBusHostBuilder(hostBuilder.Services); setup(builder); return(hostBuilder); }
public FlashCacheBuilder(IServiceCollection services, IFlashHostBuilder flashHost) { this._services = services; this._flashHost = flashHost; }
public static IFlashHostBuilder AddRabbitMQ(this IFlashHostBuilder hostBuilder, Action <RabbitMQOption> setup) { setup = setup ?? throw new ArgumentNullException(nameof(setup)); var option = new RabbitMQOption(); setup(option); hostBuilder.Services.AddSingleton <IConnectionFactory>(sp => { var logger = sp.GetRequiredService <ILogger <IConnectionFactory> >(); var factory = new ConnectionFactory(); factory.HostName = option.HostName; factory.Port = option.Port; factory.Password = option.Password; factory.UserName = option.UserName; factory.VirtualHost = option.VirtualHost; factory.AutomaticRecoveryEnabled = true; factory.TopologyRecoveryEnabled = true; factory.UseBackgroundThreadsForIO = true; return(factory); }); hostBuilder.Services.AddSingleton <ILoadBalancerFactory <IRabbitMQPersistentConnection> >(sp => { return(new DefaultLoadBalancerFactory <IRabbitMQPersistentConnection>()); }); hostBuilder.Services.AddSingleton <IBus, RabbitMQBus>(sp => { var logger = sp.GetRequiredService <ILogger <IBus> >(); var loggerConnection = sp.GetRequiredService <ILogger <IRabbitMQPersistentConnection> >(); var rabbitMQPersisterConnectionLoadBalancerFactory = sp.GetRequiredService <ILoadBalancerFactory <IRabbitMQPersistentConnection> >(); var connectionFactory = sp.GetRequiredService <IConnectionFactory>(); var senderConnections = new List <IRabbitMQPersistentConnection>(); var receiveConnections = new List <IRabbitMQPersistentConnection>(); //消费端连接池 for (int i = 0; i < option.ReceiverMaxConnections; i++) { var connection = new RabbitMQPersistentConnection(connectionFactory, loggerConnection, option.ReceiverAcquireRetryAttempts); connection.TryConnect(); //消费端的连接池 receiveConnections.Add(connection); } //发送端连接池 for (int i = 0; i < option.SenderMaxConnections; i++) { var connection = new RabbitMQPersistentConnection(connectionFactory, loggerConnection, option.SenderAcquireRetryAttempts); connection.TryConnect(); senderConnections.Add(connection); } var receiveLoadBlancer = rabbitMQPersisterConnectionLoadBalancerFactory.Resolve(() => receiveConnections, option.ReceiverLoadBalancer); var senderLoadBlancer = rabbitMQPersisterConnectionLoadBalancerFactory.Resolve(() => senderConnections, option.SenderLoadBalancer); return(new RabbitMQBus( receiveLoadBlancer, senderLoadBlancer, sp, logger, option.ReveiverMaxDegreeOfParallelism, option.ReceiverAcquireRetryAttempts, option.ReceiverHandlerTimeoutMillseconds, option.SenderAcquireRetryAttempts, option.PreFetch, option.Exchange, option.ExchangeType )); }); return(hostBuilder); }