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
f2cfaf16
Commit
f2cfaf16
authored
Jun 27, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add KafkaOptions and remove old options in CAP project.
parent
80406124
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
96 additions
and
16 deletions
+96
-16
CAP.BuilderExtensions.cs
src/DotNetCore.CAP.Kafka/CAP.BuilderExtensions.cs
+33
-0
CAP.KafkaOptions.cs
src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs
+33
-0
DotNetCore.CAP.Kafka.csproj
src/DotNetCore.CAP.Kafka/DotNetCore.CAP.Kafka.csproj
+4
-0
IProcessor.KafkaJobProcessor.cs
src/DotNetCore.CAP.Kafka/IProcessor.KafkaJobProcessor.cs
+10
-7
KafkaConsumerClient.cs
src/DotNetCore.CAP.Kafka/KafkaConsumerClient.cs
+4
-6
KafkaConsumerClientFactory.cs
src/DotNetCore.CAP.Kafka/KafkaConsumerClientFactory.cs
+12
-3
No files found.
src/DotNetCore.CAP.Kafka/
Microsoft.Extensions.DependencyInjection/Consistency
BuilderExtensions.cs
→
src/DotNetCore.CAP.Kafka/
CAP.
BuilderExtensions.cs
View file @
f2cfaf16
using
DotNetCore.CAP
;
using
System
;
using
DotNetCore.CAP
;
using
DotNetCore.CAP.Job
;
using
DotNetCore.CAP.Job
;
using
DotNetCore.CAP.Kafka
;
using
DotNetCore.CAP.Kafka
;
namespace
Microsoft.Extensions.DependencyInjection
namespace
Microsoft.Extensions.DependencyInjection
{
{
public
static
class
ConsistencyBuilderExtensions
/// <summary>
/// Contains extension methods to <see cref="CapBuilder"/> for adding kafka service.
/// </summary>
public
static
class
CapBuilderExtensions
{
{
public
static
CapBuilder
AddKafka
(
this
CapBuilder
builder
)
/// <summary>
/// Adds an Kafka implementation of CAP messages queue.
/// </summary>
/// <param name="builder">The <see cref="CapBuilder"/> instance this method extends</param>
/// <param name="setupAction">An action to configure the <see cref="KafkaOptions"/>.</param>
/// <returns>An <see cref="CapBuilder"/> for creating and configuring the CAP system.</returns>
public
static
CapBuilder
AddKafka
(
this
CapBuilder
builder
,
Action
<
KafkaOptions
>
setupAction
)
{
{
if
(
setupAction
==
null
)
throw
new
ArgumentNullException
(
nameof
(
setupAction
));
builder
.
Services
.
Configure
(
setupAction
);
builder
.
Services
.
AddSingleton
<
IConsumerClientFactory
,
KafkaConsumerClientFactory
>();
builder
.
Services
.
AddSingleton
<
IConsumerClientFactory
,
KafkaConsumerClientFactory
>();
builder
.
Services
.
AddTransient
<
IJobProcessor
,
KafkaJobProcessor
>();
builder
.
Services
.
AddTransient
<
IJobProcessor
,
KafkaJobProcessor
>();
...
...
src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs
0 → 100644
View file @
f2cfaf16
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
DotNetCore.CAP.Kafka
{
public
class
KafkaOptions
{
/// <summary>
/// Gets the Kafka broker id.
/// </summary>
public
int
BrokerId
{
get
;
}
/// <summary>
/// Gets the Kafka broker hostname.
/// </summary>
public
string
Host
{
get
;
}
/// <summary>
/// Gets the Kafka broker port.
/// </summary>
public
int
Port
{
get
;
}
/// <summary>
/// Returns a JSON representation of the BrokerMetadata object.
/// </summary>
/// <returns>
/// A JSON representation of the BrokerMetadata object.
/// </returns>
public
override
string
ToString
()
=>
$"
{{
\
"BrokerId\": {BrokerId}, \"Host\": \"{Host}\", \"Port\": {Port} }}"
;
}
}
src/DotNetCore.CAP.Kafka/DotNetCore.CAP.Kafka.csproj
View file @
f2cfaf16
...
@@ -11,6 +11,10 @@
...
@@ -11,6 +11,10 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>
</PropertyGroup>
<ItemGroup>
<None Include="CAP.KafkaOptions.cs" />
</ItemGroup>
<ItemGroup>
<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="0.9.5" />
<PackageReference Include="Confluent.Kafka" Version="0.9.5" />
</ItemGroup>
</ItemGroup>
...
...
src/DotNetCore.CAP.Kafka/IProcessor.
Produce
r.cs
→
src/DotNetCore.CAP.Kafka/IProcessor.
KafkaJobProcesso
r.cs
View file @
f2cfaf16
...
@@ -16,25 +16,27 @@ namespace DotNetCore.CAP.Kafka
...
@@ -16,25 +16,27 @@ namespace DotNetCore.CAP.Kafka
{
{
public
class
KafkaJobProcessor
:
IJobProcessor
public
class
KafkaJobProcessor
:
IJobProcessor
{
{
private
readonly
CapOptions
_options
;
private
readonly
CapOptions
_capOptions
;
private
readonly
KafkaOptions
_kafkaOptions
;
private
readonly
CancellationTokenSource
_cts
;
private
readonly
CancellationTokenSource
_cts
;
private
readonly
IServiceProvider
_provider
;
private
readonly
IServiceProvider
_provider
;
private
readonly
ILogger
_logger
;
private
readonly
ILogger
_logger
;
//internal static readonly AutoResetEvent PulseEvent = new AutoResetEvent(true);
private
TimeSpan
_pollingDelay
;
private
TimeSpan
_pollingDelay
;
public
KafkaJobProcessor
(
public
KafkaJobProcessor
(
IOptions
<
CapOptions
>
options
,
IOptions
<
CapOptions
>
capOptions
,
IOptions
<
KafkaOptions
>
kafkaOptions
,
ILogger
<
KafkaJobProcessor
>
logger
,
ILogger
<
KafkaJobProcessor
>
logger
,
IServiceProvider
provider
)
IServiceProvider
provider
)
{
{
_logger
=
logger
;
_logger
=
logger
;
_options
=
options
.
Value
;
_capOptions
=
capOptions
.
Value
;
_kafkaOptions
=
kafkaOptions
.
Value
;
_provider
=
provider
;
_provider
=
provider
;
_cts
=
new
CancellationTokenSource
();
_cts
=
new
CancellationTokenSource
();
_pollingDelay
=
TimeSpan
.
FromSeconds
(
_
o
ptions
.
PollingDelay
);
_pollingDelay
=
TimeSpan
.
FromSeconds
(
_
capO
ptions
.
PollingDelay
);
}
}
public
bool
Waiting
{
get
;
private
set
;
}
public
bool
Waiting
{
get
;
private
set
;
}
...
@@ -62,7 +64,8 @@ namespace DotNetCore.CAP.Kafka
...
@@ -62,7 +64,8 @@ namespace DotNetCore.CAP.Kafka
var
token
=
GetTokenToWaitOn
(
context
);
var
token
=
GetTokenToWaitOn
(
context
);
}
}
await
WaitHandleEx
.
WaitAnyAsync
(
WaitHandleEx
.
PulseEvent
,
context
.
CancellationToken
.
WaitHandle
,
_pollingDelay
);
await
WaitHandleEx
.
WaitAnyAsync
(
WaitHandleEx
.
PulseEvent
,
context
.
CancellationToken
.
WaitHandle
,
_pollingDelay
);
}
}
finally
finally
{
{
...
@@ -120,7 +123,7 @@ namespace DotNetCore.CAP.Kafka
...
@@ -120,7 +123,7 @@ namespace DotNetCore.CAP.Kafka
{
{
try
try
{
{
var
config
=
new
Dictionary
<
string
,
object
>
{
{
"bootstrap.servers"
,
_
options
.
BrokerUrlLi
st
}
};
var
config
=
new
Dictionary
<
string
,
object
>
{
{
"bootstrap.servers"
,
_
kafkaOptions
.
Ho
st
}
};
using
(
var
producer
=
new
Producer
<
Null
,
string
>(
config
,
null
,
new
StringSerializer
(
Encoding
.
UTF8
)))
using
(
var
producer
=
new
Producer
<
Null
,
string
>(
config
,
null
,
new
StringSerializer
(
Encoding
.
UTF8
)))
{
{
var
message
=
producer
.
ProduceAsync
(
topic
,
null
,
content
).
Result
;
var
message
=
producer
.
ProduceAsync
(
topic
,
null
,
content
).
Result
;
...
...
src/DotNetCore.CAP.Kafka/KafkaConsumerClient.cs
View file @
f2cfaf16
...
@@ -10,19 +10,17 @@ namespace DotNetCore.CAP.Kafka
...
@@ -10,19 +10,17 @@ namespace DotNetCore.CAP.Kafka
public
class
KafkaConsumerClient
:
IConsumerClient
public
class
KafkaConsumerClient
:
IConsumerClient
{
{
private
readonly
string
_groupId
;
private
readonly
string
_groupId
;
private
readonly
string
_bootstrapServers
;
private
readonly
KafkaOptions
_kafkaOptions
;
private
Consumer
<
Null
,
string
>
_consumerClient
;
private
Consumer
<
Null
,
string
>
_consumerClient
;
public
event
EventHandler
<
MessageBase
>
MessageReceieved
;
public
event
EventHandler
<
MessageBase
>
MessageReceieved
;
public
IDeserializer
<
string
>
StringDeserializer
{
get
;
set
;
}
public
IDeserializer
<
string
>
StringDeserializer
{
get
;
set
;
}
public
KafkaConsumerClient
(
string
groupId
,
string
bootstrapServer
s
)
public
KafkaConsumerClient
(
string
groupId
,
KafkaOptions
option
s
)
{
{
_groupId
=
groupId
;
_groupId
=
groupId
;
_bootstrapServers
=
bootstrapServers
;
_kafkaOptions
=
options
;
StringDeserializer
=
new
StringDeserializer
(
Encoding
.
UTF8
);
StringDeserializer
=
new
StringDeserializer
(
Encoding
.
UTF8
);
}
}
...
@@ -60,7 +58,7 @@ namespace DotNetCore.CAP.Kafka
...
@@ -60,7 +58,7 @@ namespace DotNetCore.CAP.Kafka
{
{
var
config
=
new
Dictionary
<
string
,
object
>{
var
config
=
new
Dictionary
<
string
,
object
>{
{
"group.id"
,
_groupId
},
{
"group.id"
,
_groupId
},
{
"bootstrap.servers"
,
_
bootstrapServers
}
{
"bootstrap.servers"
,
_
kafkaOptions
.
Host
}
};
};
_consumerClient
=
new
Consumer
<
Null
,
string
>(
config
,
null
,
StringDeserializer
);
_consumerClient
=
new
Consumer
<
Null
,
string
>(
config
,
null
,
StringDeserializer
);
...
...
src/DotNetCore.CAP.Kafka/KafkaConsumerClientFactory.cs
View file @
f2cfaf16
namespace
DotNetCore.CAP.Kafka
using
Microsoft.Extensions.Options
;
namespace
DotNetCore.CAP.Kafka
{
{
public
class
KafkaConsumerClientFactory
:
IConsumerClientFactory
public
class
KafkaConsumerClientFactory
:
IConsumerClientFactory
{
{
public
IConsumerClient
Create
(
string
groupId
,
string
clientHostAddress
)
private
readonly
KafkaOptions
_kafkaOptions
;
public
KafkaConsumerClientFactory
(
IOptions
<
KafkaOptions
>
kafkaOptions
)
{
{
return
new
KafkaConsumerClient
(
groupId
,
clientHostAddress
);
_kafkaOptions
=
kafkaOptions
.
Value
;
}
public
IConsumerClient
Create
(
string
groupId
)
{
return
new
KafkaConsumerClient
(
groupId
,
_kafkaOptions
);
}
}
}
}
}
}
\ 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