Kafka producer

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}

import org.apache.kafka.common.serialization.StringSerializer

import io.confluent.kafka.serializers.{KafkaAvroSerializer, AbstractKafkaAvroSerDeConfig}

import javax.net.ssl.SSLContext

import org.apache.kafka.common.config.SslConfigs

import org.apache.kafka.common.security.auth.SecurityProtocol

import io.confluent.kafka.ssl.{SslConfig, SslFactory}

 

val props = new Properties()

props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka.example.com:9093")

props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer].getName)

props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[KafkaAvroSerializer].getName)

props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "https://schemaregistry.example.com:8081")

props.put(AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, "USER_INFO")

props.put(AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG, "username:password")

 

val sslConfig = SslConfig(props)

val sslFactory = new SslFactory(sslConfig)

val sslContext = sslFactory.createSSLContext()

 

props.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "")

props.put(SslConfigs.SSL_SOCKET_FACTORY_CLASS_CONFIG, sslFactory.getClass.getName)

props.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, "/path/to/keystore.jks")

props.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "password")

props.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, "/path/to/truststore.jks")

props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "password")

props.put(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG, "TLSv1.2")

 

props.put(ProducerConfig.ACKS_CONFIG, "all")

props.put(ProducerConfig.RETRIES_CONFIG, "10")

props.put(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, "5")

props.put(ProducerConfig.CLIENT_ID_CONFIG, "my-producer")

 

val producer = new KafkaProducer[String, Any](props)

val record = new ProducerRecord[String, Any]("test", "key", Map("field1" -> "value1", "field2" -> 2))

producer.send(record)

 

producer.close()

你可能感兴趣的:(kafka)