3

前言

作为消息中间件,Kafka用以服务消息的异步传输
功能和JMS类似:生产者把消息放进队列中,消费者从队列中获取数据
但是实现起来却是完全不一样

Kafka流程结构图

clipboard.png

1.Producer是通过连接Broker来发送消息的
2.Producer是向某个Topic来传递送消息的
3.发送到Topic中的消息是可以给不同Broker处理的
4.Consumer是连接到某个Broker来监听订阅的Topic的
5.Brokers是通过Zookeeper来进行管理的,并互相通知各自的运行情况
6.如果Producer或Consumer连接到的Broker没有相关的Topic的,那么消息会自动路由到相关的Broker, 下一次Producer或者Consumer会自动记住相关的Broker

clipboard.png

1.在发送消息后,每个消息会依次排列到每个Partition
2.消息是可以通过配置来决定要在Partition上保留多久
3.每个消费者可以从不同队列位置来开始消费消息,并且可以重复消费

Kafka安装

下载:

curl -L -O http://mirror.bit.edu.cn/apache/kafka/1.0.0/kafka_2.11-1.0.0.tgz

修改配置

vi kakfa/config/server.properties

日志目录
log.dirs=/tmp/kafka/logs

分配内存(小于256会导致内存溢出)
export KAFKA_HEAP_OPTS="-Xmx256M -Xms128M" 

向zookeeper注册
zookeeper.connect=106.15.205.155:2181

删除数据(防止磁盘爆炸)

vi /config/server.properties

根据存放时间
log.retention.hours=168

根据数据大小
log.segment.bytes=1073741824

启动

bin/kafka-server-start.sh config/server.properties &

关闭

bin/kafka-server-stop.sh

创建一个topic

bin/kafka-topics.sh --create --zookeeper 106.15.205.155:2181 --replication-factor 1 --partitions 1 --topic test

查看所有topic

bin/kafka-topics.sh --list --zookeeper 106.15.205.155:2181

producer发送消息

bin/kafka-console-producer.sh --broker-list 106.15.205.155:9092 --topic test

consumer接收消息

bin/kafka-console-consumer.sh --zookeeper 106.15.205.155:2181 --topic test --from-beginning

删除指定topic

./bin/kafka-topics.sh  --delete --zookeeper 106.15.205.155:2181  --topic test

实际操作代码

使用spring操作kafka

导入maven依赖:

        <!--kafka-->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>

消息生产者:

配置类:


@Configuration
public class KafkaProducerConfig {
    private String bootstrapServers = "IP:9092";

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return new DefaultKafkaProducerFactory<>(configProps);
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
}

发送消息类:

@Component
public class KafkaSender {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void send(){
        kafkaTemplate.send("topic_1","luxiaotao");
    }
}

消息接受者
配置类:

@Configuration
public class KafkaConsumerConfig {

    private String bootstrapServers="IP:9092";


    private String topic = "topic_1";

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        configProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configProps.put(ConsumerConfig.GROUP_ID_CONFIG, topic);
        return new DefaultKafkaConsumerFactory<>(configProps);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());

        return factory;
    }

}

消息接受类:

@Component
public class KafkaReceive {
    @KafkaListener(topics = {"topic_1"})
    public void receive(String content){
        System.out.println("============================="+content+"============================");
    }
}

以上便是kafka的基本安装和使用,谢谢阅读


大叔一枝花
610 声望56 粉丝

Talk is cheap,show me the code