设为首页 收藏本站
查看: 1200|回复: 0

[经验分享] Kafka常用操作

[复制链接]

尚未签到

发表于 2019-1-31 08:40:00 | 显示全部楼层 |阅读模式
  添加和删除主题(Topic)
  
  你可能使用过手动创建主题或发送消息时自动创建主题这样的操作。但是你并不了解其中的详情。Kafka中的主题可以在主题配置中配置为自动创建的。
  

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 10 --topic my-favourite-topic
Created topic "my-favourite-topic".  

  复制因子“replication-factor”决定了有多少台服务器会复制消息的次数。通过建议有2~3台服务器进行容错,防止一台服务器宕机后数据就会丢失的问题。
  

  分区数决定了同一个主题内的消息被分成多少个日志。分区数对于主题有以下影响。首先,每个服务器上的每个分区必须是完整的。比如你有10个分区组成的数据集,只能有不超过10个的服务器(集群服务器不算)处理。其次,分区数会影响并行的消费者数。详情可参考Kafka消费者部分的描述。
  修改主题
  
  将my-favourite-topic的主题分区修改为40个
  

./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-favourite-topic --partitions 20
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!  注意,改变分区数量并不会改变分区中的数据,但可能会对依赖这些分区的消费者有影响。如果把分区数当作哈希(hash-key)的话,增加分区可能会打乱数据,但是Kafka在任何情况下都不会自动重排数据。
  

  增加配置
  
./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-favourite-topic --config x=y  
    移除配置
  
./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-favourite-topic --deleteConfig x=y     
    删除主题
  
./kafka-topics.sh --zookeeper localhost:2181 --delete --topic my-favourite-topic
Topic my-favourite-topic is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true     
    为防止活动的主题被删除,主题的删除方法默认是禁用的。如果想要删除主题的话,在server.properties中将delete.topic.enable设置为true即可。
  

  
delete.topic.enable=true
  
  优雅的停止服务器
  当集群中的leader服务器停止服务后,Kafka集群会自动检测其它的服务器并选举出新的Leader服务器。因此每次使用Kill武断的停止服务器并不是明智的做法,当服务器停止后,它还会做两件事:
  1. 它会将所有日志同步到磁盘避免下次重启时需要进行日志恢复。
  2. 在迁移其它副本的数据前,将leader服务器的所有分区数据进行迁移。这样使得领导关系传递的更快,将每个分区的不可用时间缩小到毫秒。
  如果不是使用kill结束服务的话,当服务器停止时同步日志是自动的。但是必须将以下属性设置为true,才能主动停止服务。
  

controlled.shutdown.enable=true  记住只有代理托管的所有分区都有副本时,关闭控制器才是有效的。
  

  平衡领导力
  

  当代理服务器停止或宕机时,这种领导关系就会自动转移到其它副本。即便该代理以后重启,它也只是一个从服务器,这样它就不能用于客户端读写数据。
  为避免这种情况,Kafka有一个预定义副本的概念。如果副本集中定义的分区号是1,5,9,那么会按照顺序节点1优先成为leader服务器,节点5和9次之。
  我们可以使用下面的命令试一试。如果不想每次都这么麻烦的话,就把设置auto.leader.rebalance.enable=true
./kafka-preferred-replica-election.sh --zookeeper localhost:2181
Successfully started preferred replica election for partitions Set([__consumer_offsets,32], [__consumer_offsets,16], [__consumer_offsets,49], [__consumer_offsets,44], [my-favourite-topic,17], [my-favourite-topic,8], [test,0], [__consumer_offsets,28], [my-topic,0], [my-favourite-topic,11], [__consumer_offsets,17], [__consumer_offsets,23], [__consumer_offsets,7], [my-favourite-topic,4], [my-favourite-topic,15], [bar,0], [__consumer_offsets,4], [__consumer_offsets,29], [my-favourite-topic,2], [__consumer_offsets,35], [my-favourite-topic,7], [__consumer_offsets,3], [__consumer_offsets,24], [my-favourite-topic,16], [__consumer_offsets,41], [my-favourite-topic,0], [__consumer_offsets,0], [__consumer_offsets,38], [__consumer_offsets,13], [__consumer_offsets,8], [my-favourite-topic,10], [__consumer_offsets,5], [__consumer_offsets,39], [__consumer_offsets,36], [fav-topic,0], [my-favourite-topic,1], [__consumer_offsets,40], [__consumer_offsets,45], [__consumer_offsets,15], [my-favourite-topic,5], [__consumer_offsets,33], [__consumer_offsets,37], [my-favourite-topic,12], [__consumer_offsets,21], [my-replicated-topic,0], [__consumer_offsets,6], [my-favourite-topic,6], [__consumer_offsets,11], [__consumer_offsets,20], [__consumer_offsets,47], [myfavourite,0], [__consumer_offsets,2], [__consumer_offsets,27], [my-favourite-topic,3], [my-favourite-topic,13], [__consumer_offsets,34], [__consumer_offsets,9], [__consumer_offsets,22], [__consumer_offsets,42], [__consumer_offsets,14], [__consumer_offsets,25], [my-favourite-topic,14], [__consumer_offsets,10], [__consumer_offsets,48], [__consumer_offsets,31], [__consumer_offsets,18], [__consumer_offsets,19], [__consumer_offsets,12], [my-favourite-topic,9], [__consumer_offsets,46], [__consumer_offsets,43], [__consumer_offsets,1], [my-favourite-topic,19], [__consumer_offsets,26], [my-favourite-topic,18], [__consumer_offsets,30])  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-669845-1-1.html 上篇帖子: kafka单机部署 下篇帖子: RabbitMq与Kafka的比较
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表