forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyInjection.cs
144 lines (135 loc) · 5.78 KB
/
DependencyInjection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using DotNetCore.CAP;
using DotNetCore.CAP.Dashboard;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using RabbitMQ.Client;
using Savorboard.CAP.InMemoryMessageQueue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IoTSharp.Contracts;
using static IoTSharp.EventBus.EventBusOption;
namespace IoTSharp.EventBus.CAP
{
public static class DependencyInjection
{
public static IApplicationBuilder UseCAPEventBus(this IApplicationBuilder app)
{
var provider = app.ApplicationServices;
var options = provider.GetService<EventBusOption>();
return app;
}
public static void UserCAP(this EventBusOption opt)
{
var settings = opt.AppSettings;
var healthChecks = opt.HealthChecks;
var _EventBusStore = opt.EventBusStore;
var _EventBusMQ = opt.EventBusMQ;
var services = opt.services;
services.AddTransient<ISubscriber, CapSubscriber>();
services.AddTransient<IPublisher, CapPublisher>();
services.AddCap(x =>
{
string _hc_EventBusStore = $"{nameof(EventBusStore)}-{Enum.GetName(settings.EventBusStore)}";
x.SucceedMessageExpiredAfter = settings.SucceedMessageExpiredAfter;
x.ConsumerThreadCount = settings.ConsumerThreadCount;
switch (settings.EventBusStore)
{
case EventBusStore.PostgreSql:
x.UsePostgreSql(_EventBusStore);
healthChecks.AddNpgSql(_EventBusStore, name: _hc_EventBusStore);
break;
case EventBusStore.MongoDB:
x.UseMongoDB(_EventBusStore); //注意,仅支持MongoDB 4.0+集群
healthChecks.AddMongoDb(_EventBusStore, name: _hc_EventBusStore);
break;
case EventBusStore.LiteDB:
x.UseLiteDBStorage(_EventBusStore);
break;
case EventBusStore.MySql:
x.UseMySql(_EventBusStore);
break;
case EventBusStore.SqlServer:
x.UseSqlServer(_EventBusStore);
break;
case EventBusStore.InMemory:
default:
x.UseInMemoryStorage();
break;
}
string _hc_EventBusMQ = $"{nameof(EventBusMQ)}-{Enum.GetName(settings.EventBusMQ)}";
switch (settings.EventBusMQ)
{
case EventBusMQ.RabbitMQ:
var url = new Uri(_EventBusMQ);
x.UseRabbitMQ(cfg =>
{
cfg.ConnectionFactoryOptions = cf =>
{
cf.AutomaticRecoveryEnabled = true;
cf.Uri = new Uri(_EventBusMQ);
};
});
//amqp://guest:guest@localhost:5672
healthChecks.AddRabbitMQ(connectionFactory =>
{
var factory = new ConnectionFactory()
{
Uri = new Uri(_EventBusMQ),
AutomaticRecoveryEnabled = true
};
return factory.CreateConnection();
}, _hc_EventBusMQ);
break;
case EventBusMQ.Kafka:
x.UseKafka(_EventBusMQ);
healthChecks.AddKafka(cfg =>
{
cfg.BootstrapServers = _EventBusMQ;
}, name: _hc_EventBusMQ);
break;
case EventBusMQ.ZeroMQ:
x.UseZeroMQ(cfg =>
{
cfg.HostName = _EventBusMQ ?? "127.0.0.1";
cfg.Pattern = MaiKeBing.CAP.NetMQPattern.PushPull;
});
break;
case EventBusMQ.AzureServiceBus:
x.UseAzureServiceBus(_EventBusMQ);
break;
case EventBusMQ.AmazonSQS:
x.UseAmazonSQS(opts =>
{
var uri = new Uri(_EventBusMQ);
if (!string.IsNullOrEmpty(uri.UserInfo) && uri.UserInfo?.Split(':').Length == 2)
{
var userinfo = uri.UserInfo.Split(':');
opts.Credentials = new Amazon.Runtime.BasicAWSCredentials(userinfo[0], userinfo[1]);
}
opts.Region = Amazon.RegionEndpoint.GetBySystemName(uri.Host);
});
break;
case EventBusMQ.RedisStreams:
x.UseRedis(_EventBusMQ);
break;
case EventBusMQ.NATS:
x.UseNATS(_EventBusMQ);
break;
case EventBusMQ.Pulsar:
x.UsePulsar(_EventBusMQ);
break;
case EventBusMQ.InMemory:
default:
x.UseInMemoryMessageQueue();
break;
}
x.UseDashboard();
//x.UseDiscovery();
});
}
}
}