Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dylanmei
Created February 12, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanmei/0c592c6acd66ce3b2da2344faf70a58c to your computer and use it in GitHub Desktop.
Save dylanmei/0c592c6acd66ce3b2da2344faf70a58c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"crypto/tls"
"crypto/x509"
"github.com/Shopify/sarama"
)
func main() {
sarama.Logger = log.New(os.Stdout, "[Sarama] ", log.LstdFlags)
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Retry.Max = 1
pem, err := ioutil.ReadFile("./ca.pem")
if err != nil {
log.Fatal("Could not load certificate!")
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(pem)
config.Net.TLS.Enable = true
config.Net.TLS.Config = &tls.Config{
RootCAs: certPool,
//MinVersion: tls.VersionTLS10,
//InsecureSkipVerify: true,
}
var broker string
if os.Getenv("BROKER") == "" {
broker = "...:9093"
} else {
broker = os.Getenv("BROKER")
}
producer, err := sarama.NewSyncProducer([]string{broker}, config)
if err != nil {
// Should not reach here
log.Fatalf("Could not create producer. %v", err)
}
defer func() {
if err := producer.Close(); err != nil {
// Should not reach here
panic(err)
}
}()
topic := "hello-logs"
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.StringEncoder(fmt.Sprintf("hello %s", broker)),
}
partition, offset, err := producer.SendMessage(msg)
if err != nil {
log.Fatalf("Could not send message. %v", err)
}
fmt.Printf("Message is stored in topic(%s)/partition(%d)/offset(%d)\n", topic, partition, offset)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment