├── .gitignore
├── .mvn
└── wrapper
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── README.md
├── docker-compose.yml
├── documentation
└── images
│ └── kafka-manager-create-cluster.png
├── mvnw
├── mvnw.cmd
├── pom.xml
├── spring-kafka-consumer
├── .gitignore
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── nl
│ │ │ └── jtim
│ │ │ └── spring
│ │ │ └── kafka
│ │ │ ├── consumer
│ │ │ ├── KafkaController.java
│ │ │ ├── MessageConsumer.java
│ │ │ ├── SpringKafkaConsumerApplication.java
│ │ │ └── config
│ │ │ │ └── KafkaConsumerConfig.java
│ │ │ └── producer
│ │ │ └── randommessage
│ │ │ └── Message.java
│ └── resources
│ │ └── application.yml
│ └── test
│ ├── java
│ └── nl
│ │ └── jtim
│ │ └── spring
│ │ └── kafka
│ │ └── consumer
│ │ └── SpringKafkaConsumerApplicationTests.java
│ └── resources
│ └── application.yml
└── spring-kafka-producer
├── .gitignore
├── pom.xml
└── src
├── main
├── java
│ └── nl
│ │ └── jtim
│ │ └── spring
│ │ └── kafka
│ │ └── producer
│ │ ├── SpringKafkaProducerApplication.java
│ │ ├── config
│ │ └── KafkaProducerConfig.java
│ │ └── randommessage
│ │ ├── Message.java
│ │ ├── RandomMessageGenerator.java
│ │ ├── RandomMessageKafkaProducer.java
│ │ └── RandomMessagePublisher.java
└── resources
│ └── application.yml
└── test
├── java
└── nl
│ └── jtim
│ └── spring
│ └── kafka
│ └── producer
│ └── SpringKafkaProducerApplicationTests.java
└── resources
└── application.yml
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/j-tim/spring-kafka-micrometer/f88a5dc49d14dba71902916be24bf9eec42bfcbe/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Spring Kafka micrometer
2 |
3 | Example project to play around with Spring Kafka Micrometer.
4 |
5 | Versions used in this project:
6 |
7 | * Spring Boot: 2.1.0
8 | * Spring Kafka: 2.2.0.RELEASE
9 | * Micrometer: 1.1.0
10 |
11 | ## How to build the project
12 |
13 | ```
14 | ./mvnw clean package
15 | ```
16 |
17 | ## How to run this project
18 |
19 | Make sure to set the environment variable `DOCKER_HOST_IP` to the ip of the host running the
20 | Docker containers using docker-compose.
21 |
22 | On Linux you can get the correct IP from docker0 interface by executing: ifconfig
23 | In my case `DOCKER_HOST_IP=172.17.0.1`
24 |
25 | If you are using Docker for Mac <= 1.11, or Docker Toolbox for Windows (your docker machine IP is usually 192.168.99.100)
26 |
27 | ```
28 | docker-compose up -d
29 | ```
30 |
31 | Start the producer:
32 |
33 | ```
34 | cd spring-kafka-producer
35 | mvn spring-boot:run
36 | ```
37 |
38 | The producer will publish a Hello World message to topic: hello-world-messages
39 |
40 | Start the consumer:
41 |
42 | ```
43 | cd
44 | mvn spring-boot:run
45 | ```
46 |
47 | How the consumer metrics:
48 |
49 | [http://localhost:8081/metrics/](http://localhost:8081/metrics/)
50 |
51 | Kafka metrics are visible.
52 |
53 | See specific Kafka metrics:
54 |
55 | [http://localhost:8081/actuator/metrics/kafka.consumer.records.consumed.total](http://localhost:8081/actuator/metrics/kafka.consumer.records.consumed.total)
56 |
57 | ```json
58 | {
59 | name: "kafka.consumer.records.consumed.total",
60 | description: "The total number of records consumed.",
61 | baseUnit: "records",
62 | measurements: [
63 | {
64 | statistic: "COUNT",
65 | value: "NaN"
66 | }
67 | ],
68 | availableTags: [
69 | {
70 | tag: 'client.id',
71 | values: [
72 | "spring-kafka-consumer-hello-world-app"
73 | ]
74 | }
75 | ]
76 | }
77 | ```
78 |
79 | My Question why is the value of `COUNT` `NaN`?
80 |
81 | ```json
82 | {
83 | measurements: [
84 | {
85 | statistic: "COUNT",
86 | value: "NaN"
87 | }
88 | ]
89 | }
90 | ```
91 |
92 | This [issue](https://github.com/micrometer-metrics/micrometer/issues/983) has been fixed in Micrometer version 1.1.1 see
93 | [commit](https://github.com/micrometer-metrics/micrometer/commit/e9b3efeb42681120dd46b1145f0d682f8569bc28)
94 |
95 | ## Kafka manager
96 |
97 | Part of the `docker-compose` setup is the Kafka Manager.
98 | To get more insight in the broker, topics and more you can use this tool.
99 |
100 | * Open [http://localhost:9000](http://localhost:9000)
101 | * Create a Kafka cluster view [http://localhost:9000/addCluster](http://localhost:9000/addCluster)
102 |
103 | 
104 |
105 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: '3.7'
2 |
3 | # Please export the environment variable DOCKER_HOST_IP before starting the stack using docker-compose
4 | # On linux make sure you have set DOCKER_HOST_IP=172.17.0.1
5 | # Get the correct IP address from docker0 interface by executing: ifconfig
6 |
7 | # If you are using Docker for Mac <= 1.11, or Docker Toolbox for Windows
8 | # (your docker machine IP is usually 192.168.99.100)
9 |
10 | services:
11 |
12 | # https://hub.docker.com/r/confluentinc/cp-zookeeper/
13 | zookeeper:
14 | container_name: zookeeper
15 | image: confluentinc/cp-zookeeper:5.0.1
16 | environment:
17 | - ZOOKEEPER_CLIENT_PORT=2181
18 | ports:
19 | - 2181:2181
20 |
21 | # https://hub.docker.com/r/confluentinc/cp-kafka/
22 | kafka:
23 | container_name: kafka
24 | image: confluentinc/cp-kafka:5.0.1
25 | environment:
26 | # The KAFKA_ADVERTISED_LISTENERS variable is set to kafka:9092. This will make Kafka accessible to other
27 | # containers by advertising it’s location on the Docker network.
28 | - KAFKA_ADVERTISED_LISTENERS= LISTENER_DOCKER_INTERNAL://kafka:19092,LISTENER_DOCKER_EXTERNAL://${DOCKER_HOST_IP:-127.0.0.1}:9092
29 | - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP= LISTENER_DOCKER_INTERNAL:PLAINTEXT,LISTENER_DOCKER_EXTERNAL:PLAINTEXT
30 | # The same ZooKeeper port is specified here as the previous container.
31 | - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
32 | - KAFKA_INTER_BROKER_LISTENER_NAME= LISTENER_DOCKER_INTERNAL
33 | # The KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR is set to 1 for a single-node cluster. Unless you have three or more
34 | # nodes you do not need to change this from the default.
35 | - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
36 | - KAFKA_DEFAULT_REPLICATION_FACTOR=1
37 | - KAFKA_NUM_PARTITIONS=1
38 | - JMX_PORT=9999
39 | - KAFKA_JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=kafka -Dcom.sun.management.jmxremote.rmi.port=9999
40 | - KAFKA_LOG4J_LOGGERS= "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
41 | ports:
42 | - 9092:9092
43 | - 9999:9999
44 | depends_on:
45 | - zookeeper
46 |
47 | # https://hub.docker.com/r/hlebalbau/kafka-manager/
48 | kafka-manager:
49 | container_name: kafka-manager
50 | image: hlebalbau/kafka-manager:1.3.3.18
51 | environment:
52 | - ZK_HOSTS=zookeeper:2181
53 | ports:
54 | - 9000:9000
55 | depends_on:
56 | - kafka
--------------------------------------------------------------------------------
/documentation/images/kafka-manager-create-cluster.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/j-tim/spring-kafka-micrometer/f88a5dc49d14dba71902916be24bf9eec42bfcbe/documentation/images/kafka-manager-create-cluster.png
--------------------------------------------------------------------------------
/mvnw:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # ----------------------------------------------------------------------------
3 | # Licensed to the Apache Software Foundation (ASF) under one
4 | # or more contributor license agreements. See the NOTICE file
5 | # distributed with this work for additional information
6 | # regarding copyright ownership. The ASF licenses this file
7 | # to you under the Apache License, Version 2.0 (the
8 | # "License"); you may not use this file except in compliance
9 | # with the License. You may obtain a copy of the License at
10 | #
11 | # http://www.apache.org/licenses/LICENSE-2.0
12 | #
13 | # Unless required by applicable law or agreed to in writing,
14 | # software distributed under the License is distributed on an
15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | # KIND, either express or implied. See the License for the
17 | # specific language governing permissions and limitations
18 | # under the License.
19 | # ----------------------------------------------------------------------------
20 |
21 | # ----------------------------------------------------------------------------
22 | # Maven2 Start Up Batch script
23 | #
24 | # Required ENV vars:
25 | # ------------------
26 | # JAVA_HOME - location of a JDK home dir
27 | #
28 | # Optional ENV vars
29 | # -----------------
30 | # M2_HOME - location of maven2's installed home dir
31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven
32 | # e.g. to debug Maven itself, use
33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files
35 | # ----------------------------------------------------------------------------
36 |
37 | if [ -z "$MAVEN_SKIP_RC" ] ; then
38 |
39 | if [ -f /etc/mavenrc ] ; then
40 | . /etc/mavenrc
41 | fi
42 |
43 | if [ -f "$HOME/.mavenrc" ] ; then
44 | . "$HOME/.mavenrc"
45 | fi
46 |
47 | fi
48 |
49 | # OS specific support. $var _must_ be set to either true or false.
50 | cygwin=false;
51 | darwin=false;
52 | mingw=false
53 | case "`uname`" in
54 | CYGWIN*) cygwin=true ;;
55 | MINGW*) mingw=true;;
56 | Darwin*) darwin=true
57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
59 | if [ -z "$JAVA_HOME" ]; then
60 | if [ -x "/usr/libexec/java_home" ]; then
61 | export JAVA_HOME="`/usr/libexec/java_home`"
62 | else
63 | export JAVA_HOME="/Library/Java/Home"
64 | fi
65 | fi
66 | ;;
67 | esac
68 |
69 | if [ -z "$JAVA_HOME" ] ; then
70 | if [ -r /etc/gentoo-release ] ; then
71 | JAVA_HOME=`java-config --jre-home`
72 | fi
73 | fi
74 |
75 | if [ -z "$M2_HOME" ] ; then
76 | ## resolve links - $0 may be a link to maven's home
77 | PRG="$0"
78 |
79 | # need this for relative symlinks
80 | while [ -h "$PRG" ] ; do
81 | ls=`ls -ld "$PRG"`
82 | link=`expr "$ls" : '.*-> \(.*\)$'`
83 | if expr "$link" : '/.*' > /dev/null; then
84 | PRG="$link"
85 | else
86 | PRG="`dirname "$PRG"`/$link"
87 | fi
88 | done
89 |
90 | saveddir=`pwd`
91 |
92 | M2_HOME=`dirname "$PRG"`/..
93 |
94 | # make it fully qualified
95 | M2_HOME=`cd "$M2_HOME" && pwd`
96 |
97 | cd "$saveddir"
98 | # echo Using m2 at $M2_HOME
99 | fi
100 |
101 | # For Cygwin, ensure paths are in UNIX format before anything is touched
102 | if $cygwin ; then
103 | [ -n "$M2_HOME" ] &&
104 | M2_HOME=`cygpath --unix "$M2_HOME"`
105 | [ -n "$JAVA_HOME" ] &&
106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
107 | [ -n "$CLASSPATH" ] &&
108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
109 | fi
110 |
111 | # For Migwn, ensure paths are in UNIX format before anything is touched
112 | if $mingw ; then
113 | [ -n "$M2_HOME" ] &&
114 | M2_HOME="`(cd "$M2_HOME"; pwd)`"
115 | [ -n "$JAVA_HOME" ] &&
116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
117 | # TODO classpath?
118 | fi
119 |
120 | if [ -z "$JAVA_HOME" ]; then
121 | javaExecutable="`which javac`"
122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
123 | # readlink(1) is not available as standard on Solaris 10.
124 | readLink=`which readlink`
125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
126 | if $darwin ; then
127 | javaHome="`dirname \"$javaExecutable\"`"
128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
129 | else
130 | javaExecutable="`readlink -f \"$javaExecutable\"`"
131 | fi
132 | javaHome="`dirname \"$javaExecutable\"`"
133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'`
134 | JAVA_HOME="$javaHome"
135 | export JAVA_HOME
136 | fi
137 | fi
138 | fi
139 |
140 | if [ -z "$JAVACMD" ] ; then
141 | if [ -n "$JAVA_HOME" ] ; then
142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
143 | # IBM's JDK on AIX uses strange locations for the executables
144 | JAVACMD="$JAVA_HOME/jre/sh/java"
145 | else
146 | JAVACMD="$JAVA_HOME/bin/java"
147 | fi
148 | else
149 | JAVACMD="`which java`"
150 | fi
151 | fi
152 |
153 | if [ ! -x "$JAVACMD" ] ; then
154 | echo "Error: JAVA_HOME is not defined correctly." >&2
155 | echo " We cannot execute $JAVACMD" >&2
156 | exit 1
157 | fi
158 |
159 | if [ -z "$JAVA_HOME" ] ; then
160 | echo "Warning: JAVA_HOME environment variable is not set."
161 | fi
162 |
163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
164 |
165 | # traverses directory structure from process work directory to filesystem root
166 | # first directory with .mvn subdirectory is considered project base directory
167 | find_maven_basedir() {
168 |
169 | if [ -z "$1" ]
170 | then
171 | echo "Path not specified to find_maven_basedir"
172 | return 1
173 | fi
174 |
175 | basedir="$1"
176 | wdir="$1"
177 | while [ "$wdir" != '/' ] ; do
178 | if [ -d "$wdir"/.mvn ] ; then
179 | basedir=$wdir
180 | break
181 | fi
182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc)
183 | if [ -d "${wdir}" ]; then
184 | wdir=`cd "$wdir/.."; pwd`
185 | fi
186 | # end of workaround
187 | done
188 | echo "${basedir}"
189 | }
190 |
191 | # concatenates all lines of a file
192 | concat_lines() {
193 | if [ -f "$1" ]; then
194 | echo "$(tr -s '\n' ' ' < "$1")"
195 | fi
196 | }
197 |
198 | BASE_DIR=`find_maven_basedir "$(pwd)"`
199 | if [ -z "$BASE_DIR" ]; then
200 | exit 1;
201 | fi
202 |
203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
204 | echo $MAVEN_PROJECTBASEDIR
205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
206 |
207 | # For Cygwin, switch paths to Windows format before running java
208 | if $cygwin; then
209 | [ -n "$M2_HOME" ] &&
210 | M2_HOME=`cygpath --path --windows "$M2_HOME"`
211 | [ -n "$JAVA_HOME" ] &&
212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
213 | [ -n "$CLASSPATH" ] &&
214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
215 | [ -n "$MAVEN_PROJECTBASEDIR" ] &&
216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
217 | fi
218 |
219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
220 |
221 | exec "$JAVACMD" \
222 | $MAVEN_OPTS \
223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
226 |
--------------------------------------------------------------------------------
/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM http://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven2 Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
40 |
41 | @REM set %HOME% to equivalent of $HOME
42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
43 |
44 | @REM Execute a user defined script before this one
45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
49 | :skipRcPre
50 |
51 | @setlocal
52 |
53 | set ERROR_CODE=0
54 |
55 | @REM To isolate internal variables from possible post scripts, we use another setlocal
56 | @setlocal
57 |
58 | @REM ==== START VALIDATION ====
59 | if not "%JAVA_HOME%" == "" goto OkJHome
60 |
61 | echo.
62 | echo Error: JAVA_HOME not found in your environment. >&2
63 | echo Please set the JAVA_HOME variable in your environment to match the >&2
64 | echo location of your Java installation. >&2
65 | echo.
66 | goto error
67 |
68 | :OkJHome
69 | if exist "%JAVA_HOME%\bin\java.exe" goto init
70 |
71 | echo.
72 | echo Error: JAVA_HOME is set to an invalid directory. >&2
73 | echo JAVA_HOME = "%JAVA_HOME%" >&2
74 | echo Please set the JAVA_HOME variable in your environment to match the >&2
75 | echo location of your Java installation. >&2
76 | echo.
77 | goto error
78 |
79 | @REM ==== END VALIDATION ====
80 |
81 | :init
82 |
83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
84 | @REM Fallback to current working directory if not found.
85 |
86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
88 |
89 | set EXEC_DIR=%CD%
90 | set WDIR=%EXEC_DIR%
91 | :findBaseDir
92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
93 | cd ..
94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
95 | set WDIR=%CD%
96 | goto findBaseDir
97 |
98 | :baseDirFound
99 | set MAVEN_PROJECTBASEDIR=%WDIR%
100 | cd "%EXEC_DIR%"
101 | goto endDetectBaseDir
102 |
103 | :baseDirNotFound
104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
105 | cd "%EXEC_DIR%"
106 |
107 | :endDetectBaseDir
108 |
109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
110 |
111 | @setlocal EnableExtensions EnableDelayedExpansion
112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
114 |
115 | :endReadAdditionalConfig
116 |
117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
118 |
119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
121 |
122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
123 | if ERRORLEVEL 1 goto error
124 | goto end
125 |
126 | :error
127 | set ERROR_CODE=1
128 |
129 | :end
130 | @endlocal & set ERROR_CODE=%ERROR_CODE%
131 |
132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
136 | :skipRcPost
137 |
138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
140 |
141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
142 |
143 | exit /B %ERROR_CODE%
144 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | nl.jtim.spring.kafka.micrometer
7 | spring-kafka-micrometer
8 | 0.0.1-SNAPSHOT
9 | pom
10 |
11 | spring-kafka-micrometer
12 | Spring Kafka Micrometer Playground
13 |
14 |
15 | spring-kafka-consumer
16 | spring-kafka-producer
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/spring-kafka-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | nl.jtim.spring.kafka.micrometer
7 | spring-kafka-consumer
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | spring-kafka-consumer
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.1.1.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 | 1.1.1
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-web
32 |
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-starter-actuator
37 |
38 |
39 | org.springframework.kafka
40 | spring-kafka
41 |
42 |
43 |
44 | io.micrometer
45 | micrometer-registry-prometheus
46 |
47 |
48 |
49 | org.projectlombok
50 | lombok
51 |
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-starter-test
57 | test
58 |
59 |
60 | org.springframework.kafka
61 | spring-kafka-test
62 | test
63 |
64 |
65 |
66 |
67 |
68 |
69 | org.springframework.boot
70 | spring-boot-maven-plugin
71 |
72 |
73 |
74 |
75 |
76 |
77 | spring-snapshots
78 | Spring Snapshots
79 | https://repo.spring.io/snapshot
80 |
81 | true
82 |
83 |
84 |
85 | spring-milestones
86 | Spring Milestones
87 | https://repo.spring.io/milestone
88 |
89 | false
90 |
91 |
92 |
93 |
94 |
95 |
96 | spring-snapshots
97 | Spring Snapshots
98 | https://repo.spring.io/snapshot
99 |
100 | true
101 |
102 |
103 |
104 | spring-milestones
105 | Spring Milestones
106 | https://repo.spring.io/milestone
107 |
108 | false
109 |
110 |
111 |
112 |
113 |
114 |
115 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/main/java/nl/jtim/spring/kafka/consumer/KafkaController.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.consumer;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.apache.kafka.common.Metric;
5 | import org.apache.kafka.common.MetricName;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
8 | import org.springframework.kafka.listener.MessageListenerContainer;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import java.util.Map;
13 |
14 | @RestController
15 | @Slf4j
16 | public class KafkaController {
17 |
18 | private final KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;
19 |
20 | @Autowired
21 | public KafkaController(KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry) {
22 | this.kafkaListenerEndpointRegistry = kafkaListenerEndpointRegistry;
23 | }
24 |
25 | @GetMapping("/showRawKafkaMetrics")
26 | public String showMetrics() {
27 | for (MessageListenerContainer listenerContainer : kafkaListenerEndpointRegistry.getListenerContainers()) {
28 | Map> metrics = listenerContainer.metrics();
29 | log.info("Kakfa metrics: {}", metrics);
30 | }
31 | return "Raw Kafka metrics logged";
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/main/java/nl/jtim/spring/kafka/consumer/MessageConsumer.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.consumer;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import nl.jtim.spring.kafka.producer.randommessage.Message;
5 | import nl.jtim.spring.kafka.consumer.config.KafkaConsumerConfig;
6 | import org.springframework.kafka.annotation.KafkaListener;
7 |
8 | @Slf4j
9 | public class MessageConsumer {
10 |
11 | @KafkaListener(topics = KafkaConsumerConfig.USER_MESSAGES_TOPIC_NAME)
12 | public void consume(Message message) {
13 | log.info("Consume message from Kafka topic: {}. Key: {} Value: {}", KafkaConsumerConfig.USER_MESSAGES_TOPIC_NAME, message.getId(), message);
14 | }
15 |
16 | }
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/main/java/nl/jtim/spring/kafka/consumer/SpringKafkaConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.kafka.annotation.EnableKafka;
6 |
7 | @SpringBootApplication
8 | @EnableKafka
9 | public class SpringKafkaConsumerApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(SpringKafkaConsumerApplication.class, args);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/main/java/nl/jtim/spring/kafka/consumer/config/KafkaConsumerConfig.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.consumer.config;
2 |
3 | import nl.jtim.spring.kafka.producer.randommessage.Message;
4 | import nl.jtim.spring.kafka.consumer.MessageConsumer;
5 | import org.apache.kafka.common.serialization.StringDeserializer;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
8 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.Configuration;
11 | import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
12 | import org.springframework.kafka.core.ConsumerFactory;
13 | import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
14 | //import org.springframework.kafka.support.DefaultKafkaHeaderMapper;
15 | import org.springframework.kafka.listener.KafkaMessageListenerContainer;
16 | import org.springframework.kafka.support.serializer.JsonDeserializer;
17 |
18 | import java.util.Map;
19 |
20 | @Configuration
21 | @EnableConfigurationProperties(KafkaProperties.class)
22 | public class KafkaConsumerConfig {
23 |
24 | public static final String USER_MESSAGES_TOPIC_NAME = "hello-world-messages";
25 |
26 | @Autowired
27 | private KafkaProperties kafkaProperties;
28 |
29 | @Bean
30 | public ConsumerFactory kafkaConsumerFactory() {
31 | Map consumerProperties = kafkaProperties.buildConsumerProperties();
32 | DefaultKafkaConsumerFactory stringMessageDefaultKafkaConsumerFactory = new DefaultKafkaConsumerFactory<>(consumerProperties, new StringDeserializer(), new JsonDeserializer<>(Message.class));
33 | return stringMessageDefaultKafkaConsumerFactory;
34 | }
35 |
36 | @Bean
37 | public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() {
38 | ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>();
39 | factory.setConsumerFactory(kafkaConsumerFactory());
40 | factory.setConcurrency(1);
41 | return factory;
42 | }
43 |
44 | @Bean
45 | public MessageConsumer messageConsumer() {
46 | return new MessageConsumer();
47 | }
48 |
49 | }
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/main/java/nl/jtim/spring/kafka/producer/randommessage/Message.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer.randommessage;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class Message {
7 |
8 | private String id;
9 | private String message;
10 | private String user;
11 |
12 | Message() {
13 | }
14 |
15 | public Message(String message, String user) {
16 | this.message = message;
17 | this.user = user;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8081
3 |
4 | management:
5 | metrics:
6 | kafka:
7 | consumer:
8 | enabled: true
9 | endpoints:
10 | web:
11 | exposure:
12 | include: "*"
13 |
14 | spring:
15 | kafka:
16 | consumer:
17 | bootstrap-servers: localhost:9092
18 | properties:
19 | spring.json.trusted.packages: "*"
20 | group-id: foo
21 | auto-offset-reset: earliest
22 | client-id: spring-kafka-consumer-hello-world-app
23 | key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
24 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/test/java/nl/jtim/spring/kafka/consumer/SpringKafkaConsumerApplicationTests.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.consumer;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.kafka.test.context.EmbeddedKafka;
7 | import org.springframework.test.context.junit4.SpringRunner;
8 |
9 | @RunWith(SpringRunner.class)
10 | @SpringBootTest
11 | @EmbeddedKafka
12 | public class SpringKafkaConsumerApplicationTests {
13 |
14 | @Test
15 | public void contextLoads() {
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/spring-kafka-consumer/src/test/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | kafka:
3 | consumer:
4 | bootstrap-servers: ${spring.embedded.kafka.brokers}
5 | properties:
6 | spring.json.trusted.packages: "*"
7 | group-id: foo
8 | auto-offset-reset: earliest
9 | client-id: spring-kafka-consumer-hello-world-app
10 | key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
11 |
--------------------------------------------------------------------------------
/spring-kafka-producer/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/spring-kafka-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | nl.jtim.spring.kafka.micrometer
7 | spring-kafka-producer
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | spring-kafka-producer
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.1.1.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-actuator
36 |
37 |
38 | org.springframework.kafka
39 | spring-kafka
40 |
41 |
42 | org.projectlombok
43 | lombok
44 |
45 |
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-starter-test
50 | test
51 |
52 |
53 | org.springframework.kafka
54 | spring-kafka-test
55 | test
56 |
57 |
58 |
59 |
60 |
61 |
62 | org.springframework.boot
63 | spring-boot-maven-plugin
64 |
65 |
66 |
67 |
68 |
69 |
70 | spring-snapshots
71 | Spring Snapshots
72 | https://repo.spring.io/snapshot
73 |
74 | true
75 |
76 |
77 |
78 | spring-milestones
79 | Spring Milestones
80 | https://repo.spring.io/milestone
81 |
82 | false
83 |
84 |
85 |
86 |
87 |
88 |
89 | spring-snapshots
90 | Spring Snapshots
91 | https://repo.spring.io/snapshot
92 |
93 | true
94 |
95 |
96 |
97 | spring-milestones
98 | Spring Milestones
99 | https://repo.spring.io/milestone
100 |
101 | false
102 |
103 |
104 |
105 |
106 |
107 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/java/nl/jtim/spring/kafka/producer/SpringKafkaProducerApplication.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import nl.jtim.spring.kafka.producer.randommessage.RandomMessagePublisher;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 | import org.springframework.kafka.annotation.EnableKafka;
9 | import org.springframework.kafka.core.KafkaAdmin;
10 | import org.springframework.scheduling.annotation.EnableAsync;
11 |
12 | import javax.annotation.PostConstruct;
13 | import javax.annotation.PreDestroy;
14 |
15 | @SpringBootApplication
16 | @Slf4j
17 | @EnableKafka
18 | @EnableAsync
19 | public class SpringKafkaProducerApplication {
20 |
21 | private final RandomMessagePublisher randomMessagePublisher;
22 |
23 | private final KafkaAdmin kafkaAdmin;
24 |
25 | @Autowired
26 | public SpringKafkaProducerApplication(RandomMessagePublisher randomMessagePublisher, KafkaAdmin kafkaAdmin) {
27 | this.randomMessagePublisher = randomMessagePublisher;
28 | this.kafkaAdmin = kafkaAdmin;
29 | }
30 |
31 | public static void main(String[] args) {
32 | SpringApplication.run(SpringKafkaProducerApplication.class, args);
33 | }
34 |
35 | @PostConstruct
36 | void started() {
37 | log.info("Application initialized");
38 |
39 | kafkaAdmin.getConfig();
40 |
41 | randomMessagePublisher.start();
42 | }
43 |
44 | @PreDestroy
45 | void shutDown() {
46 | log.info("Application about to stop");
47 | randomMessagePublisher.stop();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/java/nl/jtim/spring/kafka/producer/config/KafkaProducerConfig.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer.config;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import nl.jtim.spring.kafka.producer.randommessage.Message;
5 | import nl.jtim.spring.kafka.producer.randommessage.RandomMessageKafkaProducer;
6 | import org.apache.kafka.clients.admin.AdminClientConfig;
7 | import org.apache.kafka.clients.admin.NewTopic;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
10 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.context.annotation.Configuration;
13 | import org.springframework.kafka.core.DefaultKafkaProducerFactory;
14 | import org.springframework.kafka.core.KafkaAdmin;
15 | import org.springframework.kafka.core.KafkaTemplate;
16 | import org.springframework.kafka.core.ProducerFactory;
17 |
18 | import java.util.HashMap;
19 | import java.util.Map;
20 |
21 | @Configuration
22 | @EnableConfigurationProperties(KafkaProperties.class)
23 | @Slf4j
24 | public class KafkaProducerConfig {
25 |
26 | public static final String TOPIC_NAME = "hello-world-messages";
27 | private static final int NUMBER_OF_PARTITIONS = 1;
28 | private static final short REPLICATION_FACTOR = (short) 1;
29 |
30 | private final KafkaProperties kafkaProperties;
31 |
32 | @Bean
33 | public KafkaAdmin admin() {
34 | log.info("Create KafkaAdmin");
35 | Map configs = new HashMap<>();
36 | configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
37 | return new KafkaAdmin(configs);
38 | }
39 |
40 | /**
41 | * If the broker supports it (1.0.0 or higher), the admin will increase the number of partitions if it is found that
42 | * an existing topic has fewer partitions than the NewTopic.numPartitions
43 | */
44 | @Bean
45 | public NewTopic userMessageTopic() {
46 | log.info("About to create topic: {} ", TOPIC_NAME);
47 | return new NewTopic(TOPIC_NAME, NUMBER_OF_PARTITIONS, REPLICATION_FACTOR);
48 | }
49 |
50 | @Autowired
51 | public KafkaProducerConfig(KafkaProperties kafkaProperties) {
52 | this.kafkaProperties = kafkaProperties;
53 | }
54 |
55 | @Bean
56 | public ProducerFactory producerFactory() {
57 | Map producerProperties = kafkaProperties.buildProducerProperties();
58 | return new DefaultKafkaProducerFactory<>(producerProperties);
59 | }
60 |
61 | @Bean
62 | public KafkaTemplate kafkaTemplate() {
63 | return new KafkaTemplate<>(producerFactory());
64 | }
65 |
66 | @Bean
67 | public RandomMessageKafkaProducer messageProducer() {
68 | return new RandomMessageKafkaProducer();
69 | }
70 | }
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/java/nl/jtim/spring/kafka/producer/randommessage/Message.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer.randommessage;
2 |
3 | import lombok.Data;
4 |
5 | import java.util.UUID;
6 |
7 | @Data
8 | public class Message {
9 |
10 | private String id;
11 | private String message;
12 | private String user;
13 |
14 | Message() {
15 | }
16 |
17 | public Message(String message, String user) {
18 | id = UUID.randomUUID().toString();
19 | this.message = message;
20 | this.user = user;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/java/nl/jtim/spring/kafka/producer/randommessage/RandomMessageGenerator.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer.randommessage;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | import java.util.Random;
6 |
7 | @Component
8 | class RandomMessageGenerator {
9 |
10 | private String names[] = {"Tim", "Claudia", "Tess", "Maud", "Mike", "Roy"};
11 | private String[] messages = {"Cowboy", "World", "Foo", "Bar", "Duck", "Coder"};
12 |
13 | Message generateRandomMessage() {
14 | return new Message(generateRandomMessageString(), pickRandomName());
15 | }
16 |
17 | private String pickRandomName() {
18 | return names[new Random().nextInt(names.length)];
19 | }
20 |
21 | private String generateRandomMessageString() {
22 | return "Hello " + messages[new Random().nextInt(messages.length)];
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/java/nl/jtim/spring/kafka/producer/randommessage/RandomMessageKafkaProducer.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer.randommessage;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.kafka.core.KafkaTemplate;
6 |
7 | import static nl.jtim.spring.kafka.producer.config.KafkaProducerConfig.TOPIC_NAME;
8 |
9 | @Slf4j
10 | public class RandomMessageKafkaProducer {
11 |
12 | @Autowired
13 | private KafkaTemplate template;
14 |
15 | void send(String key, Message message) {
16 | log.info("Thread: {}. Produce message to Kafka topic: {}. Key: {} Value: {}", Thread.currentThread().getName(), TOPIC_NAME, key, message);
17 |
18 | template.send(TOPIC_NAME, message);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/java/nl/jtim/spring/kafka/producer/randommessage/RandomMessagePublisher.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer.randommessage;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.scheduling.annotation.Async;
6 | import org.springframework.stereotype.Component;
7 |
8 | import java.time.Duration;
9 | import java.util.concurrent.atomic.AtomicBoolean;
10 |
11 | import static java.time.temporal.ChronoUnit.SECONDS;
12 |
13 | @Component
14 | @Slf4j
15 | public class RandomMessagePublisher {
16 |
17 | private final RandomMessageKafkaProducer randomMessageKafkaProducer;
18 | private final RandomMessageGenerator randomMessageGenerator;
19 |
20 | private AtomicBoolean publishingToKafkaEnabled = new AtomicBoolean(false);
21 |
22 | @Autowired
23 | public RandomMessagePublisher(RandomMessageKafkaProducer randomMessageKafkaProducer, RandomMessageGenerator randomMessageGenerator) {
24 | this.randomMessageKafkaProducer = randomMessageKafkaProducer;
25 | this.randomMessageGenerator = randomMessageGenerator;
26 | }
27 |
28 | @Async
29 | public void start() {
30 | log.info("Start publishing random messages to Kafka");
31 | publishingToKafkaEnabled.set(true);
32 | while (publishingToKafkaEnabled.get()) {
33 | try {
34 | Thread.sleep(Duration.of(1, SECONDS).toMillis());
35 | Message message = randomMessageGenerator.generateRandomMessage();
36 | randomMessageKafkaProducer.send(message.getId(), message);
37 | } catch (InterruptedException e) {
38 | log.info("Async thread has been interrupted: {}", e.getMessage());
39 | }
40 | }
41 | }
42 |
43 | public void stop() {
44 | log.info("Stop publishing random messages to Kafka");
45 | publishingToKafkaEnabled.set(true);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | kafka:
3 | bootstrap-servers: localhost:9092
4 | producer:
5 | key-serializer: org.apache.kafka.common.serialization.StringSerializer
6 | value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
7 | admin:
8 | # By default, if the broker is not available, a message will be logged, but the context will continue to load.
9 | # You can programmatically invoke the admin’s initialize() method to try again later.
10 | # If you wish this condition to be considered fatal, set the admin’s fatalIfBrokerNotAvailable property to
11 | # true and the context will fail to initialize.
12 | fail-fast: true
13 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/test/java/nl/jtim/spring/kafka/producer/SpringKafkaProducerApplicationTests.java:
--------------------------------------------------------------------------------
1 | package nl.jtim.spring.kafka.producer;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.kafka.test.context.EmbeddedKafka;
7 | import org.springframework.test.context.junit4.SpringRunner;
8 |
9 | @RunWith(SpringRunner.class)
10 | @SpringBootTest
11 | @EmbeddedKafka
12 | public class SpringKafkaProducerApplicationTests {
13 |
14 | @Test
15 | public void contextLoads() {
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/spring-kafka-producer/src/test/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | kafka:
3 | bootstrap-servers: ${spring.embedded.kafka.brokers}
4 | producer:
5 | key-serializer: org.apache.kafka.common.serialization.StringSerializer
6 | value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
--------------------------------------------------------------------------------