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
03392c00
Commit
03392c00
authored
May 30, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update kafka proejct
parent
de1715b7
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
4 deletions
+63
-4
Cap.Consistency.Kafka.csproj
src/Cap.Consistency.Kafka/Cap.Consistency.Kafka.csproj
+1
-0
KafkaConsumerClient.cs
src/Cap.Consistency.Kafka/KafkaConsumerClient.cs
+4
-2
KafkaProducerClient.cs
src/Cap.Consistency.Kafka/KafkaProducerClient.cs
+34
-1
KafkaTopicAttribute.cs
src/Cap.Consistency.Kafka/KafkaTopicAttribute.cs
+1
-1
ConsistencyBuilderExtensions.cs
...sions.DependencyInjection/ConsistencyBuilderExtensions.cs
+23
-0
No files found.
src/Cap.Consistency.Kafka/Cap.Consistency.Kafka.csproj
View file @
03392c00
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
<PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<TargetFramework>netstandard1.6</TargetFramework>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
</PropertyGroup>
</PropertyGroup>
<ItemGroup>
<ItemGroup>
...
...
src/Cap.Consistency.Kafka/KafkaConsumerClient.cs
View file @
03392c00
...
@@ -38,6 +38,7 @@ namespace Cap.Consistency.Kafka
...
@@ -38,6 +38,7 @@ namespace Cap.Consistency.Kafka
InitKafkaClient
();
InitKafkaClient
();
}
}
_consumerClient
.
Assignment
.
Add
(
new
TopicPartition
(
topicName
,
partition
));
_consumerClient
.
Assignment
.
Add
(
new
TopicPartition
(
topicName
,
partition
));
_consumerClient
.
Subscribe
(
topicName
);
}
}
public
void
Listening
(
TimeSpan
timeout
)
{
public
void
Listening
(
TimeSpan
timeout
)
{
...
@@ -65,7 +66,8 @@ namespace Cap.Consistency.Kafka
...
@@ -65,7 +66,8 @@ namespace Cap.Consistency.Kafka
private
void
ConsumerClient_OnMessage
(
object
sender
,
Message
<
Null
,
string
>
e
)
{
private
void
ConsumerClient_OnMessage
(
object
sender
,
Message
<
Null
,
string
>
e
)
{
var
message
=
new
DeliverMessage
{
var
message
=
new
DeliverMessage
{
MessageKey
=
e
.
Topic
,
MessageKey
=
e
.
Topic
,
Value
=
e
.
Value
Value
=
e
.
Value
,
Body
=
Encoding
.
UTF8
.
GetBytes
(
e
.
Value
)
};
};
MessageReceieved
?.
Invoke
(
sender
,
message
);
MessageReceieved
?.
Invoke
(
sender
,
message
);
}
}
...
...
src/Cap.Consistency.Kafka/KafkaProducerClient.cs
View file @
03392c00
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Cap.Consistency.Infrastructure
;
using
Cap.Consistency.Producer
;
using
Confluent.Kafka
;
using
Confluent.Kafka.Serialization
;
using
Microsoft.Extensions.Logging
;
using
Microsoft.Extensions.Options
;
namespace
Cap.Consistency.Kafka
namespace
Cap.Consistency.Kafka
{
{
public
class
KafkaProducerClient
public
class
KafkaProducerClient
:
IProducerClient
{
{
private
readonly
ConsistencyOptions
_options
;
private
readonly
ILogger
_logger
;
public
KafkaProducerClient
(
IOptions
<
ConsistencyOptions
>
options
,
ILoggerFactory
loggerFactory
)
{
_options
=
options
.
Value
;
_logger
=
loggerFactory
.
CreateLogger
(
nameof
(
KafkaProducerClient
));
}
public
Task
SendAsync
(
string
topic
,
string
content
)
{
var
config
=
new
Dictionary
<
string
,
object
>
{
{
"bootstrap.servers"
,
_options
.
BrokerUrlList
}
};
try
{
using
(
var
producer
=
new
Producer
<
Null
,
string
>(
config
,
null
,
new
StringSerializer
(
Encoding
.
UTF8
)))
{
var
task
=
producer
.
ProduceAsync
(
topic
,
null
,
content
);
task
.
ContinueWith
(
g
=>
{
_logger
.
LogInformation
(
$"publish message to topic:
{
topic
}
\r\n -------content:
{
content
}
\r\n "
);
});
//producer.Flush(1000);
return
Task
.
CompletedTask
;
}
}
catch
(
Exception
e
)
{
_logger
.
LogError
(
new
EventId
(
1
),
e
,
$"publish message to [topic:
{
topic
}
] ,content:
{
content
}
"
);
return
Task
.
CompletedTask
;
}
}
}
}
}
}
src/Cap.Consistency.Kafka/KafkaTopicAttribute.cs
View file @
03392c00
src/Cap.Consistency.Kafka/Microsoft.Extensions.DependencyInjection/ConsistencyBuilderExtensions.cs
0 → 100644
View file @
03392c00
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
Cap.Consistency
;
using
Cap.Consistency.Consumer
;
using
Cap.Consistency.Kafka
;
using
Cap.Consistency.Producer
;
namespace
Microsoft.Extensions.DependencyInjection
{
public
static
class
ConsistencyBuilderExtensions
{
public
static
ConsistencyBuilder
AddKafka
(
this
ConsistencyBuilder
builder
)
{
builder
.
Services
.
AddSingleton
<
IConsumerClientFactory
,
KafkaConsumerClientFactory
>();
builder
.
Services
.
AddTransient
<
IProducerClient
,
KafkaProducerClient
>();
return
builder
;
}
}
}
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