Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
444de4e9
Commit
444de4e9
authored
Feb 04, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
a614ab1d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
54 deletions
+35
-54
ConsistencyServer.cs
src/Cap.Consistency.Server/ConsistencyServer.cs
+22
-53
IConsistencyServer.cs
src/Cap.Consistency.Server/IConsistencyServer.cs
+9
-0
project.json
src/Cap.Consistency.Server/project.json
+2
-1
BrokerOptions.cs
src/Cap.Consistency/BrokerOptions.cs
+2
-0
No files found.
src/Cap.Consistency.Server/ConsistencyServer.cs
View file @
444de4e9
using
System
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
Cap.Consistency.Server.Internal.Infrastructure
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.Hosting.Server
;
using
Microsoft.AspNetCore.Http.Features
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.Logging
;
using
Microsoft.Extensions.Options
;
using
RdKafka
;
using
System.Text
;
namespace
Cap.Consistency.Server
{
public
class
ConsistencyServer
:
IServer
public
class
ConsistencyServer
:
I
Consistency
Server
{
private
Stack
<
IDisposable
>
_disposables
;
private
readonly
IApplicationLifetime
_applicationLifetime
;
private
readonly
ILogger
_logger
;
private
readonly
IConsumer
_consumer
;
public
ConsistencyServer
(
IOptions
<
ConsistencyServerOptions
>
options
,
IApplicationLifetime
applicationLifetime
,
ILoggerFactory
loggerFactory
)
public
ConsistencyServer
(
IOptions
<
ConsistencyServerOptions
>
options
,
ILoggerFactory
loggerFactory
)
{
if
(
options
==
null
)
{
throw
new
ArgumentNullException
(
nameof
(
options
));
}
if
(
applicationLifetime
==
null
)
{
throw
new
ArgumentNullException
(
nameof
(
applicationLifetime
));
}
if
(
loggerFactory
==
null
)
{
throw
new
ArgumentNullException
(
nameof
(
loggerFactory
));
}
Options
=
options
.
Value
??
new
ConsistencyServerOptions
();
_applicationLifetime
=
applicationLifetime
;
_logger
=
loggerFactory
.
CreateLogger
(
typeof
(
ConsistencyServer
).
GetTypeInfo
().
Namespace
);
_consumer
=
Options
.
ApplicationServices
.
GetService
<
IConsumer
>();
}
public
ConsistencyServerOptions
Options
{
get
;
}
public
IFeatureCollection
Features
{
get
;
set
;
}
public
void
Start
<
TContext
>(
IHttpApplication
<
TContext
>
application
)
{
if
(
_disposables
!=
null
)
{
// The server has already started and/or has not been cleaned up yet
throw
new
InvalidOperationException
(
"Server has already started."
);
}
_disposables
=
new
Stack
<
IDisposable
>();
var
trace
=
new
ConsistencyTrace
(
_logger
);
public
ConsistencyServerOptions
Options
{
get
;
}
_consumer
.
Log
=
trace
;
public
void
Run
()
{
//配置消费者组
var
config
=
new
Config
()
{
GroupId
=
"example-csharp-consumer"
};
using
(
var
consumer
=
new
EventConsumer
(
config
,
"127.0.0.1:9092"
))
{
_disposables
.
Push
(
_consumer
);
var
threadCount
=
Options
.
ThreadCount
;
//注册一个事件
consumer
.
OnMessage
+=
(
obj
,
msg
)
=>
{
string
text
=
Encoding
.
UTF8
.
GetString
(
msg
.
Payload
,
0
,
msg
.
Payload
.
Length
);
Console
.
WriteLine
(
$"Topic:
{
msg
.
Topic
}
Partition:
{
msg
.
Partition
}
Offset:
{
msg
.
Offset
}
{
text
}
"
);
};
if
(
threadCount
<=
0
)
{
throw
new
ArgumentOutOfRangeException
(
nameof
(
threadCount
),
threadCount
,
"ThreadCount must be positive."
);
}
//订阅一个或者多个Topic
consumer
.
Subscribe
(
new
[]
{
"testtopic"
});
try
{
_consumer
.
Start
(
threadCount
);
}
catch
(
Exception
ex
)
{
throw
ex
;
}
}
//启动
consumer
.
Start
();
public
void
Dispose
()
{
if
(
_disposables
!=
null
)
{
while
(
_disposables
.
Count
>
0
)
{
_disposables
.
Pop
().
Dispose
();
}
_disposables
=
null
;
_logger
.
LogInformation
(
"Started consumer..."
);
}
}
}
...
...
src/Cap.Consistency.Server/IConsistencyServer.cs
0 → 100644
View file @
444de4e9
namespace
Cap.Consistency.Server
{
public
interface
IConsistencyServer
{
ConsistencyServerOptions
Options
{
get
;
}
void
Run
();
}
}
\ No newline at end of file
src/Cap.Consistency.Server/project.json
View file @
444de4e9
...
...
@@ -7,7 +7,8 @@
"Microsoft.AspNetCore.Hosting"
:
"1.1.0-*"
,
"Microsoft.Extensions.Logging.Abstractions"
:
"1.1.0-*"
,
"Microsoft.Extensions.Options"
:
"1.1.0"
,
"NETStandard.Library"
:
"1.6.1"
"NETStandard.Library"
:
"1.6.1"
,
"RdKafka"
:
"0.9.2-ci-189"
},
"frameworks"
:
{
"netstandard1.6"
:
{
...
...
src/Cap.Consistency/BrokerOptions.cs
View file @
444de4e9
...
...
@@ -2,5 +2,7 @@
{
public
class
BrokerOptions
{
public
string
HostName
{
get
;
set
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment