2017-09-13 2 views
0

Ich habe Homecontroller wie unten,Wie mithilfe von kafka-net

#region Properties 

const string topic = "AnotherTestTopic"; 
const string host = "http://localhost:9092"; 

#endregion 


[HttpPost] 
public ActionResult Save(FormCollection form) 
{ 
    var kafkaOptions = new KafkaOptions(new Uri(host)); 

    var brokerRouter = new BrokerRouter(kafkaOptions); 

    var producer = new Producer(brokerRouter); 

    producer.SendMessageAsync(topic, new[] { new Message("Test message") }).Wait(); 

    return RedirectToAction("Index", "Home"); 
} 

I kafka-net dll und meine SendMessageAsync Methode wie unten

public async Task<List<ProduceResponse>> SendMessageAsync(string topic, IEnumerable<Message> messages, Int16 acks = 1, 
      TimeSpan? timeout = null, MessageCodec codec = MessageCodec.CodecNone) 
     { 
      if (_stopToken.IsCancellationRequested) 
       throw new ObjectDisposedException("Cannot send new documents as producer is disposing."); 
      if (timeout == null) timeout = TimeSpan.FromMilliseconds(DefaultAckTimeoutMS); 

      var batch = messages.Select(message => new TopicMessage 
      { 
       Acks = acks, 
       Codec = codec, 
       Timeout = timeout.Value, 
       Topic = topic, 
       Message = message 
      }).ToList(); 

      _asyncCollection.AddRange(batch); 

      await Task.WhenAll(batch.Select(x => x.Tcs.Task)); 

      return batch.Select(topicMessage => topicMessage.Tcs.Task.Result) 
           .Distinct() 
           .ToList(); 
     } 
bin mit neuem Thema in C# erstellen

Frage:

ich lea gerade erst begonnen rning kafka.Ich weiß wirklich nicht, wie ich Thema von C# code erstellen kann. Wie kann ich Thema von C# hinzufügen?

Jede Hilfe wird geschätzt.

Danke.

Antwort

1

Sie können auto.create.topics.enable=true in Ihrer Brokerkonfiguration festlegen, damit Kafka das Thema für Sie erstellen kann, wenn es noch nicht erstellt wurde.

Sie können auch num.partitions und default.replication.factor auf die entsprechenden Werte in Ihrer Broker-Konfiguration setzen.

+0

wo sind die Konfigurationsdateien? –

+0

es ist in Ihrem Broker conf-Verzeichnis; normalerweise 'server.properties' genannt – alirabiee

Verwandte Themen