KafkaProducerConfig.java 1.03 KB
package cn.com.glsx.kafka.config;

import kafka.javaapi.producer.Producer;
import kafka.producer.ProducerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;


/**
 * 设备上点中转kafak
 *
 * @author yll
 */
@Configuration
public class KafkaProducerConfig {

    @Autowired
    private MqConfig mqConfig;

    @Bean("producer")
    public Producer getProducer() {
        return new Producer(new ProducerConfig(initProperties()));
    }

    private Properties initProperties() {
        Properties properties = new Properties();
        properties.setProperty("serializer.class", "kafka.serializer.DefaultEncoder");
        properties.setProperty("key.serializer.class", "kafka.serializer.DefaultEncoder");
        properties.setProperty("metadata.broker.list", mqConfig.getProducerServers());
        properties.setProperty("producer.type", "async");
        return properties;
    }

}