为了使用异步的功能,为项目引入kafka组件,提供高可用的mq服务 本次使用单点的方式为例子,集群同理
运行zookeeper容器
docker run -d --name zookeeper -p 2181:2181 -t wurstmeister/zookeeper
运行kafka容器
docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_ZOOKEEPER_CONNECT=ip:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://ip:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092
-v /opt/kafka/config
wurstmeister/kafka
之后修改挂载的server.properties 以及 consumer.properties
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# see org.apache.kafka.clients.consumer.ConsumerConfig for more details
# list of brokers used for bootstrapping knowledge about the rest of the cluster
# format: host1:port1,host2:port2 ...
bootstrap.servers=121.4.112.75:9092
# consumer group id
group.id=miniprogram-group
# What to do when there is no initial offset in Kafka or if the current
# offset does not exist any more on the server: latest, earliest, none
#auto.offset.reset=
重启容器 开放防火墙
docker restart kafka
添加pom依赖
<dependency>
<groupId>cn.org.faster</groupId>
<artifactId>spring-boot-starter-kafka</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
application.yml 添加 kafka配置:
spring
consumer:
group-id: miniprogram-group
enable-auto-commit: true
auto-commit-interval: 3000
bootstrap-servers: ip:9092
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名,转载请标明出处 最后编辑时间为: 2021/10/04 13:10