Commit 98cd5861 authored by yangxiaodong's avatar yangxiaodong

Refactor KafkaOptions

parent f184550a
...@@ -35,7 +35,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore ...@@ -35,7 +35,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore
public async Task<OperateResult> StoreSentMessageAsync(CapSentMessage message) public async Task<OperateResult> StoreSentMessageAsync(CapSentMessage message)
{ {
if (message == null) throw new ArgumentNullException(nameof(message)); if (message == null) throw new ArgumentNullException(nameof(message));
Context.Add(message); Context.Add(message);
await Context.SaveChangesAsync(); await Context.SaveChangesAsync();
return OperateResult.Success; return OperateResult.Success;
......
...@@ -18,7 +18,6 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -18,7 +18,6 @@ namespace Microsoft.Extensions.DependencyInjection
/// <returns>An <see cref="CapBuilder"/> for creating and configuring the CAP system.</returns> /// <returns>An <see cref="CapBuilder"/> for creating and configuring the CAP system.</returns>
public static CapBuilder AddKafka(this CapBuilder builder, Action<KafkaOptions> setupAction) public static CapBuilder AddKafka(this CapBuilder builder, Action<KafkaOptions> setupAction)
{ {
if (setupAction == null) throw new ArgumentNullException(nameof(setupAction)); if (setupAction == null) throw new ArgumentNullException(nameof(setupAction));
builder.Services.Configure(setupAction); builder.Services.Configure(setupAction);
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Linq;
namespace DotNetCore.CAP.Kafka namespace DotNetCore.CAP.Kafka
{ {
/// <summary>
/// Provides programmatic configuration for the CAP kafka project.
/// </summary>
public class KafkaOptions public class KafkaOptions
{ {
/// <summary> public KafkaOptions()
/// Gets the Kafka broker id. {
/// </summary> MainConfig = new Dictionary<string, object>();
public int BrokerId { get; } }
/// <summary> /// <summary>
/// Gets the Kafka broker hostname. /// librdkafka configuration parameters (refer to https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md).
/// <para>
/// Topic configuration parameters are specified via the "default.topic.config" sub-dictionary config parameter.
/// </para>
/// </summary> /// </summary>
public string Host { get; } public IDictionary<string, object> MainConfig { get; private set; }
/// <summary> /// <summary>
/// Gets the Kafka broker port. /// The `bootstrap.servers` item config of `MainConfig`.
/// <para>
/// Initial list of brokers as a CSV list of broker host or host:port.
/// </para>
/// </summary> /// </summary>
public int Port { get; } public string Servers { get; set; }
/// <summary> internal IEnumerable<KeyValuePair<string, object>> AsRdkafkaConfig()
/// Returns a JSON representation of the BrokerMetadata object. {
/// </summary> if (!MainConfig.ContainsKey("bootstrap.servers"))
/// <returns> {
/// A JSON representation of the BrokerMetadata object. if (string.IsNullOrEmpty(Servers))
/// </returns> {
public override string ToString() throw new ArgumentNullException(nameof(Servers));
=> $"{{ \"BrokerId\": {BrokerId}, \"Host\": \"{Host}\", \"Port\": {Port} }}"; }
else
{
MainConfig.Add("bootstrap.servers", Servers);
}
}
return MainConfig.AsEnumerable();
}
} }
} }
\ No newline at end of file
...@@ -123,7 +123,7 @@ namespace DotNetCore.CAP.Kafka ...@@ -123,7 +123,7 @@ namespace DotNetCore.CAP.Kafka
{ {
try try
{ {
var config = new Dictionary<string, object> { { "bootstrap.servers", _kafkaOptions.Host } }; var config = _kafkaOptions.AsRdkafkaConfig();
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;
......
using System; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using Confluent.Kafka; using Confluent.Kafka;
using Confluent.Kafka.Serialization; using Confluent.Kafka.Serialization;
...@@ -56,12 +55,11 @@ namespace DotNetCore.CAP.Kafka ...@@ -56,12 +55,11 @@ namespace DotNetCore.CAP.Kafka
private void InitKafkaClient() private void InitKafkaClient()
{ {
var config = new Dictionary<string, object>{ _kafkaOptions.MainConfig.Add("group.id", _groupId);
{ "group.id", _groupId },
{ "bootstrap.servers", _kafkaOptions.Host }
};
var config = _kafkaOptions.AsRdkafkaConfig();
_consumerClient = new Consumer<Null, string>(config, null, StringDeserializer); _consumerClient = new Consumer<Null, string>(config, null, StringDeserializer);
_consumerClient.OnMessage += ConsumerClient_OnMessage; _consumerClient.OnMessage += ConsumerClient_OnMessage;
} }
......
using System; namespace DotNetCore.CAP.RabbitMQ
using System.Collections.Generic;
using System.Text;
namespace DotNetCore.CAP.RabbitMQ
{ {
public class RabbitMQOptions public class RabbitMQOptions
{ {
...@@ -68,4 +64,4 @@ namespace DotNetCore.CAP.RabbitMQ ...@@ -68,4 +64,4 @@ namespace DotNetCore.CAP.RabbitMQ
/// </summary> /// </summary>
public int Port { get; set; } = -1; public int Port { get; set; } = -1;
} }
} }
\ No newline at end of file
using DotNetCore.CAP.Infrastructure; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options;
namespace DotNetCore.CAP.RabbitMQ namespace DotNetCore.CAP.RabbitMQ
{ {
......
...@@ -7,7 +7,7 @@ namespace DotNetCore.CAP.Infrastructure ...@@ -7,7 +7,7 @@ namespace DotNetCore.CAP.Infrastructure
/// </summary> /// </summary>
public class CapOptions public class CapOptions
{ {
/// <summary> /// <summary>
/// Corn expression for configuring retry cron job. Default is 1 min. /// Corn expression for configuring retry cron job. Default is 1 min.
/// </summary> /// </summary>
public string CronExp { get; set; } = Cron.Minutely(); public string CronExp { get; set; } = Cron.Minutely();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment