一、kafka自带的消费机制

  kafka有个offset的概念,当每个消息被写进去后,都有一个offset,代表他的序号,然后consumer消费该数据之后,隔一段时间,会把自己消费过的消息的offset提交一下,代表我已经消费过了。下次我要是重启,就会继续从上次消费到的offset来继续消费。

  但是当我们直接kill进程了,再重启。这会导致consumer有些消息处理了,但是没来得及提交offset。等重启之后,少数消息就会再次消费一次。

  其他MQ也会有这种重复消费的问题,那么针对这种问题,我们需要从业务角度,考虑它的幂等性。

"D:\Program Files\Java\jdk1.8.0_91\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=60742:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;D:\kafka_212240\target\classes;D:\kafka\kafka_2.12-2.4.0\libs\activation-1.1.1.jar;D:\kafka\kafka_2.12-2.4.0\libs\aopalliance-repackaged-2.5.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\argparse4j-0.7.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\audience-annotations-0.5.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\commons-cli-1.4.jar;D:\kafka\kafka_2.12-2.4.0\libs\commons-lang3-3.8.1.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-api-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-basic-auth-extension-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-file-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-json-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-mirror-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-mirror-client-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-runtime-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\connect-transforms-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\guava-20.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\hk2-api-2.5.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\hk2-locator-2.5.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\hk2-utils-2.5.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-annotations-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-core-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-databind-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-dataformat-csv-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-datatype-jdk8-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-jaxrs-base-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-jaxrs-json-provider-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-module-jaxb-annotations-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-module-paranamer-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jackson-module-scala_2.12-2.10.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jakarta.activation-api-1.2.1.jar;D:\kafka\kafka_2.12-2.4.0\libs\jakarta.annotation-api-1.3.4.jar;D:\kafka\kafka_2.12-2.4.0\libs\jakarta.inject-2.5.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jakarta.ws.rs-api-2.1.5.jar;D:\kafka\kafka_2.12-2.4.0\libs\jakarta.xml.bind-api-2.3.2.jar;D:\kafka\kafka_2.12-2.4.0\libs\javassist-3.22.0-CR2.jar;D:\kafka\kafka_2.12-2.4.0\libs\javax.servlet-api-3.1.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\javax.ws.rs-api-2.1.1.jar;D:\kafka\kafka_2.12-2.4.0\libs\jaxb-api-2.3.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-client-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-common-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-container-servlet-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-container-servlet-core-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-hk2-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-media-jaxb-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jersey-server-2.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-client-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-continuation-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-http-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-io-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-security-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-server-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-servlet-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-servlets-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jetty-util-9.4.20.v20190813.jar;D:\kafka\kafka_2.12-2.4.0\libs\jopt-simple-5.0.4.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-clients-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-log4j-appender-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-streams-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-streams-examples-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-streams-scala_2.12-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-streams-test-utils-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka-tools-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka_2.12-2.4.0-test.jar;D:\kafka\kafka_2.12-2.4.0\libs\kafka_2.12-2.4.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\log4j-1.2.17.jar;D:\kafka\kafka_2.12-2.4.0\libs\lz4-java-1.6.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\maven-artifact-3.6.1.jar;D:\kafka\kafka_2.12-2.4.0\libs\metrics-core-2.2.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-buffer-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-codec-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-common-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-handler-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-resolver-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-transport-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-transport-native-epoll-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\netty-transport-native-unix-common-4.1.42.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\osgi-resource-locator-1.0.1.jar;D:\kafka\kafka_2.12-2.4.0\libs\paranamer-2.8.jar;D:\kafka\kafka_2.12-2.4.0\libs\plexus-utils-3.2.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\reflections-0.9.11.jar;D:\kafka\kafka_2.12-2.4.0\libs\rocksdbjni-5.18.3.jar;D:\kafka\kafka_2.12-2.4.0\libs\scala-collection-compat_2.12-2.1.2.jar;D:\kafka\kafka_2.12-2.4.0\libs\scala-java8-compat_2.12-0.9.0.jar;D:\kafka\kafka_2.12-2.4.0\libs\scala-library-2.12.10.jar;D:\kafka\kafka_2.12-2.4.0\libs\scala-logging_2.12-3.9.2.jar;D:\kafka\kafka_2.12-2.4.0\libs\scala-reflect-2.12.10.jar;D:\kafka\kafka_2.12-2.4.0\libs\slf4j-api-1.7.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\slf4j-log4j12-1.7.28.jar;D:\kafka\kafka_2.12-2.4.0\libs\snappy-java-1.1.7.3.jar;D:\kafka\kafka_2.12-2.4.0\libs\validation-api-2.0.1.Final.jar;D:\kafka\kafka_2.12-2.4.0\libs\zookeeper-3.5.6.jar;D:\kafka\kafka_2.12-2.4.0\libs\zookeeper-jute-3.5.6.jar;D:\kafka\kafka_2.12-2.4.0\libs\zstd-jni-1.4.3-1.jar" KafkaCons
offset = 0, key = 0, value = 0
offset = 1, key = 1, value = 1
offset = 2, key = 2, value = 2
offset = 3, key = 3, value = 3
offset = 4, key = 4, value = 4
offset = 5, key = 5, value = 5
offset = 6, key = 6, value = 6
offset = 7, key = 7, value = 7
offset = 8, key = 8, value = 8
offset = 9, key = 9, value = 9
offset = 10, key = 10, value = 10
offset = 11, key = 11, value = 11
offset = 12, key = 12, value = 12
offset = 13, key = 13, value = 13
offset = 14, key = 14, value = 14
offset = 15, key = 15, value = 15
offset = 16, key = 16, value = 16
offset = 17, key = 17, value = 17
offset = 18, key = 18, value = 18
offset = 19, key = 19, value = 19
offset = 20, key = 20, value = 20
offset = 21, key = 21, value = 21
offset = 22, key = 22, value = 22
offset = 23, key = 23, value = 23
offset = 24, key = 24, value = 24
offset = 25, key = 25, value = 25
offset = 26, key = 26, value = 26
offset = 27, key = 27, value = 27
offset = 28, key = 28, value = 28
offset = 29, key = 29, value = 29
offset = 30, key = 30, value = 30
offset = 31, key = 31, value = 31
offset = 32, key = 32, value = 32
offset = 33, key = 33, value = 33
offset = 34, key = 34, value = 34
offset = 35, key = 35, value = 35
offset = 36, key = 36, value = 36
offset = 37, key = 37, value = 37
offset = 38, key = 38, value = 38
offset = 39, key = 39, value = 39
offset = 40, key = 40, value = 40
offset = 41, key = 41, value = 41
offset = 42, key = 42, value = 42
offset = 43, key = 43, value = 43
offset = 44, key = 44, value = 44
offset = 45, key = 45, value = 45
offset = 46, key = 46, value = 46
offset = 47, key = 47, value = 47
offset = 48, key = 48, value = 48
offset = 49, key = 49, value = 49
offset = 50, key = 50, value = 50
offset = 51, key = 51, value = 51
offset = 52, key = 52, value = 52
offset = 53, key = 53, value = 53
offset = 54, key = 54, value = 54
offset = 55, key = 55, value = 55
offset = 56, key = 56, value = 56
offset = 57, key = 57, value = 57
offset = 58, key = 58, value = 58
offset = 59, key = 59, value = 59
offset = 60, key = 60, value = 60
offset = 61, key = 61, value = 61
offset = 62, key = 62, value = 62
offset = 63, key = 63, value = 63
offset = 64, key = 64, value = 64
offset = 65, key = 65, value = 65
offset = 66, key = 66, value = 66
offset = 67, key = 67, value = 67
offset = 68, key = 68, value = 68
offset = 69, key = 69, value = 69
offset = 70, key = 70, value = 70
offset = 71, key = 71, value = 71
offset = 72, key = 72, value = 72
offset = 73, key = 73, value = 73
offset = 74, key = 74, value = 74
offset = 75, key = 75, value = 75
offset = 76, key = 76, value = 76
offset = 77, key = 77, value = 77
offset = 78, key = 78, value = 78
offset = 79, key = 79, value = 79
offset = 80, key = 80, value = 80
offset = 81, key = 81, value = 81
offset = 82, key = 82, value = 82
offset = 83, key = 83, value = 83
offset = 84, key = 84, value = 84
offset = 85, key = 85, value = 85
offset = 86, key = 86, value = 86
offset = 87, key = 87, value = 87
offset = 88, key = 88, value = 88
offset = 89, key = 89, value = 89
offset = 90, key = 90, value = 90
offset = 91, key = 91, value = 91
offset = 92, key = 92, value = 92
offset = 93, key = 93, value = 93
offset = 94, key = 94, value = 94
offset = 95, key = 95, value = 95
offset = 96, key = 96, value = 96
offset = 97, key = 97, value = 97
offset = 98, key = 98, value = 98
offset = 99, key = 99, value = 99
offset = 100, key = 0, value = 0
offset = 101, key = 1, value = 1
offset = 102, key = 2, value = 2
offset = 103, key = 3, value = 3
offset = 104, key = 4, value = 4
offset = 105, key = 5, value = 5
offset = 106, key = 6, value = 6
offset = 107, key = 7, value = 7
offset = 108, key = 8, value = 8
offset = 109, key = 9, value = 9
offset = 110, key = 10, value = 10
offset = 111, key = 11, value = 11
offset = 112, key = 12, value = 12
offset = 113, key = 13, value = 13
offset = 114, key = 14, value = 14
offset = 115, key = 15, value = 15
offset = 116, key = 16, value = 16
offset = 117, key = 17, value = 17
offset = 118, key = 18, value = 18
offset = 119, key = 19, value = 19
offset = 120, key = 20, value = 20
offset = 121, key = 21, value = 21
offset = 122, key = 22, value = 22
offset = 123, key = 23, value = 23
offset = 124, key = 24, value = 24
offset = 125, key = 25, value = 25
offset = 126, key = 26, value = 26
offset = 127, key = 27, value = 27
offset = 128, key = 28, value = 28
offset = 129, key = 29, value = 29
offset = 130, key = 30, value = 30
offset = 131, key = 31, value = 31
offset = 132, key = 32, value = 32
offset = 133, key = 33, value = 33
offset = 134, key = 34, value = 34
offset = 135, key = 35, value = 35
offset = 136, key = 36, value = 36
offset = 137, key = 37, value = 37
offset = 138, key = 38, value = 38
offset = 139, key = 39, value = 39
offset = 140, key = 40, value = 40
offset = 141, key = 41, value = 41
offset = 142, key = 42, value = 42
offset = 143, key = 43, value = 43
offset = 144, key = 44, value = 44
offset = 145, key = 45, value = 45
offset = 146, key = 46, value = 46
offset = 147, key = 47, value = 47
offset = 148, key = 48, value = 48
offset = 149, key = 49, value = 49
offset = 150, key = 50, value = 50
offset = 151, key = 51, value = 51
offset = 152, key = 52, value = 52
offset = 153, key = 53, value = 53
offset = 154, key = 54, value = 54
offset = 155, key = 55, value = 55
offset = 156, key = 56, value = 56
offset = 157, key = 57, value = 57
offset = 158, key = 58, value = 58
offset = 159, key = 59, value = 59
offset = 160, key = 60, value = 60
offset = 161, key = 61, value = 61
offset = 162, key = 62, value = 62
offset = 163, key = 63, value = 63
offset = 164, key = 64, value = 64
offset = 165, key = 65, value = 65
offset = 166, key = 66, value = 66
offset = 167, key = 67, value = 67
offset = 168, key = 68, value = 68
offset = 169, key = 69, value = 69
offset = 170, key = 70, value = 70
offset = 171, key = 71, value = 71
offset = 172, key = 72, value = 72
offset = 173, key = 73, value = 73
offset = 174, key = 74, value = 74
offset = 175, key = 75, value = 75
offset = 176, key = 76, value = 76
offset = 177, key = 77, value = 77
offset = 178, key = 78, value = 78
offset = 179, key = 79, value = 79
offset = 180, key = 80, value = 80
offset = 181, key = 81, value = 81
offset = 182, key = 82, value = 82
offset = 183, key = 83, value = 83
offset = 184, key = 84, value = 84
offset = 185, key = 85, value = 85
offset = 186, key = 86, value = 86
offset = 187, key = 87, value = 87
offset = 188, key = 88, value = 88
offset = 189, key = 89, value = 89
offset = 190, key = 90, value = 90
offset = 191, key = 91, value = 91
offset = 192, key = 92, value = 92
offset = 193, key = 93, value = 93
offset = 194, key = 94, value = 94
offset = 195, key = 95, value = 95
offset = 196, key = 96, value = 96
offset = 197, key = 97, value = 97
offset = 198, key = 98, value = 98
offset = 199, key = 99, value = 99
offset = 200, key = 0, value = 0
offset = 201, key = 1, value = 1
offset = 202, key = 2, value = 2
offset = 203, key = 3, value = 3
offset = 204, key = 4, value = 4
offset = 205, key = 5, value = 5
offset = 206, key = 6, value = 6
offset = 207, key = 7, value = 7
offset = 208, key = 8, value = 8
offset = 209, key = 9, value = 9
offset = 210, key = 10, value = 10
offset = 211, key = 11, value = 11
offset = 212, key = 12, value = 12
offset = 213, key = 13, value = 13
offset = 214, key = 14, value = 14
offset = 215, key = 15, value = 15
offset = 216, key = 16, value = 16
offset = 217, key = 17, value = 17
offset = 218, key = 18, value = 18
offset = 219, key = 19, value = 19
offset = 220, key = 20, value = 20
offset = 221, key = 21, value = 21
offset = 222, key = 22, value = 22
offset = 223, key = 23, value = 23
offset = 224, key = 24, value = 24
offset = 225, key = 25, value = 25
offset = 226, key = 26, value = 26
offset = 227, key = 27, value = 27
offset = 228, key = 28, value = 28
offset = 229, key = 29, value = 29
offset = 230, key = 30, value = 30
offset = 231, key = 31, value = 31
offset = 232, key = 32, value = 32
offset = 233, key = 33, value = 33
offset = 234, key = 34, value = 34
offset = 235, key = 35, value = 35
offset = 236, key = 36, value = 36
offset = 237, key = 37, value = 37
offset = 238, key = 38, value = 38
offset = 239, key = 39, value = 39
offset = 240, key = 40, value = 40
offset = 241, key = 41, value = 41
offset = 242, key = 42, value = 42
offset = 243, key = 43, value = 43
offset = 244, key = 44, value = 44
offset = 245, key = 45, value = 45
offset = 246, key = 46, value = 46
offset = 247, key = 47, value = 47
offset = 248, key = 48, value = 48
offset = 249, key = 49, value = 49
offset = 250, key = 50, value = 50
offset = 251, key = 51, value = 51
offset = 252, key = 52, value = 52
offset = 253, key = 53, value = 53
offset = 254, key = 54, value = 54
offset = 255, key = 55, value = 55
offset = 256, key = 56, value = 56
offset = 257, key = 57, value = 57
offset = 258, key = 58, value = 58
offset = 259, key = 59, value = 59
offset = 260, key = 60, value = 60
offset = 261, key = 61, value = 61
offset = 262, key = 62, value = 62
offset = 263, key = 63, value = 63
offset = 264, key = 64, value = 64
offset = 265, key = 65, value = 65
offset = 266, key = 66, value = 66
offset = 267, key = 67, value = 67
offset = 268, key = 68, value = 68
offset = 269, key = 69, value = 69
offset = 270, key = 70, value = 70
offset = 271, key = 71, value = 71
offset = 272, key = 72, value = 72
offset = 273, key = 73, value = 73
offset = 274, key = 74, value = 74
offset = 275, key = 75, value = 75
offset = 276, key = 76, value = 76
offset = 277, key = 77, value = 77
offset = 278, key = 78, value = 78
offset = 279, key = 79, value = 79
offset = 280, key = 80, value = 80
offset = 281, key = 81, value = 81
offset = 282, key = 82, value = 82
offset = 283, key = 83, value = 83
offset = 284, key = 84, value = 84
offset = 285, key = 85, value = 85
offset = 286, key = 86, value = 86
offset = 287, key = 87, value = 87
offset = 288, key = 88, value = 88
offset = 289, key = 89, value = 89
offset = 290, key = 90, value = 90
offset = 291, key = 91, value = 91
offset = 292, key = 92, value = 92
offset = 293, key = 93, value = 93
offset = 294, key = 94, value = 94
offset = 295, key = 95, value = 95
offset = 296, key = 96, value = 96
offset = 297, key = 97, value = 97
offset = 298, key = 98, value = 98
offset = 299, key = 99, value = 99
offset = 300, key = 0, value = 0
offset = 301, key = 1, value = 1
offset = 302, key = 2, value = 2
offset = 303, key = 3, value = 3
offset = 304, key = 4, value = 4
offset = 305, key = 5, value = 5
offset = 306, key = 6, value = 6
offset = 307, key = 7, value = 7
offset = 308, key = 8, value = 8
offset = 309, key = 9, value = 9
offset = 310, key = 10, value = 10
offset = 311, key = 11, value = 11
offset = 312, key = 12, value = 12
offset = 313, key = 13, value = 13
offset = 314, key = 14, value = 14
offset = 315, key = 15, value = 15
offset = 316, key = 16, value = 16
offset = 317, key = 17, value = 17
offset = 318, key = 18, value = 18
offset = 319, key = 19, value = 19
offset = 320, key = 20, value = 20
offset = 321, key = 21, value = 21
offset = 322, key = 22, value = 22
offset = 323, key = 23, value = 23
offset = 324, key = 24, value = 24
offset = 325, key = 25, value = 25
offset = 326, key = 26, value = 26
offset = 327, key = 27, value = 27
offset = 328, key = 28, value = 28
offset = 329, key = 29, value = 29
offset = 330, key = 30, value = 30
offset = 331, key = 31, value = 31
offset = 332, key = 32, value = 32
offset = 333, key = 33, value = 33
offset = 334, key = 34, value = 34
offset = 335, key = 35, value = 35
offset = 336, key = 36, value = 36
offset = 337, key = 37, value = 37
offset = 338, key = 38, value = 38
offset = 339, key = 39, value = 39
offset = 340, key = 40, value = 40
offset = 341, key = 41, value = 41
offset = 342, key = 42, value = 42
offset = 343, key = 43, value = 43
offset = 344, key = 44, value = 44
offset = 345, key = 45, value = 45
offset = 346, key = 46, value = 46
offset = 347, key = 47, value = 47
offset = 348, key = 48, value = 48
offset = 349, key = 49, value = 49
offset = 350, key = 50, value = 50
offset = 351, key = 51, value = 51
offset = 352, key = 52, value = 52
offset = 353, key = 53, value = 53
offset = 354, key = 54, value = 54
offset = 355, key = 55, value = 55
offset = 356, key = 56, value = 56
offset = 357, key = 57, value = 57
offset = 358, key = 58, value = 58
offset = 359, key = 59, value = 59
offset = 360, key = 60, value = 60
offset = 361, key = 61, value = 61
offset = 362, key = 62, value = 62
offset = 363, key = 63, value = 63
offset = 364, key = 64, value = 64
offset = 365, key = 65, value = 65
offset = 366, key = 66, value = 66
offset = 367, key = 67, value = 67
offset = 368, key = 68, value = 68
offset = 369, key = 69, value = 69
offset = 370, key = 70, value = 70
offset = 371, key = 71, value = 71
offset = 372, key = 72, value = 72
offset = 373, key = 73, value = 73
offset = 374, key = 74, value = 74
offset = 375, key = 75, value = 75
offset = 376, key = 76, value = 76
offset = 377, key = 77, value = 77
offset = 378, key = 78, value = 78
offset = 379, key = 79, value = 79
offset = 380, key = 80, value = 80
offset = 381, key = 81, value = 81
offset = 382, key = 82, value = 82
offset = 383, key = 83, value = 83
offset = 384, key = 84, value = 84
offset = 385, key = 85, value = 85
offset = 386, key = 86, value = 86
offset = 387, key = 87, value = 87
offset = 388, key = 88, value = 88
offset = 389, key = 89, value = 89
offset = 390, key = 90, value = 90
offset = 391, key = 91, value = 91
offset = 392, key = 92, value = 92
offset = 393, key = 93, value = 93
offset = 394, key = 94, value = 94
offset = 395, key = 95, value = 95
offset = 396, key = 96, value = 96
offset = 397, key = 97, value = 97
offset = 398, key = 98, value = 98
offset = 399, key = 99, value = 99
offset = 400, key = 0, value = 0
offset = 401, key = 1, value = 1
offset = 402, key = 2, value = 2
offset = 403, key = 3, value = 3
offset = 404, key = 4, value = 4
offset = 405, key = 5, value = 5
offset = 406, key = 6, value = 6
offset = 407, key = 7, value = 7
offset = 408, key = 8, value = 8
offset = 409, key = 9, value = 9
offset = 410, key = 10, value = 10
offset = 411, key = 11, value = 11
offset = 412, key = 12, value = 12
offset = 413, key = 13, value = 13
offset = 414, key = 14, value = 14
offset = 415, key = 15, value = 15
offset = 416, key = 16, value = 16
offset = 417, key = 17, value = 17
offset = 418, key = 18, value = 18
offset = 419, key = 19, value = 19
offset = 420, key = 20, value = 20
offset = 421, key = 21, value = 21
offset = 422, key = 22, value = 22
offset = 423, key = 23, value = 23
offset = 424, key = 24, value = 24
offset = 425, key = 25, value = 25
offset = 426, key = 26, value = 26
offset = 427, key = 27, value = 27
offset = 428, key = 28, value = 28
offset = 429, key = 29, value = 29
offset = 430, key = 30, value = 30
offset = 431, key = 31, value = 31
offset = 432, key = 32, value = 32
offset = 433, key = 33, value = 33
offset = 434, key = 34, value = 34
offset = 435, key = 35, value = 35
offset = 436, key = 36, value = 36
offset = 437, key = 37, value = 37
offset = 438, key = 38, value = 38
offset = 439, key = 39, value = 39
offset = 440, key = 40, value = 40
offset = 441, key = 41, value = 41
offset = 442, key = 42, value = 42
offset = 443, key = 43, value = 43
offset = 444, key = 44, value = 44
offset = 445, key = 45, value = 45
offset = 446, key = 46, value = 46
offset = 447, key = 47, value = 47
offset = 448, key = 48, value = 48
offset = 449, key = 49, value = 49
offset = 450, key = 50, value = 50
offset = 451, key = 51, value = 51
offset = 452, key = 52, value = 52
offset = 453, key = 53, value = 53
offset = 454, key = 54, value = 54
offset = 455, key = 55, value = 55
offset = 456, key = 56, value = 56
offset = 457, key = 57, value = 57
offset = 458, key = 58, value = 58
offset = 459, key = 59, value = 59
offset = 460, key = 60, value = 60
offset = 461, key = 61, value = 61
offset = 462, key = 62, value = 62
offset = 463, key = 63, value = 63
offset = 464, key = 64, value = 64
offset = 465, key = 65, value = 65
offset = 466, key = 66, value = 66
offset = 467, key = 67, value = 67
offset = 468, key = 68, value = 68
offset = 469, key = 69, value = 69
offset = 470, key = 70, value = 70
offset = 471, key = 71, value = 71
offset = 472, key = 72, value = 72
offset = 473, key = 73, value = 73
offset = 474, key = 74, value = 74
offset = 475, key = 75, value = 75
offset = 476, key = 76, value = 76
offset = 477, key = 77, value = 77
offset = 478, key = 78, value = 78
offset = 479, key = 79, value = 79
offset = 480, key = 80, value = 80
offset = 481, key = 81, value = 81
offset = 482, key = 82, value = 82
offset = 483, key = 83, value = 83
offset = 484, key = 84, value = 84
offset = 485, key = 85, value = 85
offset = 486, key = 86, value = 86
offset = 487, key = 87, value = 87
offset = 488, key = 88, value = 88
offset = 489, key = 89, value = 89
offset = 490, key = 90, value = 90
offset = 491, key = 91, value = 91
offset = 492, key = 92, value = 92
offset = 493, key = 93, value = 93
offset = 494, key = 94, value = 94
offset = 495, key = 95, value = 95
offset = 496, key = 96, value = 96
offset = 497, key = 97, value = 97
offset = 498, key = 98, value = 98
offset = 499, key = 99, value = 99
offset = 500, key = null, value = hello world
offset = 501, key = null, value = caijunjie 思密达
offset = 502, key = null, value = caijunjie 思密达
offset = 503, key = null, value = caijunjie world

二、通过保证消息队列消费的幂等性来保证

  举个例子,当消费一条消息时就往数据库插入一条数据。如何保证重复消费也插入一条数据呢?

  那么我们就需要从幂等性角度考虑了。幂等性,通俗点说,就一个数据,或者一个请求,无论来多次,对应的数据都不会改变的,不能出错。

 

怎么保证消息队列消费的幂等性?

需要结合业务来思考,比如下面的例子:

  1.比如某个数据要写库,你先根据主键查一下,如果数据有了,就别插入了,update一下好吧

  2.比如你是写redis,那没问题了,反正每次都是set,天然幂等性

  3.对于消息,我们可以建个表(专门存储消息消费记录)

    生产者,发送消息前判断库中是否有记录(有记录说明已发送),没有记录,先入库,状态为待消费,然后发送消息并把主键id带上。

    消费者,接收消息,通过主键ID查询记录表,判断消息状态是否已消费。若没消费过,则处理消息,处理完后,更新消息记录的状态为已消费。

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐