├── .gitignore
├── .vscode
├── launch.json
└── tasks.json
├── EmitLog
├── EmitLog.csproj
├── Program.cs
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── EmitLog.deps.json
│ │ ├── EmitLog.runtimeconfig.dev.json
│ │ └── EmitLog.runtimeconfig.json
├── logs_from_rabbit.log
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── EmitLog.AssemblyInfo.cs
│ │ └── EmitLog.csproj.FileListAbsolute.txt
│ ├── EmitLog.csproj.nuget.g.props
│ ├── EmitLog.csproj.nuget.g.targets
│ └── project.assets.json
├── EmitLogDirect
├── EmitLogDirect.csproj
├── Program.cs
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── EmitLogDirect.deps.json
│ │ ├── EmitLogDirect.runtimeconfig.dev.json
│ │ └── EmitLogDirect.runtimeconfig.json
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── EmitLogDirect.AssemblyInfo.cs
│ │ └── EmitLogDirect.csproj.FileListAbsolute.txt
│ ├── EmitLogDirect.csproj.nuget.g.props
│ ├── EmitLogDirect.csproj.nuget.g.targets
│ └── project.assets.json
├── EmitLogTopic
├── EmitLogTopic.csproj
└── Program.cs
├── NewTask
├── NewTask.csproj
├── Program.cs
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── NewTask.deps.json
│ │ ├── NewTask.dll
│ │ ├── NewTask.pdb
│ │ ├── NewTask.runtimeconfig.dev.json
│ │ └── NewTask.runtimeconfig.json
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── NewTask.AssemblyInfo.cs
│ │ ├── NewTask.csproj.FileListAbsolute.txt
│ │ ├── NewTask.dll
│ │ └── NewTask.pdb
│ ├── NewTask.csproj.nuget.g.props
│ ├── NewTask.csproj.nuget.g.targets
│ └── project.assets.json
├── README.md
├── RPCClient
├── Program.cs
└── RPCClient.csproj
├── RPCServer
├── Program.cs
└── RPCServer.csproj
├── Receive
├── Program.cs
├── Receive.csproj
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── Receive.deps.json
│ │ ├── Receive.dll
│ │ ├── Receive.pdb
│ │ ├── Receive.runtimeconfig.dev.json
│ │ └── Receive.runtimeconfig.json
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── Receive.AssemblyInfo.cs
│ │ ├── Receive.csproj.FileListAbsolute.txt
│ │ ├── Receive.csprojResolveAssemblyReference.cache
│ │ ├── Receive.dll
│ │ └── Receive.pdb
│ ├── Receive.csproj.nuget.g.props
│ ├── Receive.csproj.nuget.g.targets
│ └── project.assets.json
├── ReceiveLogs
├── Program.cs
├── ReceiveLogs.csproj
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── ReceiveLogs.deps.json
│ │ ├── ReceiveLogs.runtimeconfig.dev.json
│ │ └── ReceiveLogs.runtimeconfig.json
├── logs_from_rabbit.log
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── ReceiveLogs.AssemblyInfo.cs
│ │ └── ReceiveLogs.csproj.FileListAbsolute.txt
│ ├── ReceiveLogs.csproj.nuget.g.props
│ ├── ReceiveLogs.csproj.nuget.g.targets
│ └── project.assets.json
├── ReceiveLogsDirect
├── Program.cs
├── ReceiveLogsDirect.csproj
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── ReceiveLogsDirect.deps.json
│ │ ├── ReceiveLogsDirect.runtimeconfig.dev.json
│ │ └── ReceiveLogsDirect.runtimeconfig.json
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── ReceiveLogsDirect.AssemblyInfo.cs
│ │ └── ReceiveLogsDirect.csproj.FileListAbsolute.txt
│ ├── ReceiveLogsDirect.csproj.nuget.g.props
│ ├── ReceiveLogsDirect.csproj.nuget.g.targets
│ └── project.assets.json
├── ReceiveLogsTopic
├── Program.cs
└── ReceiveLogsTopic.csproj
├── Send
├── Program.cs
├── Send.csproj
├── bin
│ └── Debug
│ │ └── netcoreapp1.1
│ │ ├── Send.deps.json
│ │ ├── Send.dll
│ │ ├── Send.pdb
│ │ ├── Send.runtimeconfig.dev.json
│ │ └── Send.runtimeconfig.json
└── obj
│ ├── Debug
│ └── netcoreapp1.1
│ │ ├── CoreCompileInputs.cache
│ │ ├── Send.AssemblyInfo.cs
│ │ ├── Send.csproj.FileListAbsolute.txt
│ │ ├── Send.dll
│ │ └── Send.pdb
│ ├── Send.csproj.nuget.g.props
│ ├── Send.csproj.nuget.g.targets
│ └── project.assets.json
└── Worker
├── Program.cs
├── Worker.csproj
├── bin
└── Debug
│ └── netcoreapp1.1
│ ├── Worker.deps.json
│ ├── Worker.dll
│ ├── Worker.pdb
│ ├── Worker.runtimeconfig.dev.json
│ └── Worker.runtimeconfig.json
└── obj
├── Debug
└── netcoreapp1.1
│ ├── CoreCompileInputs.cache
│ ├── Worker.AssemblyInfo.cs
│ ├── Worker.csproj.FileListAbsolute.txt
│ ├── Worker.dll
│ └── Worker.pdb
├── Worker.csproj.nuget.g.props
├── Worker.csproj.nuget.g.targets
└── project.assets.json
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | [Xx]64/
19 | [Xx]86/
20 | [Bb]uild/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 |
85 | # Visual Studio profiler
86 | *.psess
87 | *.vsp
88 | *.vspx
89 | *.sap
90 |
91 | # TFS 2012 Local Workspace
92 | $tf/
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 | *.DotSettings.user
101 |
102 | # JustCode is a .NET coding add-in
103 | .JustCode
104 |
105 | # TeamCity is a build add-in
106 | _TeamCity*
107 |
108 | # DotCover is a Code Coverage Tool
109 | *.dotCover
110 |
111 | # NCrunch
112 | _NCrunch_*
113 | .*crunch*.local.xml
114 | nCrunchTemp_*
115 |
116 | # MightyMoose
117 | *.mm.*
118 | AutoTest.Net/
119 |
120 | # Web workbench (sass)
121 | .sass-cache/
122 |
123 | # Installshield output folder
124 | [Ee]xpress/
125 |
126 | # DocProject is a documentation generator add-in
127 | DocProject/buildhelp/
128 | DocProject/Help/*.HxT
129 | DocProject/Help/*.HxC
130 | DocProject/Help/*.hhc
131 | DocProject/Help/*.hhk
132 | DocProject/Help/*.hhp
133 | DocProject/Help/Html2
134 | DocProject/Help/html
135 |
136 | # Click-Once directory
137 | publish/
138 |
139 | # Publish Web Output
140 | *.[Pp]ublish.xml
141 | *.azurePubxml
142 |
143 | # TODO: Un-comment the next line if you do not want to checkin
144 | # your web deploy settings because they may include unencrypted
145 | # passwords
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # NuGet Packages
150 | *.nupkg
151 | # The packages folder can be ignored because of Package Restore
152 | **/packages/*
153 | # except build/, which is used as an MSBuild target.
154 | !**/packages/build/
155 | # Uncomment if necessary however generally it will be regenerated when needed
156 | #!**/packages/repositories.config
157 | # NuGet v3's project.json files produces more ignoreable files
158 | *.nuget.props
159 | *.nuget.targets
160 |
161 | # Microsoft Azure Build Output
162 | csx/
163 | *.build.csdef
164 |
165 | # Microsoft Azure Emulator
166 | ecf/
167 | rcf/
168 |
169 | # Windows Store app package directory
170 | AppPackages/
171 | BundleArtifacts/
172 |
173 | # Visual Studio cache files
174 | # files ending in .cache can be ignored
175 | *.[Cc]ache
176 | # but keep track of directories ending in .cache
177 | !*.[Cc]ache/
178 |
179 | # Others
180 | ClientBin/
181 | [Ss]tyle[Cc]op.*
182 | ~$*
183 | *~
184 | *.dbmdl
185 | *.dbproj.schemaview
186 | *.pfx
187 | *.publishsettings
188 | node_modules/
189 | orleans.codegen.cs
190 |
191 | # RIA/Silverlight projects
192 | Generated_Code/
193 |
194 | # Backup & report files from converting an old project file
195 | # to a newer Visual Studio version. Backup files are not needed,
196 | # because we have git ;-)
197 | _UpgradeReport_Files/
198 | Backup*/
199 | UpgradeLog*.XML
200 | UpgradeLog*.htm
201 |
202 | # SQL Server files
203 | *.mdf
204 | *.ldf
205 |
206 | # Business Intelligence projects
207 | *.rdl.data
208 | *.bim.layout
209 | *.bim_*.settings
210 |
211 | # Microsoft Fakes
212 | FakesAssemblies/
213 |
214 | # GhostDoc plugin setting file
215 | *.GhostDoc.xml
216 |
217 | # Node.js Tools for Visual Studio
218 | .ntvs_analysis.dat
219 |
220 | # Visual Studio 6 build log
221 | *.plg
222 |
223 | # Visual Studio 6 workspace options file
224 | *.opt
225 |
226 | # Visual Studio LightSwitch build output
227 | **/*.HTMLClient/GeneratedArtifacts
228 | **/*.DesktopClient/GeneratedArtifacts
229 | **/*.DesktopClient/ModelManifest.xml
230 | **/*.Server/GeneratedArtifacts
231 | **/*.Server/ModelManifest.xml
232 | _Pvt_Extensions
233 |
234 | # LightSwitch generated files
235 | GeneratedArtifacts/
236 | ModelManifest.xml
237 |
238 | # Paket dependency manager
239 | .paket/paket.exe
240 |
241 | # FAKE - F# Make
242 | .fake/
243 |
244 | Logs.txt.*
245 | Logs.txt
246 | *.pubxml
247 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to find out which attributes exist for C# debugging
3 | // Use hover for the description of the existing attributes
4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "name": ".NET Core Launch (console)",
9 | "type": "coreclr",
10 | "request": "launch",
11 | "preLaunchTask": "build",
12 | // If you have changed target frameworks, make sure to update the program path.
13 | "program": "${workspaceRoot}/RPCServer/bin/Debug/netcoreapp1.1/RPCServer.dll",
14 | "args": [],
15 | "cwd": "${workspaceRoot}/RPCServer",
16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
17 | "console": "internalConsole",
18 | "stopAtEntry": false,
19 | "internalConsoleOptions": "openOnSessionStart"
20 | },
21 | {
22 | "name": ".NET Core Attach",
23 | "type": "coreclr",
24 | "request": "attach",
25 | "processId": "${command:pickProcess}"
26 | }
27 | ]
28 | }
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.1.0",
3 | "command": "dotnet",
4 | "isShellCommand": true,
5 | "args": [],
6 | "tasks": [
7 | {
8 | "taskName": "build",
9 | "args": [
10 | "${workspaceRoot}/RPCServer/RPCServer.csproj"
11 | ],
12 | "isBuildCommand": true,
13 | "problemMatcher": "$msCompile"
14 | }
15 | ]
16 | }
--------------------------------------------------------------------------------
/EmitLog/EmitLog.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/EmitLog/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 |
5 | namespace EmitLog
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var connectionFactory = new ConnectionFactory() { HostName = "localhost" };
12 | using (var connection = connectionFactory.CreateConnection())
13 | {
14 | using (var channel = connection.CreateModel())
15 | {
16 | // 生成随机队列名称
17 | var queueName = channel.QueueDeclare().QueueName;
18 | //使用fanout exchange type,指定exchange名称
19 | channel.ExchangeDeclare(exchange: "logs", type: "fanout");
20 |
21 | var message = GetMessage(args);
22 | var body = Encoding.UTF8.GetBytes(message);
23 | //发布到指定exchange,fanout类型无需指定routingKey
24 | channel.BasicPublish(exchange: "logs", routingKey: "", basicProperties: null, body: body);
25 | Console.WriteLine(" [x] Sent {0}", message);
26 | }
27 | }
28 |
29 | Console.WriteLine(" Press [enter] to exit.");
30 | Console.ReadLine();
31 | }
32 |
33 | private static string GetMessage(string[] args)
34 | {
35 | return ((args.Length > 0) ?
36 | string.Join(" ", args) :
37 | "info: Hello World!");
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/EmitLog/bin/Debug/netcoreapp1.1/EmitLog.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "emitlog/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "EmitLog.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "emitlog/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/EmitLog/bin/Debug/netcoreapp1.1/EmitLog.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/EmitLog/bin/Debug/netcoreapp1.1/EmitLog.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/EmitLog/logs_from_rabbit.log:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/EmitLog/logs_from_rabbit.log
--------------------------------------------------------------------------------
/EmitLog/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 67b7c0b9b1072201f9d4c3d2ef970d19d4feb21b
2 |
--------------------------------------------------------------------------------
/EmitLog/obj/Debug/netcoreapp1.1/EmitLog.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("EmitLog")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("EmitLog")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("EmitLog")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/EmitLog/obj/Debug/netcoreapp1.1/EmitLog.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | F:\Coding\RabbitMQ\emitlog\bin\Debug\netcoreapp1.1\EmitLog.deps.json
2 | F:\Coding\RabbitMQ\emitlog\bin\Debug\netcoreapp1.1\EmitLog.runtimeconfig.json
3 | F:\Coding\RabbitMQ\emitlog\bin\Debug\netcoreapp1.1\EmitLog.runtimeconfig.dev.json
4 | F:\Coding\RabbitMQ\emitlog\bin\Debug\netcoreapp1.1\EmitLog.dll
5 | F:\Coding\RabbitMQ\emitlog\bin\Debug\netcoreapp1.1\EmitLog.pdb
6 | F:\Coding\RabbitMQ\emitlog\obj\Debug\netcoreapp1.1\EmitLog.AssemblyInfo.cs
7 | F:\Coding\RabbitMQ\emitlog\obj\Debug\netcoreapp1.1\EmitLog.dll
8 | F:\Coding\RabbitMQ\emitlog\obj\Debug\netcoreapp1.1\EmitLog.pdb
9 |
--------------------------------------------------------------------------------
/EmitLog/obj/EmitLog.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | F:\Coding\RabbitMQ\emitlog\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/EmitLog/obj/EmitLog.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/EmitLogDirect/EmitLogDirect.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/EmitLogDirect/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Text;
4 | using RabbitMQ.Client;
5 |
6 | namespace EmitLogDirect {
7 | class Program {
8 | static void Main (string[] args) {
9 | var factory = new ConnectionFactory () { HostName = "localhost" };
10 | using (var connection = factory.CreateConnection ())
11 | using (var channel = connection.CreateModel ()) {
12 | channel.ExchangeDeclare (exchange: "direct_logs",
13 | type: "direct");
14 |
15 | var logLevel = (args.Length > 0) ? args[0] : "info";
16 | var message = (args.Length > 1) ?
17 | string.Join (" ", args.Skip (1).ToArray ()) :
18 | "Hello RabbitMQ!";
19 | var body = Encoding.UTF8.GetBytes (message);
20 | channel.BasicPublish (exchange: "direct_logs",
21 | routingKey : logLevel,
22 | basicProperties : null,
23 | body : body);
24 | Console.WriteLine (" [x] Sent '{0}':'{1}'", logLevel, message);
25 | }
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/EmitLogDirect/bin/Debug/netcoreapp1.1/EmitLogDirect.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "emitlogdirect/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "EmitLogDirect.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "emitlogdirect/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/EmitLogDirect/bin/Debug/netcoreapp1.1/EmitLogDirect.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/EmitLogDirect/bin/Debug/netcoreapp1.1/EmitLogDirect.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/EmitLogDirect/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 37b0ee8e042d7082e3cd36cad899f495e02cdccc
2 |
--------------------------------------------------------------------------------
/EmitLogDirect/obj/Debug/netcoreapp1.1/EmitLogDirect.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("EmitLogDirect")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("EmitLogDirect")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("EmitLogDirect")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/EmitLogDirect/obj/Debug/netcoreapp1.1/EmitLogDirect.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | F:\Coding\RabbitMQ\emitlogdirect\bin\Debug\netcoreapp1.1\EmitLogDirect.deps.json
2 | F:\Coding\RabbitMQ\emitlogdirect\bin\Debug\netcoreapp1.1\EmitLogDirect.runtimeconfig.json
3 | F:\Coding\RabbitMQ\emitlogdirect\bin\Debug\netcoreapp1.1\EmitLogDirect.runtimeconfig.dev.json
4 | F:\Coding\RabbitMQ\emitlogdirect\bin\Debug\netcoreapp1.1\EmitLogDirect.dll
5 | F:\Coding\RabbitMQ\emitlogdirect\bin\Debug\netcoreapp1.1\EmitLogDirect.pdb
6 | F:\Coding\RabbitMQ\emitlogdirect\obj\Debug\netcoreapp1.1\EmitLogDirect.AssemblyInfo.cs
7 | F:\Coding\RabbitMQ\emitlogdirect\obj\Debug\netcoreapp1.1\EmitLogDirect.dll
8 | F:\Coding\RabbitMQ\emitlogdirect\obj\Debug\netcoreapp1.1\EmitLogDirect.pdb
9 |
--------------------------------------------------------------------------------
/EmitLogDirect/obj/EmitLogDirect.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | F:\Coding\RabbitMQ\emitlogdirect\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/EmitLogDirect/obj/EmitLogDirect.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/EmitLogTopic/EmitLogTopic.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/EmitLogTopic/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using RabbitMQ.Client;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace EmitLogTopic
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | using (var connection = factory.CreateConnection())
14 | {
15 | using (var channel = connection.CreateModel())
16 | {
17 | channel.ExchangeDeclare(exchange: "topic_logs", type: "topic");
18 | var routingKey = (args.Length > 0) ? args[0] : "anonymous.info";
19 |
20 | var message = args.Length > 1 ? string.Join(" ", args.Skip(1).ToArray()) : "Hello RabbitMQ!";
21 | var body = Encoding.UTF8.GetBytes(message);
22 |
23 | channel.BasicPublish(exchange: "topic_logs", routingKey: routingKey, body: body);
24 |
25 | Console.WriteLine("[x] Sent '{0}':'{1}'", routingKey, message);
26 | }
27 | }
28 |
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/NewTask/NewTask.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/NewTask/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 |
5 | namespace NewTask
6 | {
7 | class Program
8 | {
9 | public static void Main(string[] args)
10 | {
11 | //1.创建基于本地的连接工厂
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | //2. 建立连接
14 | using (var connection = factory.CreateConnection())
15 | {
16 | //3. 创建频道
17 | using (var channel = connection.CreateModel())
18 | {
19 | //4. 申明队列(指定durable:true,告知rabbitmq对消息进行持久化)
20 | channel.QueueDeclare(queue: "work_queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
21 | //将消息标记为持久性 - 将IBasicProperties.SetPersistent设置为true
22 | var properties = channel.CreateBasicProperties();
23 | properties.Persistent = true;
24 |
25 | //5. 构建byte消息数据包
26 | string message = GetMessage(args);
27 | var body = Encoding.UTF8.GetBytes(message);
28 |
29 | //6. 发送数据包
30 | channel.BasicPublish(exchange: "", routingKey: "work_queue", basicProperties: properties, body: body);
31 | Console.WriteLine(" [x] Sent {0}", message);
32 | }
33 | }
34 | }
35 | private static string GetMessage(string[] args)
36 | {
37 | return ((args.Length > 0) ? string.Join(" ", args) : "Hello RabbitMQ!");
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/NewTask/bin/Debug/netcoreapp1.1/NewTask.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "newtask/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "NewTask.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "newtask/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/NewTask/bin/Debug/netcoreapp1.1/NewTask.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/NewTask/bin/Debug/netcoreapp1.1/NewTask.dll
--------------------------------------------------------------------------------
/NewTask/bin/Debug/netcoreapp1.1/NewTask.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/NewTask/bin/Debug/netcoreapp1.1/NewTask.pdb
--------------------------------------------------------------------------------
/NewTask/bin/Debug/netcoreapp1.1/NewTask.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/NewTask/bin/Debug/netcoreapp1.1/NewTask.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/NewTask/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | cd55c5b29588f9b5e94625001d2c803e41c3e40b
2 |
--------------------------------------------------------------------------------
/NewTask/obj/Debug/netcoreapp1.1/NewTask.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("NewTask")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("NewTask")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("NewTask")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/NewTask/obj/Debug/netcoreapp1.1/NewTask.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | D:\Coding\RabbitMQ\NewTask\bin\Debug\netcoreapp1.1\NewTask.deps.json
2 | D:\Coding\RabbitMQ\NewTask\bin\Debug\netcoreapp1.1\NewTask.runtimeconfig.json
3 | D:\Coding\RabbitMQ\NewTask\bin\Debug\netcoreapp1.1\NewTask.runtimeconfig.dev.json
4 | D:\Coding\RabbitMQ\NewTask\bin\Debug\netcoreapp1.1\NewTask.dll
5 | D:\Coding\RabbitMQ\NewTask\bin\Debug\netcoreapp1.1\NewTask.pdb
6 | D:\Coding\RabbitMQ\NewTask\obj\Debug\netcoreapp1.1\NewTask.AssemblyInfo.cs
7 | D:\Coding\RabbitMQ\NewTask\obj\Debug\netcoreapp1.1\NewTask.dll
8 | D:\Coding\RabbitMQ\NewTask\obj\Debug\netcoreapp1.1\NewTask.pdb
9 | F:\Coding\RabbitMQ\newtask\bin\Debug\netcoreapp1.1\NewTask.deps.json
10 | F:\Coding\RabbitMQ\newtask\bin\Debug\netcoreapp1.1\NewTask.runtimeconfig.json
11 | F:\Coding\RabbitMQ\newtask\bin\Debug\netcoreapp1.1\NewTask.runtimeconfig.dev.json
12 | F:\Coding\RabbitMQ\newtask\bin\Debug\netcoreapp1.1\NewTask.dll
13 | F:\Coding\RabbitMQ\newtask\bin\Debug\netcoreapp1.1\NewTask.pdb
14 | F:\Coding\RabbitMQ\newtask\obj\Debug\netcoreapp1.1\NewTask.AssemblyInfo.cs
15 | F:\Coding\RabbitMQ\newtask\obj\Debug\netcoreapp1.1\NewTask.dll
16 | F:\Coding\RabbitMQ\newtask\obj\Debug\netcoreapp1.1\NewTask.pdb
17 |
--------------------------------------------------------------------------------
/NewTask/obj/Debug/netcoreapp1.1/NewTask.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/NewTask/obj/Debug/netcoreapp1.1/NewTask.dll
--------------------------------------------------------------------------------
/NewTask/obj/Debug/netcoreapp1.1/NewTask.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/NewTask/obj/Debug/netcoreapp1.1/NewTask.pdb
--------------------------------------------------------------------------------
/NewTask/obj/NewTask.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | F:\Coding\RabbitMQ\newtask\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/NewTask/obj/NewTask.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > **关注我的公众号【微服务知多少】,发现更多精彩!**
2 | >
3 | 
4 |
5 | -------
6 |
7 |
8 | 
9 |
10 |
11 | # 1.引言
12 | RabbitMQ——Rabbit Message Queue的简写,但不能仅仅理解其为消息队列,消息代理更合适。RabbitMQ 是一个由 Erlang 语言开发的AMQP(高级消息队列协议)的开源实现,其内部结构如下:
13 |
14 | 
15 |
16 | RabbitMQ作为一个消息代理,主要和**消息**打交道,负责接收并转发消息。RabbitMQ提供了可靠的消息机制、跟踪机制和灵活的消息路由,支持消息集群和分布式部署。适用于排队算法、秒杀活动、消息分发、异步处理、数据同步、处理耗时任务、CQRS等应用场景。
17 |
18 | 下面我们就来学习下RabbitMQ。
19 |
20 | # 2. 环境搭建
21 | 本文主要基于Windows下使用Vs Code 基于.net core进行demo演示。开始之前我们需要准备好以下环境。
22 | * 安装Erlang运行环境
23 | 下载安装[Erlang](http://www.erlang.org/downloads)。
24 | * 安装RabbitMQ
25 | 下载安装Windows版本的[RabbitMQ](http://www.rabbitmq.com/install-windows.html)。
26 | * 启动RabbitMQ Server
27 | 点击Windows开始按钮,输入RabbitMQ找到`RabbitMQ Comman Prompt`,以管理员身份运行。
28 | * 依次执行以下命令启动RabbitMQ服务
29 | ```
30 | rabbitmq-service install
31 | rabbitmq-service enable
32 | rabbitmq-service start
33 | ```
34 | * 执行`rabbitmqlctl status`检查RabbitMQ状态
35 | * 安装管理平台插件
36 | 执行`rabbitmq-plugins enable rabbitmq_management`即可成功安装,使用默认账号密码(guest/guest)登录[http://localhost:15672/](http://localhost:15672/ )即可。
37 |
38 | # 3. Hello RabbitMQ
39 | 在开始之前我们先来了解下消息模型:
40 | 
41 | 消费者(consumer)订阅某个队列。生产者(producer)创建消息,然后发布到队列(queue)中,队列再将消息发送到监听的消费者。
42 |
43 | 下面我们我们通过demo来了解RabbitMQ的基本用法。
44 |
45 | ## 3.1.消息的发送和接收
46 |
47 | 创建RabbitMQ文件夹,打开命令提示符,分别创建两个控制台项目Send、Receive。
48 | ```
49 | dotnet new console --name Send //创建发送端控制台应用
50 | cd Send //进入Send目录
51 | dotnet add package RabbitMQ.Client //添加RabbitMQ.Client包
52 | dotnet restore //恢复包
53 | ```
54 | ```
55 | dotnet new console --name Receive //创建接收端控制台应用
56 | cd Receive //进入Receive目录
57 | dotnet add package RabbitMQ.Client //添加RabbitMQ.Client包
58 | dotnet restore //恢复包
59 | ```
60 | 我们先来添加消息发送端逻辑:
61 | ```
62 | //Send.cs
63 | public static void Main(string[] args)
64 | {
65 | //1.1.实例化连接工厂
66 | var factory = new ConnectionFactory() { HostName = "localhost" };
67 | //2. 建立连接
68 | using (var connection = factory.CreateConnection())
69 | {
70 | //3. 创建信道
71 | using (var channel = connection.CreateModel())
72 | {
73 | //4. 申明队列
74 | channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
75 | //5. 构建byte消息数据包
76 | string message = args.Length > 0 ? args[0] : "Hello RabbitMQ!";
77 | var body = Encoding.UTF8.GetBytes(message);
78 | //6. 发送数据包
79 | channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
80 | Console.WriteLine(" [x] Sent {0}", message);
81 | }
82 | }
83 | }
84 | ```
85 | 再来完善消息接收端逻辑:
86 | ```
87 | //Receive.cs 省略部分代码
88 | public static void Main()
89 | {
90 | //1.实例化连接工厂
91 | var factory = new ConnectionFactory() { HostName = "localhost" };
92 | //2. 建立连接
93 | using (var connection = factory.CreateConnection())
94 | {
95 | //3. 创建信道
96 | using (var channel = connection.CreateModel())
97 | {
98 | //4. 申明队列
99 | channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
100 | //5. 构造消费者实例
101 | var consumer = new EventingBasicConsumer(channel);
102 | //6. 绑定消息接收后的事件委托
103 | consumer.Received += (model, ea) =>
104 | {
105 | var message = Encoding.UTF8.GetString(ea.Body);
106 | Console.WriteLine(" [x] Received {0}", message);
107 | Thread.Sleep(6000);//模拟耗时
108 | Console.WriteLine (" [x] Done");
109 | };
110 | //7. 启动消费者
111 | channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
112 | Console.WriteLine(" Press [enter] to exit.");
113 | Console.ReadLine();
114 | }
115 | }
116 | }
117 | ```
118 | 先运行消息接收端,再运行消息发送端,结果如下图。
119 |
120 | 
121 |
122 | 从上面的代码中可以看出,发送端和消费端的代码前4步都是一样的。主要的区别在于发送端调用`channel.BasicPublish`方法发送消息;而接收端需要实例化一个`EventingBasicConsumer`实例来进行消息处理逻辑。另外一点需要注意的是:消息接收端和发送端的队列名称(queue)必须保持一致,这里指定的队列名称为hello。
123 |
124 | ## 3.2. 循环调度
125 | 使用工作队列的好处就是它能够并行的处理队列。如果堆积了很多任务,我们只需要添加更多的工作者(workers)就可以了。我们先启动两个接收端,等待消息接收,再启动一个发送端进行消息发送。
126 |
127 | 
128 |
129 | 我们增加运行一个消费端后的运行结果:
130 |
131 | 
132 |
133 | 从图中可知,我们循环发送4条信息,两个消息接收端按顺序被循环分配。
134 | 默认情况下,RabbitMQ将按顺序将每条消息发送给下一个消费者。平均每个消费者将获得相同数量的消息。这种分发消息的方式叫做循环(round-robin)。
135 |
136 | ## 3.3. 消息确认
137 | 按照我们上面的demo,一旦RabbitMQ将消息发送到消费端,消息就会立即从内存中移出,无论消费端是否处理完成。在这种情况下,消息就会丢失。
138 |
139 | 为了确保一个消息永远不会丢失,RabbitMQ支持**消息确认(message acknowledgments)**。当消费端接收消息并且处理完成后,会发送一个ack(消息确认)信号到RabbitMQ,RabbitMQ接收到这个信号后,就可以删除掉这条已经处理的消息任务。但如果消费端挂掉了(比如,通道关闭、连接丢失等)没有发送ack信号。RabbitMQ就会明白某个消息没有正常处理,RabbitMQ将会重新将消息入队,如果有另外一个消费端在线,就会快速的重新发送到另外一个消费端。
140 |
141 | RabbitMQ中没有消息超时的概念,只有当消费端关闭或奔溃时,RabbitMQ才会重新分发消息。
142 |
143 | 微调下Receive中的代码逻辑:
144 | ```
145 | //5. 构造消费者实例
146 | var consumer = new EventingBasicConsumer(channel);
147 | //6. 绑定消息接收后的事件委托
148 | consumer.Received += (model, ea) =>
149 | {
150 | var message = Encoding.UTF8.GetString(ea.Body);
151 | Console.WriteLine(" [x] Received {0}", message);
152 | Thread.Sleep(6000);//模拟耗时
153 | Console.WriteLine(" [x] Done");
154 | // 7. 发送消息确认信号(手动消息确认)
155 | channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
156 | };
157 | //8. 启动消费者
158 | //autoAck:true;自动进行消息确认,当消费端接收到消息后,就自动发送ack信号,不管消息是否正确处理完毕
159 | //autoAck:false;关闭自动消息确认,通过调用BasicAck方法手动进行消息确认
160 | channel.BasicConsume(queue: "hello", autoAck: false, consumer: consumer);
161 | ```
162 |
163 | 主要改动的是将 `autoAck:true`修改为`autoAck:fasle`,以及在消息处理完毕后手动调用`BasicAck`方法进行手动消息确认。
164 |
165 | 
166 |
167 | 从图中可知,消息发送端连续发送4条消息,其中消费端1先被分配处理第一条消息,消费端2被循环分配第二条消息,第三条消息由于没有空闲消费者仍然在队列中。
168 | 在消费端2未处理完第一条消息之前,手动中断(ctrl+c)。我们可以发现RabbitMQ在下一次分发时,会优先将被中断的消息分发给消费端1处理。
169 |
170 | ## 3.4. 消息持久化
171 | 消息确认确保了即使消费端异常,消息也不会丢失能够被重新分发处理。但是如果RabbitMQ服务端异常,消息依然会丢失。除非我们指定`durable:true`,否则当RabbitMQ退出或奔溃时,消息将依然会丢失。通过指定`durable:true`,并指定`Persistent=true`,来告知RabbitMQ将消息持久化。
172 | ```
173 | //send.cs
174 | //4. 申明队列(指定durable:true,告知rabbitmq对消息进行持久化)
175 | channel.QueueDeclare(queue: "hello", durable: true, exclusive: false, autoDelete: false, arguments
176 | //将消息标记为持久性 - 将IBasicProperties.SetPersistent设置为true
177 | var properties = channel.CreateBasicProperties();
178 | properties.Persistent = true;
179 | //5. 构建byte消息数据包
180 | string message = args.Length > 0 ? args[0] : "Hello RabbitMQ!";
181 | var body = Encoding.UTF8.GetBytes(message);
182 | //6. 发送数据包(指定basicProperties)
183 | channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: properties, body: body);
184 | ```
185 | 将消息标记为持久性不能完全保证消息不会丢失。虽然它告诉RabbitMQ将消息保存到磁盘,但是当RabbitMQ接受消息并且还没有保存时,仍然有一个很短的时间窗口。RabbitMQ 可能只是将消息保存到了缓存中,并没有将其写入到磁盘上。持久化是不能够一定保证的,但是对于一个简单任务队列来说已经足够。如果需要确保消息队列的持久化,可以使用[publisher confirms](https://www.rabbitmq.com/confirms.html).
186 |
187 | ## 3.5. 公平分发
188 | RabbitMQ的消息分发默认按照消费端的数量,按顺序循环分发。这样仅是确保了消费端被平均分发消息的数量,但却忽略了消费端的闲忙情况。这就可能出现某个消费端一直处理耗时任务处于阻塞状态,某个消费端一直处理一般任务处于空置状态,而只是它们分配的任务数量一样。
189 |
190 | 
191 |
192 | 但我们可以通过`channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);`
193 | 设置`prefetchCount : 1`来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时,不再分配任务。
194 | ```
195 | //Receive.cs
196 | //4. 申明队列
197 | channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
198 | //设置prefetchCount : 1来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时
199 | channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
200 | ```
201 |
202 | 这时你需要注意的是如果所有的消费端都处于忙碌状态,你的队列可能会被塞满。你需要注意这一点,要么添加更多的消费端,要么采取其他策略。
203 |
204 | # 4. Exchange
205 | 细心的你也许发现上面的demo,生产者和消费者直接是通过相同队列名称进行匹配衔接的。消费者订阅某个队列,生产者创建消息发布到队列中,队列再将消息转发到订阅的消费者。这样就会有一个局限性,即消费者一次只能发送消息到某一个队列。
206 |
207 | 那消费者如何才能发送消息到多个消息队列呢?
208 | RabbitMQ提供了**Exchange**,它类似于路由器的功能,它用于对消息进行路由,将消息发送到多个队列上。Exchange一方面从生产者接收消息,另一方面将消息推送到队列。但exchange必须知道如何处理接收到的消息,是将其附加到特定队列还是附加到多个队列,还是直接忽略。而这些规则由exchange type定义,exchange的原理如下图所示。
209 | 
210 |
211 | 常见的exchange type 有以下几种:
212 | * direct(明确的路由规则:消费端绑定的队列名称必须和消息发布时指定的路由名称一致)
213 | * topic (模式匹配的路由规则:支持通配符)
214 | * fanout (消息广播,将消息分发到exchange上绑定的所有队列上)
215 |
216 | 下面我们就来一一这介绍它们的用法。
217 |
218 | ## 4.1 fanout
219 | 本着先易后难的思想,我们先来了解下**fanout**的广播路由机制。fanout的路由机制如下图,即发送到 fanout 类型exchange的消息都会分发到所有绑定该exchange的队列上去。
220 |
221 | 
222 |
223 |
224 | 生产者示例代码:
225 | ```
226 | // 生成随机队列名称
227 | var queueName = channel.QueueDeclare().QueueName;
228 | //使用fanout exchange type,指定exchange名称
229 | channel.ExchangeDeclare(exchange: "fanoutEC", type: "fanout");
230 | var message = "Hello Rabbit!";
231 | var body = Encoding.UTF8.GetBytes(message);
232 | //发布到指定exchange,fanout类型无需指定routingKey
233 | channel.BasicPublish(exchange: "fanoutEC", routingKey: "", basicProperties: null, body: body);
234 | ```
235 | 消费者示例代码:
236 | ```
237 | //申明fanout类型exchange
238 | channel.ExchangeDeclare (exchange: "fanoutEC", type: "fanout");
239 | //申明随机队列名称
240 | var queuename = channel.QueueDeclare ().QueueName;
241 | //绑定队列到指定fanout类型exchange,无需指定路由键
242 | channel.QueueBind (queue : queuename, exchange: "fanoutEC", routingKey: "");
243 | ```
244 |
245 | ## 4.2. direct
246 | direct相对于fanout就属于完全匹配、单播的模式,路由机制如下图,即队列名称和消息发送时指定的路由完全匹配时,消息才会发送到指定队列上。
247 | 
248 |
249 | 生产者示例代码:
250 | ```
251 | // 生成随机队列名称
252 | var queueName = channel.QueueDeclare().QueueName;
253 | //使用direct exchange type,指定exchange名称
254 | channel.ExchangeDeclare(exchange: "directEC", type: "direct");
255 | var message = "Hello Rabbit!";
256 | var body = Encoding.UTF8.GetBytes(message);
257 | //发布到direct类型exchange,必须指定routingKey
258 | channel.BasicPublish(exchange: "directEC", routingKey: "green", basicProperties: null, body: body);
259 | ```
260 | 消费者示例代码:
261 | ```
262 | //申明direct类型exchange
263 | channel.ExchangeDeclare (exchange: "directEC", type: "direct");
264 | //绑定队列到direct类型exchange,需指定路由键routingKey
265 | channel.QueueBind (queue : green, exchange: "directEC", routingKey: "green");
266 | ```
267 |
268 | ## 4.3. topic
269 | topic是direct的升级版,是一种模式匹配的路由机制。它支持使用两种通配符来进行模式匹配:符号**`#`**和符号**`*`**。其中**`*`**匹配一个单词, **`#`**则表示匹配0个或多个单词,单词之间用**`.`**分割。如下图所示。
270 | 
271 |
272 | 生产者示例代码:
273 | ```
274 | // 生成随机队列名称
275 | var queueName = channel.QueueDeclare().QueueName;
276 | //使用topic exchange type,指定exchange名称
277 | channel.ExchangeDeclare(exchange: "topicEC", type: "topic");
278 | var message = "Hello Rabbit!";
279 | var body = Encoding.UTF8.GetBytes(message);
280 | //发布到topic类型exchange,必须指定routingKey
281 | channel.BasicPublish(exchange: "topicEC", routingKey: "first.green.fast", basicProperties: null, body: body);
282 | ```
283 | 消费者示例代码:
284 | ```
285 | //申明topic类型exchange
286 | channel.ExchangeDeclare (exchange: "topicEC", type: "topic");
287 | //申明随机队列名称
288 | var queuename = channel.QueueDeclare ().QueueName;
289 | //绑定队列到topic类型exchange,需指定路由键routingKey
290 | channel.QueueBind (queue : queuename, exchange: "topicEC", routingKey: "#.*.fast");
291 | ```
292 |
293 | # 5. RPC
294 | RPC——Remote Procedure Call,远程过程调用。
295 | 那RabbitMQ如何进行远程调用呢?示意图如下:
296 | 
297 | 第一步,主要是进行远程调用的客户端需要指定接收远程回调的队列,并申明消费者监听此队列。
298 | 第二步,远程调用的服务端除了要申明消费端接收远程调用请求外,还要将结果发送到客户端用来监听回调结果的队列中去。
299 |
300 | 远程调用客户端:
301 | ```
302 | //申明唯一guid用来标识此次发送的远程调用请求
303 | var correlationId = Guid.NewGuid().ToString();
304 | //申明需要监听的回调队列
305 | var replyQueue = channel.QueueDeclare().QueueName;
306 | var properties = channel.CreateBasicProperties();
307 | properties.ReplyTo = replyQueue;//指定回调队列
308 | properties.CorrelationId = correlationId;//指定消息唯一标识
309 | string number = args.Length > 0 ? args[0] : "30";
310 | var body = Encoding.UTF8.GetBytes(number);
311 | //发布消息
312 | channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: properties, body: body);
313 | Console.WriteLine($"[*] Request fib({number})");
314 | // //创建消费者用于处理消息回调(远程调用返回结果)
315 | var callbackConsumer = new EventingBasicConsumer(channel);
316 | channel.BasicConsume(queue: replyQueue, autoAck: true, consumer: callbackConsumer);
317 | callbackConsumer.Received += (model, ea) =>
318 | {
319 | //仅当消息回调的ID与发送的ID一致时,说明远程调用结果正确返回。
320 | if (ea.BasicProperties.CorrelationId == correlationId)
321 | {
322 | var responseMsg = $"Get Response: {Encoding.UTF8.GetString(ea.Body)}";
323 | Console.WriteLine($"[x]: {responseMsg}");
324 | }
325 | };
326 | ```
327 |
328 | 远程调用服务端:
329 | ```
330 | //申明队列接收远程调用请求
331 | channel.QueueDeclare(queue: "rpc_queue", durable: false,
332 | exclusive: false, autoDelete: false, arguments: null);
333 | var consumer = new EventingBasicConsumer(channel);
334 | Console.WriteLine("[*] Waiting for message.");
335 | //请求处理逻辑
336 | consumer.Received += (model, ea) =>
337 | {
338 | var message = Encoding.UTF8.GetString(ea.Body);
339 | int n = int.Parse(message);
340 | Console.WriteLine($"Receive request of Fib({n})");
341 | int result = Fib(n);
342 | //从请求的参数中获取请求的唯一标识,在消息回传时同样绑定
343 | var properties = ea.BasicProperties;
344 | var replyProerties = channel.CreateBasicProperties();
345 | replyProerties.CorrelationId = properties.CorrelationId;
346 | //将远程调用结果发送到客户端监听的队列上
347 | channel.BasicPublish(exchange: "", routingKey: properties.ReplyTo,
348 | basicProperties: replyProerties, body: Encoding.UTF8.GetBytes(result.ToString()));
349 | //手动发回消息确认
350 | channel.BasicAck(ea.DeliveryTag, false);
351 | Console.WriteLine($"Return result: Fib({n})= {result}");
352 | };
353 | channel.BasicConsume(queue: "rpc_queue", autoAck: false, consumer: consumer);
354 | ```
355 |
356 | # 6. 总结
357 | 基于上面的demo和对几种不同exchange路由机制的学习,我们发现RabbitMQ主要是涉及到以下几个核心概念:
358 | 1. Publisher:生产者,消息的发送方。
359 | 2. Connection:网络连接。
360 | 3. Channel:信道,多路复用连接中的一条独立的双向数据流通道。
361 | 4. Exchange:交换器(路由器),负责消息的路由到相应队列。
362 | 5. Binding:队列与交换器间的关联绑定。消费者将关注的队列绑定到指定交换器上,以便Exchange能准确分发消息到指定队列。
363 | 6. Queue:队列,消息的缓冲存储区。
364 | 7. Virtual Host:虚拟主机,虚拟主机提供资源的逻辑分组和分离。包含连接,交换,队列,绑定,用户权限,策略等。
365 | 8. Broker:消息队列的服务器实体。
366 | 9. Consumer:消费者,消息的接收方。
367 |
368 | 这次作为入门就讲到这里,下次我们来讲解下**EventBus + RabbitMQ**如何实现事件的分发。
369 |
370 | >参考资料:
371 | [RabbitMQ Tutorials](http://www.rabbitmq.com/getstarted.html)
372 | [Demo路径——RabbitMQ](https://github.com/yanshengjie/RabbitMQ)
373 |
--------------------------------------------------------------------------------
/RPCClient/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 | using RabbitMQ.Client.Events;
5 |
6 | namespace RPCClient
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | using (var connection = factory.CreateConnection())
14 | {
15 | using (var channel = connection.CreateModel())
16 | {
17 | var correlationId = Guid.NewGuid().ToString();
18 | var replyQueue = channel.QueueDeclare().QueueName;
19 |
20 | var properties = channel.CreateBasicProperties();
21 | properties.ReplyTo = replyQueue;
22 | properties.CorrelationId = correlationId;
23 |
24 | string number = args.Length > 0 ? args[0] : "30";
25 | var body = Encoding.UTF8.GetBytes(number);
26 | //发布消息
27 | channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: properties, body: body);
28 |
29 | Console.WriteLine($"[*] Request fib({number})");
30 |
31 | // //创建消费者用于消息回调
32 | var callbackConsumer = new EventingBasicConsumer(channel);
33 | channel.BasicConsume(queue: replyQueue, autoAck: true, consumer: callbackConsumer);
34 |
35 | callbackConsumer.Received += (model, ea) =>
36 | {
37 | if (ea.BasicProperties.CorrelationId == correlationId)
38 | {
39 | var responseMsg = $"Get Response: {Encoding.UTF8.GetString(ea.Body)}";
40 |
41 | Console.WriteLine($"[x]: {responseMsg}");
42 | }
43 | };
44 |
45 | Console.ReadLine();
46 |
47 | }
48 | }
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/RPCClient/RPCClient.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/RPCServer/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 | using RabbitMQ.Client.Events;
5 |
6 | namespace RPCServer
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | using (var conection = factory.CreateConnection())
14 | {
15 | using (var channel = conection.CreateModel())
16 | {
17 | channel.QueueDeclare(queue: "rpc_queue", durable: false,
18 | exclusive: false, autoDelete: false, arguments: null);
19 |
20 | var consumer = new EventingBasicConsumer(channel);
21 | Console.WriteLine("[*] Waiting for message.");
22 |
23 | consumer.Received += (model, ea) =>
24 | {
25 | var message = Encoding.UTF8.GetString(ea.Body);
26 | int n = int.Parse(message);
27 | Console.WriteLine($"Receive request of Fib({n})");
28 | int result = Fib(n);
29 |
30 | var properties = ea.BasicProperties;
31 | var replyProerties = channel.CreateBasicProperties();
32 | replyProerties.CorrelationId = properties.CorrelationId;
33 |
34 | channel.BasicPublish(exchange: "", routingKey: properties.ReplyTo,
35 | basicProperties: replyProerties, body: Encoding.UTF8.GetBytes(result.ToString()));
36 |
37 | channel.BasicAck(ea.DeliveryTag, false);
38 | Console.WriteLine($"Return result: Fib({n})= {result}");
39 |
40 | };
41 | channel.BasicConsume(queue: "rpc_queue", autoAck: false, consumer: consumer);
42 |
43 | Console.ReadLine();
44 | }
45 | }
46 |
47 | }
48 |
49 | private static int Fib(int n)
50 | {
51 | if (n == 0 || n == 1)
52 | {
53 | return n;
54 | }
55 | return Fib(n - 1) + Fib(n - 2);
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/RPCServer/RPCServer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Receive/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using System.Threading;
4 | using RabbitMQ.Client;
5 | using RabbitMQ.Client.Events;
6 |
7 | namespace Receive
8 | {
9 | class Program
10 | {
11 | public static void Main()
12 | {
13 | //1.实例化连接工厂
14 | var factory = new ConnectionFactory() { HostName = "localhost" };
15 | //2. 建立连接
16 | using (var connection = factory.CreateConnection())
17 | {
18 | //3. 创建信道
19 | using (var channel = connection.CreateModel())
20 | {
21 | //4. 申明队列
22 | channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
23 |
24 | //设置prefetchCount : 1来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时,不再分配任务。
25 | channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
26 |
27 | //5. 构造消费者实例
28 | var consumer = new EventingBasicConsumer(channel);
29 | //6. 绑定消息接收后的事件委托
30 | consumer.Received += (model, ea) =>
31 | {
32 | var message = Encoding.UTF8.GetString(ea.Body);
33 | Console.WriteLine(" [x] Received {0}", message);
34 | Thread.Sleep(20000);//模拟耗时
35 | Console.WriteLine(" [x] Done");
36 | // 7. 发送消息确认信号(手动消息确认)
37 | channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
38 | };
39 | //8. 启动消费者
40 | //autoAck:true;自动进行消息确认,当消费端接收到消息后,就自动发送ack信号,不管消息是否正确处理完毕
41 | //autoAck:false;关闭自动消息确认,通过调用BasicAck方法手动进行消息确认
42 | channel.BasicConsume(queue: "hello", autoAck: false, consumer: consumer);
43 |
44 | Console.WriteLine(" Press [enter] to exit.");
45 | Console.ReadLine();
46 | }
47 | }
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/Receive/Receive.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Receive/bin/Debug/netcoreapp1.1/Receive.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "receive/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "Receive.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "receive/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/Receive/bin/Debug/netcoreapp1.1/Receive.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Receive/bin/Debug/netcoreapp1.1/Receive.dll
--------------------------------------------------------------------------------
/Receive/bin/Debug/netcoreapp1.1/Receive.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Receive/bin/Debug/netcoreapp1.1/Receive.pdb
--------------------------------------------------------------------------------
/Receive/bin/Debug/netcoreapp1.1/Receive.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/Receive/bin/Debug/netcoreapp1.1/Receive.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/Receive/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 66c8ed7be20dc23ab25523aa1217ef51f23182ee
2 |
--------------------------------------------------------------------------------
/Receive/obj/Debug/netcoreapp1.1/Receive.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("Receive")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("Receive")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("Receive")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/Receive/obj/Debug/netcoreapp1.1/Receive.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | D:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.deps.json
2 | D:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.runtimeconfig.json
3 | D:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.runtimeconfig.dev.json
4 | D:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.dll
5 | D:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.pdb
6 | D:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.AssemblyInfo.cs
7 | D:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.dll
8 | D:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.pdb
9 | D:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.csprojResolveAssemblyReference.cache
10 | f:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.deps.json
11 | f:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.runtimeconfig.json
12 | f:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.runtimeconfig.dev.json
13 | f:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.dll
14 | f:\Coding\RabbitMQ\Receive\bin\Debug\netcoreapp1.1\Receive.pdb
15 | f:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.csprojResolveAssemblyReference.cache
16 | f:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.AssemblyInfo.cs
17 | f:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.dll
18 | f:\Coding\RabbitMQ\Receive\obj\Debug\netcoreapp1.1\Receive.pdb
19 |
--------------------------------------------------------------------------------
/Receive/obj/Debug/netcoreapp1.1/Receive.csprojResolveAssemblyReference.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Receive/obj/Debug/netcoreapp1.1/Receive.csprojResolveAssemblyReference.cache
--------------------------------------------------------------------------------
/Receive/obj/Debug/netcoreapp1.1/Receive.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Receive/obj/Debug/netcoreapp1.1/Receive.dll
--------------------------------------------------------------------------------
/Receive/obj/Debug/netcoreapp1.1/Receive.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Receive/obj/Debug/netcoreapp1.1/Receive.pdb
--------------------------------------------------------------------------------
/Receive/obj/Receive.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | f:\Coding\RabbitMQ\Receive\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Receive/obj/Receive.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/ReceiveLogs/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 | using RabbitMQ.Client.Events;
5 |
6 | namespace ReceiveLogs {
7 | class Program {
8 | static void Main (string[] args) {
9 | var connectionFactory = new ConnectionFactory () { HostName = "localhost" };
10 | using (var connection = connectionFactory.CreateConnection ()) {
11 | using (var channel = connection.CreateModel ()) {
12 | //申明exchange
13 | channel.ExchangeDeclare (exchange: "logs", type: "fanout");
14 | //申明随机队列名称
15 | var queuename = channel.QueueDeclare ().QueueName;
16 | //绑定队列到指定exchange,使用默认路由
17 | channel.QueueBind (queue : queuename, exchange: "logs", routingKey: "");
18 | Console.WriteLine ("[*] Waitting for logs.");
19 | //申明consumer
20 | var consumer = new EventingBasicConsumer (channel);
21 | //绑定消息接收后的事件委托
22 | consumer.Received += (model, ea) => {
23 | var body = ea.Body;
24 | var message = Encoding.UTF8.GetString (body);
25 | Console.WriteLine ("[x] {0}", message);
26 |
27 | };
28 |
29 | channel.BasicConsume (queue : queuename, autoAck : true, consumer : consumer);
30 |
31 | Console.WriteLine (" Press [enter] to exit.");
32 | Console.ReadLine ();
33 | }
34 | }
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/ReceiveLogs/ReceiveLogs.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ReceiveLogs/bin/Debug/netcoreapp1.1/ReceiveLogs.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "receivelogs/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "ReceiveLogs.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "receivelogs/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/ReceiveLogs/bin/Debug/netcoreapp1.1/ReceiveLogs.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/ReceiveLogs/bin/Debug/netcoreapp1.1/ReceiveLogs.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/ReceiveLogs/logs_from_rabbit.log:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/ReceiveLogs/logs_from_rabbit.log
--------------------------------------------------------------------------------
/ReceiveLogs/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 029c06a69d3a13d0ea9c274c073959284fd76ce5
2 |
--------------------------------------------------------------------------------
/ReceiveLogs/obj/Debug/netcoreapp1.1/ReceiveLogs.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("ReceiveLogs")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("ReceiveLogs")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("ReceiveLogs")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/ReceiveLogs/obj/Debug/netcoreapp1.1/ReceiveLogs.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | F:\Coding\RabbitMQ\receivelogs\bin\Debug\netcoreapp1.1\ReceiveLogs.deps.json
2 | F:\Coding\RabbitMQ\receivelogs\bin\Debug\netcoreapp1.1\ReceiveLogs.runtimeconfig.json
3 | F:\Coding\RabbitMQ\receivelogs\bin\Debug\netcoreapp1.1\ReceiveLogs.runtimeconfig.dev.json
4 | F:\Coding\RabbitMQ\receivelogs\bin\Debug\netcoreapp1.1\ReceiveLogs.dll
5 | F:\Coding\RabbitMQ\receivelogs\bin\Debug\netcoreapp1.1\ReceiveLogs.pdb
6 | F:\Coding\RabbitMQ\receivelogs\obj\Debug\netcoreapp1.1\ReceiveLogs.AssemblyInfo.cs
7 | F:\Coding\RabbitMQ\receivelogs\obj\Debug\netcoreapp1.1\ReceiveLogs.dll
8 | F:\Coding\RabbitMQ\receivelogs\obj\Debug\netcoreapp1.1\ReceiveLogs.pdb
9 |
--------------------------------------------------------------------------------
/ReceiveLogs/obj/ReceiveLogs.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | F:\Coding\RabbitMQ\receivelogs\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ReceiveLogs/obj/ReceiveLogs.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 | using RabbitMQ.Client.Events;
5 |
6 | namespace ReceiveLogsDirect
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | using (var connection = factory.CreateConnection())
14 | using (var channel = connection.CreateModel())
15 | {
16 | channel.ExchangeDeclare(exchange: "direct_logs",
17 | type: "direct");
18 | var queueName = channel.QueueDeclare().QueueName;
19 |
20 | if (args.Length < 1)
21 | {
22 | Console.Error.WriteLine("Use one of parameters: [info] [warning] [error]");
23 | Console.WriteLine(" Press [enter] to exit.");
24 | Console.ReadLine();
25 | Environment.Exit(1);
26 | return;
27 | }
28 |
29 | foreach (var logLevel in args)
30 | {
31 | channel.QueueBind(queue: queueName,
32 | exchange: "direct_logs",
33 | routingKey: logLevel);
34 | }
35 |
36 | Console.WriteLine(" [*] Waiting for messages.");
37 |
38 | var consumer = new EventingBasicConsumer(channel);
39 | consumer.Received += (model, ea) =>
40 | {
41 | var body = ea.Body;
42 | var message = Encoding.UTF8.GetString(body);
43 | var routingKey = ea.RoutingKey;
44 | Console.WriteLine(" [x] Received '{0}':'{1}'", routingKey, message);
45 |
46 | channel.BasicAck(ea.DeliveryTag,false);
47 | };
48 |
49 | channel.BasicConsume(queue: queueName,
50 | autoAck: false,
51 | consumer: consumer);
52 |
53 | Console.WriteLine(" Press [enter] to exit.");
54 | Console.ReadLine();
55 | }
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/ReceiveLogsDirect.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/bin/Debug/netcoreapp1.1/ReceiveLogsDirect.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "receivelogsdirect/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "ReceiveLogsDirect.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "receivelogsdirect/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/ReceiveLogsDirect/bin/Debug/netcoreapp1.1/ReceiveLogsDirect.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/ReceiveLogsDirect/bin/Debug/netcoreapp1.1/ReceiveLogsDirect.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/ReceiveLogsDirect/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | b9f4654a722c4d7386911e55aa9a47809bf5e237
2 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/obj/Debug/netcoreapp1.1/ReceiveLogsDirect.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("ReceiveLogsDirect")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("ReceiveLogsDirect")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("ReceiveLogsDirect")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/obj/Debug/netcoreapp1.1/ReceiveLogsDirect.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | F:\Coding\RabbitMQ\receivelogsdirect\bin\Debug\netcoreapp1.1\ReceiveLogsDirect.deps.json
2 | F:\Coding\RabbitMQ\receivelogsdirect\bin\Debug\netcoreapp1.1\ReceiveLogsDirect.runtimeconfig.json
3 | F:\Coding\RabbitMQ\receivelogsdirect\bin\Debug\netcoreapp1.1\ReceiveLogsDirect.runtimeconfig.dev.json
4 | F:\Coding\RabbitMQ\receivelogsdirect\bin\Debug\netcoreapp1.1\ReceiveLogsDirect.dll
5 | F:\Coding\RabbitMQ\receivelogsdirect\bin\Debug\netcoreapp1.1\ReceiveLogsDirect.pdb
6 | F:\Coding\RabbitMQ\receivelogsdirect\obj\Debug\netcoreapp1.1\ReceiveLogsDirect.AssemblyInfo.cs
7 | F:\Coding\RabbitMQ\receivelogsdirect\obj\Debug\netcoreapp1.1\ReceiveLogsDirect.dll
8 | F:\Coding\RabbitMQ\receivelogsdirect\obj\Debug\netcoreapp1.1\ReceiveLogsDirect.pdb
9 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/obj/ReceiveLogsDirect.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | F:\Coding\RabbitMQ\ReceiveLogsDirect\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ReceiveLogsDirect/obj/ReceiveLogsDirect.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/ReceiveLogsTopic/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 | using RabbitMQ.Client.Events;
5 |
6 | namespace ReceiveLogsTopic
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | using (var connection = factory.CreateConnection())
14 | {
15 | using (var channel = connection.CreateModel())
16 | {
17 | channel.ExchangeDeclare(exchange: "topic_logs", type: "topic");
18 | var queuename = channel.QueueDeclare().QueueName;
19 | if (args.Length < 1)
20 | {
21 | Console.Error.WriteLine("Specify binding_key");
22 | Environment.Exit(1);
23 | return;
24 | }
25 | foreach (var bindingKey in args)
26 | {
27 | channel.QueueBind(queue: queuename, exchange: "topic_logs", routingKey: bindingKey);
28 | }
29 |
30 | Console.WriteLine("[*] Waiting for message.");
31 |
32 | var consumer = new EventingBasicConsumer(channel);
33 | consumer.Received += (model, ea) =>
34 | {
35 | var body = ea.Body;
36 | var message = Encoding.UTF8.GetString(body);
37 | var routingKey = ea.RoutingKey;
38 | Console.WriteLine("[x] Received '{0}':'{1}'", ea.RoutingKey, message);
39 | };
40 |
41 | channel.BasicConsume(queue: queuename, autoAck: true, consumer: consumer);
42 |
43 | Console.WriteLine("Press any key exit.");
44 | Console.ReadLine();
45 | }
46 | }
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/ReceiveLogsTopic/ReceiveLogsTopic.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Send/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RabbitMQ.Client;
4 |
5 | namespace Send
6 | {
7 | class Program
8 | {
9 | public static void Main(string[] args)
10 | {
11 | //1.1.实例化连接工厂
12 | var factory = new ConnectionFactory() { HostName = "localhost" };
13 | //2. 建立连接
14 | using (var connection = factory.CreateConnection())
15 | {
16 | //3. 创建信道
17 | using (var channel = connection.CreateModel())
18 | {
19 | //4. 申明队列(指定durable:true,告知rabbitmq对消息进行持久化)
20 | channel.QueueDeclare(queue: "hello", durable: true, exclusive: false, autoDelete: false, arguments: null);
21 |
22 | //将消息标记为持久性 - 将IBasicProperties.SetPersistent设置为true
23 | var properties = channel.CreateBasicProperties();
24 | properties.Persistent = true;
25 |
26 | //5. 构建byte消息数据包
27 | string message = args.Length > 0 ? args[0] : "Hello RabbitMQ!";
28 | var body = Encoding.UTF8.GetBytes(message);
29 |
30 | //6. 发送数据包(指定basicProperties)
31 | channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: properties, body: body);
32 | Console.WriteLine(" [x] Sent {0}", message);
33 | }
34 | }
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/Send/Send.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Send/bin/Debug/netcoreapp1.1/Send.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "send/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "Send.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "send/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/Send/bin/Debug/netcoreapp1.1/Send.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Send/bin/Debug/netcoreapp1.1/Send.dll
--------------------------------------------------------------------------------
/Send/bin/Debug/netcoreapp1.1/Send.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Send/bin/Debug/netcoreapp1.1/Send.pdb
--------------------------------------------------------------------------------
/Send/bin/Debug/netcoreapp1.1/Send.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/Send/bin/Debug/netcoreapp1.1/Send.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/Send/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 70308cda1925a6242aadcecefeb03786ea61d35c
2 |
--------------------------------------------------------------------------------
/Send/obj/Debug/netcoreapp1.1/Send.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("Send")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("Send")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("Send")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/Send/obj/Debug/netcoreapp1.1/Send.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | D:\Coding\RabbitMQ\Send\bin\Debug\netcoreapp1.1\Send.deps.json
2 | D:\Coding\RabbitMQ\Send\bin\Debug\netcoreapp1.1\Send.runtimeconfig.json
3 | D:\Coding\RabbitMQ\Send\bin\Debug\netcoreapp1.1\Send.runtimeconfig.dev.json
4 | D:\Coding\RabbitMQ\Send\bin\Debug\netcoreapp1.1\Send.dll
5 | D:\Coding\RabbitMQ\Send\bin\Debug\netcoreapp1.1\Send.pdb
6 | D:\Coding\RabbitMQ\Send\obj\Debug\netcoreapp1.1\Send.AssemblyInfo.cs
7 | D:\Coding\RabbitMQ\Send\obj\Debug\netcoreapp1.1\Send.dll
8 | D:\Coding\RabbitMQ\Send\obj\Debug\netcoreapp1.1\Send.pdb
9 | F:\Coding\RabbitMQ\send\bin\Debug\netcoreapp1.1\Send.deps.json
10 | F:\Coding\RabbitMQ\send\bin\Debug\netcoreapp1.1\Send.runtimeconfig.json
11 | F:\Coding\RabbitMQ\send\bin\Debug\netcoreapp1.1\Send.runtimeconfig.dev.json
12 | F:\Coding\RabbitMQ\send\bin\Debug\netcoreapp1.1\Send.dll
13 | F:\Coding\RabbitMQ\send\bin\Debug\netcoreapp1.1\Send.pdb
14 | F:\Coding\RabbitMQ\send\obj\Debug\netcoreapp1.1\Send.AssemblyInfo.cs
15 | F:\Coding\RabbitMQ\send\obj\Debug\netcoreapp1.1\Send.dll
16 | F:\Coding\RabbitMQ\send\obj\Debug\netcoreapp1.1\Send.pdb
17 |
--------------------------------------------------------------------------------
/Send/obj/Debug/netcoreapp1.1/Send.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Send/obj/Debug/netcoreapp1.1/Send.dll
--------------------------------------------------------------------------------
/Send/obj/Debug/netcoreapp1.1/Send.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Send/obj/Debug/netcoreapp1.1/Send.pdb
--------------------------------------------------------------------------------
/Send/obj/Send.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | f:\Coding\RabbitMQ\Send\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Send/obj/Send.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------
/Worker/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using System.Threading;
4 | using RabbitMQ.Client;
5 | using RabbitMQ.Client.Events;
6 |
7 | namespace Worker {
8 | class Program {
9 | public static void Main () {
10 | //1.创建基于本地的连接工厂
11 | var factory = new ConnectionFactory () { HostName = "localhost" };
12 | //2. 建立连接
13 | using (var connection = factory.CreateConnection ()) {
14 | //3. 创建频道
15 | using (var channel = connection.CreateModel ()) {
16 | //4. 申明队列(指定durable:true,告知rabbitmq对消息进行持久化)
17 | channel.QueueDeclare (queue: "work_queue", durable : true, exclusive : false, autoDelete : false, arguments : null);
18 |
19 | //设置prefetchCount : 1来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时,不再分配任务。
20 | channel.BasicQos (prefetchSize : 0, prefetchCount : 1, global : false);
21 |
22 | //5. 构造消费者实例
23 | var consumer = new EventingBasicConsumer (channel);
24 | //6. 绑定消息接收后的事件委托
25 | consumer.Received += (model, ea) => {
26 | var body = ea.Body;
27 | var message = Encoding.UTF8.GetString (body);
28 | Console.WriteLine (" [x] Received {0}", message);
29 |
30 | //7. 模拟耗时
31 | int dots = message.Split ('.').Length - 1;
32 | Thread.Sleep (dots * 1000);
33 | Console.WriteLine (" [x] Done");
34 |
35 | // 8. 发送消息确认信号(手动消息确认)
36 | channel.BasicAck (deliveryTag : ea.DeliveryTag, multiple : false);
37 | };
38 | //9. 启动消费者
39 | //autoAck:true;自动进行消息确认,当消费端接收到消息后,就自动发送ack信号,不管消息是否正确处理完毕
40 | //autoAck:false;关闭自动消息确认,通过调用BasicAck方法手动进行消息确认
41 | channel.BasicConsume (queue: "work_queue", autoAck : false, consumer : consumer);
42 |
43 | Console.WriteLine (" Press [enter] to exit.");
44 | Console.ReadLine ();
45 | }
46 | }
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/Worker/Worker.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | netcoreapp1.1
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Worker/bin/Debug/netcoreapp1.1/Worker.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v1.1",
4 | "signature": "9065a48d8cb247e2416f9860c0c89be839e670e2"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v1.1": {
9 | "worker/1.0.0": {
10 | "dependencies": {
11 | "RabbitMQ.Client": "5.0.0"
12 | },
13 | "runtime": {
14 | "Worker.dll": {}
15 | }
16 | },
17 | "rabbitmq.client/5.0.0": {
18 | "runtime": {
19 | "lib/netstandard1.5/RabbitMQ.Client.dll": {}
20 | }
21 | }
22 | }
23 | },
24 | "libraries": {
25 | "worker/1.0.0": {
26 | "type": "project",
27 | "serviceable": false,
28 | "sha512": ""
29 | },
30 | "rabbitmq.client/5.0.0": {
31 | "type": "package",
32 | "serviceable": true,
33 | "sha512": "sha512-1yiNnrQXtFOwsOBi8UeKXW8teN8r0Opjud0gGDQZnTquKmDR2iyyn5Cap/3UdgVmQsacY7tyXnOgReXJ72HrVA==",
34 | "path": "rabbitmq.client/5.0.0",
35 | "hashPath": "rabbitmq.client.5.0.0.nupkg.sha512"
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/Worker/bin/Debug/netcoreapp1.1/Worker.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Worker/bin/Debug/netcoreapp1.1/Worker.dll
--------------------------------------------------------------------------------
/Worker/bin/Debug/netcoreapp1.1/Worker.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Worker/bin/Debug/netcoreapp1.1/Worker.pdb
--------------------------------------------------------------------------------
/Worker/bin/Debug/netcoreapp1.1/Worker.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\Administrator\\.nuget\\packages"
5 | ]
6 | }
7 | }
--------------------------------------------------------------------------------
/Worker/bin/Debug/netcoreapp1.1/Worker.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "framework": {
4 | "name": "Microsoft.NETCore.App",
5 | "version": "1.1.1"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/Worker/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 417f3cec26844c01c2d0dfb503aa8060094c2a82
2 |
--------------------------------------------------------------------------------
/Worker/obj/Debug/netcoreapp1.1/Worker.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Generated by the MSBuild WriteCodeFragment class.
2 |
3 | using System;
4 | using System.Reflection;
5 |
6 | [assembly: System.Reflection.AssemblyCompanyAttribute("Worker")]
7 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
8 | [assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
9 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
10 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
11 | [assembly: System.Reflection.AssemblyProductAttribute("Worker")]
12 | [assembly: System.Reflection.AssemblyTitleAttribute("Worker")]
13 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
14 |
--------------------------------------------------------------------------------
/Worker/obj/Debug/netcoreapp1.1/Worker.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | D:\Coding\RabbitMQ\Worker\bin\Debug\netcoreapp1.1\Worker.deps.json
2 | D:\Coding\RabbitMQ\Worker\bin\Debug\netcoreapp1.1\Worker.runtimeconfig.json
3 | D:\Coding\RabbitMQ\Worker\bin\Debug\netcoreapp1.1\Worker.runtimeconfig.dev.json
4 | D:\Coding\RabbitMQ\Worker\obj\Debug\netcoreapp1.1\Worker.AssemblyInfo.cs
5 | D:\Coding\RabbitMQ\Worker\bin\Debug\netcoreapp1.1\Worker.dll
6 | D:\Coding\RabbitMQ\Worker\bin\Debug\netcoreapp1.1\Worker.pdb
7 | D:\Coding\RabbitMQ\Worker\obj\Debug\netcoreapp1.1\Worker.dll
8 | D:\Coding\RabbitMQ\Worker\obj\Debug\netcoreapp1.1\Worker.pdb
9 | F:\Coding\RabbitMQ\worker\bin\Debug\netcoreapp1.1\Worker.deps.json
10 | F:\Coding\RabbitMQ\worker\bin\Debug\netcoreapp1.1\Worker.runtimeconfig.json
11 | F:\Coding\RabbitMQ\worker\bin\Debug\netcoreapp1.1\Worker.runtimeconfig.dev.json
12 | F:\Coding\RabbitMQ\worker\bin\Debug\netcoreapp1.1\Worker.dll
13 | F:\Coding\RabbitMQ\worker\bin\Debug\netcoreapp1.1\Worker.pdb
14 | F:\Coding\RabbitMQ\worker\obj\Debug\netcoreapp1.1\Worker.AssemblyInfo.cs
15 | F:\Coding\RabbitMQ\worker\obj\Debug\netcoreapp1.1\Worker.dll
16 | F:\Coding\RabbitMQ\worker\obj\Debug\netcoreapp1.1\Worker.pdb
17 |
--------------------------------------------------------------------------------
/Worker/obj/Debug/netcoreapp1.1/Worker.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Worker/obj/Debug/netcoreapp1.1/Worker.dll
--------------------------------------------------------------------------------
/Worker/obj/Debug/netcoreapp1.1/Worker.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sheng-jie/RabbitMQ/10be7b9f144b7dbeeb27b8806d26da89f2501ad3/Worker/obj/Debug/netcoreapp1.1/Worker.pdb
--------------------------------------------------------------------------------
/Worker/obj/Worker.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | f:\Coding\RabbitMQ\Worker\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\Administrator\.nuget\packages\
9 | PackageReference
10 | 4.0.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Worker/obj/Worker.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
--------------------------------------------------------------------------------