Kafka 快速入门

什么是 Kafka Kafka 是一个分布式流处理平台。 可以将其视为一个高吞吐量、容错的消息队列。 它专为处理实时数据流而设计。 概念 主题(Topic): 发布记录的类别或馈送名称。 分区(Partition): 一个主题被分成多个分区,这些分区是有序的、不可变的记录序列。分区实现了并行性和可伸缩性。 生产者(Producer): 将记录发布到 Kafka 主题的应用程序。 消费者(Consumer): 订阅一个或多个主题并处理记录的应用程序。 代理(Broker): Kafka 服务器。代理存储数据。 集群(Cluster): 一起工作的代理组。 副本(Replica): 每个分区可以跨多个代理进行复制,以实现容错。 领导者(Leader): 分区的一个副本被指定为领导者,处理所有读写请求。 追随者(Follower): 分区的其他副本是追随者,从领导者复制数据。 偏移量(Offset): 分配给分区内每个记录的唯一、顺序 ID。消费者使用偏移量跟踪它们在分区中的位置。 消费者组(Consumer Group): 一组消费者一起从一个主题消费记录。每个分区被分配给一个组内的一个消费者。 保留策略(Retention Policy): 定义 Kafka 在删除记录之前保留记录的时间。 ZooKeeper: 用于管理和协调 Kafka 集群(尽管较新版本正在摆脱 ZooKeeper)。 常见应用场景 实时数据管道: 从各种来源摄取和处理数据流。 日志聚合: 将来自多个服务器的日志收集到一个中心位置。 流处理: 构建分析和响应数据流的实时应用程序。 事件溯源: 存储表示应用程序状态更改的事件序列。 消息传递: 应用程序之间可靠、高吞吐量的消息传递。 活动跟踪: 实时跟踪网站或应用程序上的用户活动。 提交日志: 用作分布式数据库的提交日志。 Kafka 在系统设计中的作用 解耦: Kafka 解耦了生产者和消费者,允许它们独立发展。 可伸缩性: Kafka 可以处理大量数据,并通过添加更多代理进行水平扩展。 可靠性: 容错确保数据不会丢失。 缓冲: ...

March 10, 2025 · datewu

Kafka Quick Start

what’s kafka Kafka is a distributed streaming platform. Think of it as a high-throughput, fault-tolerant message queue on steroids. It’s designed for handling real-time data feeds. Concepts Topic: A category or feed name to which records are published. Partition: A topic is divided into partitions, which are ordered, immutable sequences of records. Partitions enable parallelism and scalability. Producer: An application that publishes records to a Kafka topic. Consumer: An application that subscribes to one or more topics and processes the records. ...

March 10, 2025 · datewu

WireGuard VPN 无法访问github

为什么 GitHub 不喜欢我的 MTU 让我来告诉你我花了一整天与我的 WireGuard VPN 搏斗的挫败经历, 以及为什么 GitHub 成了唯一一个不配合的网站。我希望我的经验, 特别是如果你像我一样在 microk8s 环境中运行 WireGuard,能帮你省去一些麻烦。 环境:microk8s、Ubuntu 和 WireGuard 我的环境有点复杂。 我有一个运行在 Ubuntu 24.04.2 LTS 服务器节点(GNU/Linux 6.8.0-54-generic x86_64)上的 microk8s 集群。 我将我的 WireGuard VPN 服务器作为 pod 在这个 microk8s 集群中运行。这增加了一层我最初没有考虑到的网络复杂性。 问题:GitHub 无法访问(但其他一切都很好) 一切似乎都很好。我可以通过我的 WireGuard VPN 访问大多数网站。 Google、YouTube、Twitter——都没问题。但是 https://github.com 就是无法加载。 curl https://github.com 无限期地挂起。我挠着头,想知道是 DNS 问题、防火墙问题,还是其他什么问题。 调试:深入网络 初始测试: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 curl github.com # 有效!返回 301 重定向到 HTTPS curl -v github.com * Host github.com:80 was resolved. * IPv4: 20.27.177.113 * Trying 20.27.177.113:80... * Connected to github.com (20.27.177.113) port 80 > GET / HTTP/1.1 > Host: github.com > User-Agent: curl/8.5.0 > Accept: */* > < HTTP/1.1 301 Moved Permanently < Content-Length: 0 < Location: [https://github.com/](https://github.com/) < * Connection #0 to host github.com left intact curl [https://github.com](https://github.com) # 挂起... 这令人难以置信地困惑。HTTP 有效,但 HTTPS 无效。 但是 Google 和 Twitter 等其他 HTTPS 网站都很好。这表明问题特定于 HTTPS,并且可能与 GitHub 有关。 ...

March 4, 2025 · datewu

It's ALWAYS DNS!

今天前端遇到一个问题,前端部署的[反向代理]](/posts/nginx-proxy/)到后端的upstream一直pending。 timeout? 以为是后端服务压力大,来不及响应,所以更新upstream配置,加上timeout。 立竿见影,没问题了。 1 2 3 4 5 6 7 8 9 10 11 12 location /api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; rewrite ^/api/(.*)$ /$1 break; proxy_pass http://api_server/; proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 10s; } 一段时间后,后端又接受不到前端请求了,nginx一直报错499。 DNS ! 调查了一段时间后发现根本问题是nginx的dns cache机制的问题。 原来后端每次更新k8s后端deployment的时候,也会重建service从而导致 service的 IP发生了变化。 和后端沟通修改了后端更新流水线脚本,不再重建service 后,问题解决 :) dubug force resolve (bad performance) force nginx to resolve DNS (of a dynamic hostname) everytime when doing proxy_pass? 1 2 3 4 5 6 7 server { #... resolver 127.0.0.1; set $backend "http://dynamic.example.com:80"; proxy_pass $backend; #... } resolver ...

May 19, 2022 · datewu

mysql空磁盘挂卷问题

今天在tke上部署mysql pod,以为很简单。结果发现pod一直crash,查看日志发现是挂卷的问题: [ERROR] --initialize specified but the data directory has files in it. Aborting. 解决办法很简单,给mysql加上启动参数--ignore-db-dir=lost+found即可。 mysql 5.7 k8s 1 2 3 4 name: mysql-master image: mysql:5.7 args: - "--ignore-db-dir=lost+found" docker compose 1 2 3 4 5 6 7 version: '3' services: mysql-master: image: mysql:5.7 command: [--ignore-db-dir=lost+found] environment: - MYSQL_ROOT_PASSWORD=root

May 11, 2022 · datewu

循环播放gif图片

install 首先使用macport安装 imagemagick软件包,因为macport是编译安装软件包,所以安装过程会比较久(~9min)。 更加习惯homebrew的可以参考 官网imagemagick download 安装。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ❯ sudo port install imagemagick Password: ---> Computing dependencies for ImageMagickWarning: cltversion: The Command Line Tools are installed, but MacPorts cannot determine the version. Warning: cltversion: For a possible fix, please see: https://trac.macports.org/wiki/ProblemHotlist#reinstall-clt The following dependencies will be installed: aom brotli .... .... . . ---> Activating webp @1.2.1_0 ---> Cleaning webp ---> Fetching archive for ImageMagick ---> Attempting to fetch ImageMagick-6.9.11-60_1+x11.darwin_21.x86_64.tbz2 from https://packages.macports.org/ImageMagick ---> Attempting to fetch ImageMagick-6.9.11-60_1+x11.darwin_21.x86_64.tbz2 from https://pek.cn.packages.macports.org/macports/packages/ImageMagick ---> Attempting to fetch ImageMagick-6.9.11-60_1+x11.darwin_21.x86_64.tbz2 from https://kmq.jp.packages.macports.org/ImageMagick ---> Fetching distfiles for ImageMagick ---> Attempting to fetch ImageMagick-6.9.11-60.tar.xz from https://distfiles.macports.org/ImageMagick ---> Verifying checksums for ImageMagick ---> Extracting ImageMagick ---> Configuring ImageMagick ---> Building ImageMagick ---> Staging ImageMagick into destroot ---> Installing ImageMagick @6.9.11-60_1+x11 ---> Activating ImageMagick @6.9.11-60_1+x11 ---> Cleaning ImageMagick ---> Updating database of binaries ---> Scanning binaries for linking errors ---> No broken files found. ---> No broken ports found. ~ took 8m49s convert 修改gif循环次数,当loop为0时则关闭了gif的循环播放。 ...

November 18, 2021 · datewu

bash简介

本文会不定期更新 :) set 可以使用set命令改变shell脚本默认的执行流程。 比如 set -e 可以使得shell脚本遇到某一条命令出错( echo $? 不为0)时立即退出执行。 1 2 3 4 #/bin/bash set -e false echo you cannot see me, unless you comment out the 'set -e' flag, haha set详细文档 du 1 2 ❯ du -sh Downloads 4.1G Downloads wiki rm 1 rm $0 # 删除当前文件 for cleanup job nohup 在后台执行当前命令: 1 nohup /usr/sbin/script.sh & 默认情况下,进程是前台进程,这时就把Shell给占据了,我们无法进行其他操作,对于那些没有交互的进程, 我们希望将其在后台启动,可以在启动参数的时候加一个’&‘实现这个目的。 1 2 3 4 5 ❯ sleep 5 & [1] 34769 ✦ ❯ [1] + 34769 done sleep 5 ✦ ❯ 进程切换到后台的时候,我们把它称为job。切换到后台时会输出相关job信息,上面&的输出 [1] 34769 : [1]表示job ID 是1,11319表示进程ID是34769。切换到后台的进程,仍然可以用ps命令查看。 ...

November 17, 2021 · datewu

更换brew源

国内网络环境日益恶劣,执行brew update/upgrade花费的时间够我泡好一壶普洱茶。 不过好景不长,谁能想到那么大的普洱茶饼,日积月累一点点的被我喝完了。 哎,怀恋普洱茶呀。 没了普洱茶,我决定换了brew的官方源,给自己节约节约生命。 解决方案 挑挑拣拣一圈之后,我决定使用清华大学开源软件镜像站]的brew源。 按照官网的指引很快就换好了repo,效果很好。 和设置macos DNS servers一样我也整理了个shell脚本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #!/usr/local/bin/bash set_qh() { git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git brew update } # revocer recover() { git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git brew update } a=${1-"check"} # default to check if [ $a = "r" ]; then recover fi if [ $a = "set" ]; then set_qh fi if [ $a = "check" ]; then echo "goping to EXPORT HOMEBREW_BOTTLE_DOMAIN" export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles echo $HOMEBREW_BOTTLE_DOMAIN brew config | grep ORIGIN brew update brew upgrade brew cleanup fi echo "" echo $a successed! 上面的bash脚本支持3个参数 check,set和 recover,默认使用 check参数。 ...

August 14, 2020 · datewu

设置dns

众所周知,修改/etc/resolv.conf配置文件可以配置Linux的dns 解析服务器。 1 2 3 4 [root@VM-8-3-centos ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 183.60.83.19 nameserver 183.60.82.98 那么苹果系统的dns 解析服务器应该在哪里配置呢,也是配置/etc/resolv.conf文件吗? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ➜ ~ cat /etc/resolv.conf # macOS Notice # # This file is not consulted for DNS hostname resolution, address # resolution, or the DNS query routing mechanism used by most # processes on this system. # # To view the DNS configuration used by this system, use: # scutil --dns # # SEE ALSO # dns-sd(1), scutil(8) # # This file is automatically generated. # nameserver 127.0.0.1 nameserver 192.168.1.1 故事 因为新冠的爆发,公司下放了VPN权限,可以在家接公司办公网络办公。 ...

June 10, 2020 · datewu

socat工具

众所周知kubenetest的端口转发功能kubectl port-forward非常实用,可以提高开发人员的debug效率。 其实kubectl port-forward的底层脏活累活都是socat命令在做,kubectl只能算是一个代理商。 socat 端口转发 socat tcp-listen:58812,reuseaddr,fork tcp:localhost:8000把58812端口的流量转发到 8000上。 1 2 3 4 5 6 7 8 9 10 bash-5.1# python3 -m http.server & bash-5.1# socat tcp-listen:58812,reuseaddr,fork tcp:localhost:8000 & bash-5.1# curl -I localhost:58812 HTTP/1.0 200 OK Server: SimpleHTTP/0.6 Python/3.10.3 Date: Thu, 14 Apr 2022 03:19:15 GMT Content-type: text/html; charset=utf-8 Content-Length: 336 从而使得原来无法访问的 localhost:8000端口的服务,现在可以通过*:58812访问到了。 1 2 3 4 5 6 7 bash-5.1# netstat -nlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:58812 0.0.0.0:* LISTEN 10/socat tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 31/python3 Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node PID/Program name Path 其他 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ➜ ~ tldr socat socat Multipurpose relay (SOcket CAT). - Listen to a port, wait for an incoming connection and transfer data to STDIO: socat - TCP-LISTEN:8080,fork - Create a connection to a host and port, transfer data in STDIO to connected host: socat - TCP4:www.example.com:80 - Forward incoming data of a local port to another host and port: socat TCP-LISTEN:80,fork TCP4:www.example.com:80 语法 man 手册 ...

April 14, 2020 · datewu