Transfer.Kafka - dotnet-shashlik/shashlik.eventbus GitHub Wiki

Kafka

Nuget: Shashlik.EventBus.Kafka

注册Kafka消息传输

        /// <summary>
        /// 使用配置节点初始化kafak
        /// </summary>
        /// <param name="eventBusBuilder"></param>
        /// <param name="configurationSection"></param>
        /// <returns></returns>
        public static IEventBusBuilder AddKafka(this IEventBusBuilder eventBusBuilder, IConfigurationSection configurationSection);

        /// <summary>
        /// 手动配置kafka数据
        /// </summary>
        /// <param name="eventBusBuilder"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IEventBusBuilder AddKafka(this IEventBusBuilder eventBusBuilder, Action<EventBusKafkaOptions> action);

        /// <summary>
        /// 使用最简化配置server初始化kafka
        /// </summary>
        /// <param name="eventBusBuilder"></param>
        /// <param name="server">kafka bootstrap server</param>
        /// <returns></returns>
        public static IEventBusBuilder AddKafka(this IEventBusBuilder eventBusBuilder, string server);

        /// <summary>
        /// 注册kafak核心服务
        /// </summary>
        /// <param name="eventBusBuilder"></param>
        /// <returns></returns>
        public static IEventBusBuilder AddKafkaCore(this IEventBusBuilder eventBusBuilder)

配置项

    public class EventBusKafkaOptions
    {
        /// <summary>
        /// see: https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md
        /// </summary>
        public IDictionary<string, string> Properties { get; set; } = new Dictionary<string, string>
        {
            {"bootstrap.servers", "localhost"},
            {"enable.auto.offset.store", "false"},
            {"enable.auto.commit", "true"},
            {"auto.offset.reset", "earliest"},
        };
    }

关于kafka的各种配置项,详见https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md

默认配置:

  • "bootstrap.servers": "localhost"
  • "enable.auto.offset.store": "false"
  • "enable.auto.commit": "true"
  • "auto.offset.reset":"earliest"
  • "group.id": {event handler name}

enable.auto.offset.store/enable.auto.commit/auto.offset.reset这几项配置请勿覆盖,否则会影响消息的接收确认机制。

通过配置文件配置,例:

EventBus:
  Kafka:
    Properties:
      "bootstrap.servers": "localhost:9092"
      "allow.auto.create.topics": "true"
  services.AddKafka(Configuration.GetSection("EventBus:Kafka"))
⚠️ **GitHub.com Fallback** ⚠️