Created
February 12, 2017 14:32
-
-
Save dylanmei/0c592c6acd66ce3b2da2344faf70a58c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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