├── .gitignore ├── .travis.yml ├── README.MD ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── docs │ ├── grafana-elastic-flux-dropwizard.json │ └── grafana-elastic-flux-micrometer.json ├── java │ └── com │ │ └── nurkiewicz │ │ └── elasticflux │ │ ├── Config.java │ │ ├── ElasticAdapter.java │ │ ├── ElasticFluxApplication.java │ │ ├── ElasticsearchProperties.java │ │ ├── Person.java │ │ └── PersonController.java └── resources │ └── application.yaml └── test └── java └── com └── nurkiewicz └── elasticflux ├── PersonControllerTest.groovy └── Samples.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 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/out 26 | out/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | jdk: oraclejdk8 4 | 5 | before_cache: 6 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 7 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 8 | 9 | cache: 10 | directories: 11 | - $HOME/.gradle/caches/ 12 | - $HOME/.gradle/wrapper/ 13 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # Elasticsearch + Spring web-flux 2 | 3 | [![Build Status](https://travis-ci.org/nurkiewicz/elastic-flux.svg?branch=master)](https://travis-ci.org/nurkiewicz/elastic-flux) 4 | 5 | This sample application is described in the following articles: 6 | 7 | * [Spring, Reactor and ElasticSearch: from callbacks to reactive streams](http://www.nurkiewicz.com/2018/01/spring-reactor-and-elasticsearch-from.html) 8 | * [Spring, Reactor and ElasticSearch: bechmarking with fake test data](http://www.nurkiewicz.com/2018/01/spring-reactor-and-elasticsearch.html) 9 | * [Monitoring and measuring reactive application with Dropwizard Metrics](http://www.nurkiewicz.com/2018/01/monitoring-and-measuring-reactive.html) 10 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.0.0.M7' 4 | } 5 | repositories { 6 | mavenCentral() 7 | maven { url "https://repo.spring.io/snapshot" } 8 | maven { url "https://repo.spring.io/milestone" } 9 | } 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'eclipse' 17 | apply plugin: 'org.springframework.boot' 18 | apply plugin: 'io.spring.dependency-management' 19 | 20 | group = 'com.nurkiewicz' 21 | version = '0.0.1-SNAPSHOT' 22 | sourceCompatibility = 1.8 23 | 24 | repositories { 25 | mavenCentral() 26 | maven { url "https://repo.spring.io/snapshot" } 27 | maven { url "https://repo.spring.io/milestone" } 28 | } 29 | 30 | 31 | dependencies { 32 | compile 'org.springframework.boot:spring-boot-starter' 33 | compile 'org.springframework.boot:spring-boot-starter-webflux' 34 | compile 'org.springframework.boot:spring-boot-starter-actuator' 35 | compile 'io.micrometer:micrometer-registry-graphite:1.0.0-rc.5' 36 | compile 'io.projectreactor:reactor-test' 37 | compile 'org.elasticsearch:elasticsearch:6.0.1' 38 | compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.0.1' 39 | compile 'io.codearte.jfairy:jfairy:0.5.9' 40 | compile 'org.projectlombok:lombok:1.16.18' 41 | compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.2' 42 | 43 | testCompile 'org.springframework.boot:spring-boot-starter-test' 44 | testCompile 'io.projectreactor:reactor-test' 45 | testCompile 'org.spockframework:spock-core:1.1-groovy-2.4' 46 | testCompile 'org.spockframework:spock-spring:1.1-groovy-2.4' 47 | 48 | } 49 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nurkiewicz/elastic-flux/5c6b17e3ac1478e009a7273e556d3900c2f368ab/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-bin.zip 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/main/docs/grafana-elastic-flux-dropwizard.json: -------------------------------------------------------------------------------- 1 | { 2 | "__inputs": [ 3 | { 4 | "name": "DS_LOCAL_GRAPHITE", 5 | "label": "Local Graphite", 6 | "description": "", 7 | "type": "datasource", 8 | "pluginId": "graphite", 9 | "pluginName": "Graphite" 10 | } 11 | ], 12 | "__requires": [ 13 | { 14 | "type": "grafana", 15 | "id": "grafana", 16 | "name": "Grafana", 17 | "version": "4.4.3" 18 | }, 19 | { 20 | "type": "panel", 21 | "id": "graph", 22 | "name": "Graph", 23 | "version": "" 24 | }, 25 | { 26 | "type": "datasource", 27 | "id": "graphite", 28 | "name": "Graphite", 29 | "version": "1.0.0" 30 | } 31 | ], 32 | "annotations": { 33 | "list": [] 34 | }, 35 | "editable": true, 36 | "gnetId": null, 37 | "graphTooltip": 0, 38 | "hideControls": false, 39 | "id": null, 40 | "links": [], 41 | "refresh": "5s", 42 | "rows": [ 43 | { 44 | "collapse": false, 45 | "height": 250, 46 | "panels": [ 47 | { 48 | "aliasColors": { 49 | "p50": "#3F6833", 50 | "p75": "#7EB26D", 51 | "p95": "#CCA300", 52 | "p98": "#C15C17", 53 | "p99": "#E24D42", 54 | "p999": "#890F02" 55 | }, 56 | "bars": false, 57 | "dashLength": 10, 58 | "dashes": false, 59 | "datasource": "${DS_LOCAL_GRAPHITE}", 60 | "fill": 1, 61 | "hideTimeOverride": false, 62 | "id": 3, 63 | "legend": { 64 | "alignAsTable": false, 65 | "avg": false, 66 | "current": true, 67 | "max": false, 68 | "min": false, 69 | "rightSide": false, 70 | "show": true, 71 | "total": false, 72 | "values": true 73 | }, 74 | "lines": true, 75 | "linewidth": 1, 76 | "links": [], 77 | "nullPointMode": "null", 78 | "percentage": false, 79 | "pointradius": 5, 80 | "points": false, 81 | "renderer": "flot", 82 | "seriesOverrides": [], 83 | "spaceLength": 10, 84 | "span": 12, 85 | "stack": false, 86 | "steppedLine": false, 87 | "targets": [ 88 | { 89 | "refId": "A", 90 | "target": "aliasByNode(elastic-flux.es.index.p*, 3)" 91 | } 92 | ], 93 | "thresholds": [], 94 | "timeFrom": null, 95 | "timeShift": null, 96 | "title": "Index latency percentiles (log scale)", 97 | "tooltip": { 98 | "shared": true, 99 | "sort": 2, 100 | "value_type": "individual" 101 | }, 102 | "transparent": true, 103 | "type": "graph", 104 | "xaxis": { 105 | "buckets": null, 106 | "mode": "time", 107 | "name": null, 108 | "show": true, 109 | "values": [] 110 | }, 111 | "yaxes": [ 112 | { 113 | "format": "ms", 114 | "label": null, 115 | "logBase": 10, 116 | "max": null, 117 | "min": null, 118 | "show": true 119 | }, 120 | { 121 | "format": "short", 122 | "label": null, 123 | "logBase": 1, 124 | "max": null, 125 | "min": null, 126 | "show": true 127 | } 128 | ] 129 | } 130 | ], 131 | "repeat": null, 132 | "repeatIteration": null, 133 | "repeatRowId": null, 134 | "showTitle": false, 135 | "title": "Dashboard Row", 136 | "titleSize": "h6" 137 | }, 138 | { 139 | "collapse": false, 140 | "height": 250, 141 | "panels": [ 142 | { 143 | "aliasColors": { 144 | "concurrent.count": "#C15C17", 145 | "elastic-flux.es.failures.count": "#890F02", 146 | "failures": "#890F02", 147 | "perSecond(elastic-flux.es.failures.count)": "#BF1B00" 148 | }, 149 | "bars": true, 150 | "dashLength": 10, 151 | "dashes": false, 152 | "datasource": "${DS_LOCAL_GRAPHITE}", 153 | "fill": 0, 154 | "id": 2, 155 | "legend": { 156 | "avg": false, 157 | "current": false, 158 | "max": false, 159 | "min": false, 160 | "show": true, 161 | "total": false, 162 | "values": false 163 | }, 164 | "lines": false, 165 | "linewidth": 3, 166 | "links": [], 167 | "nullPointMode": "null", 168 | "percentage": false, 169 | "pointradius": 5, 170 | "points": false, 171 | "renderer": "flot", 172 | "seriesOverrides": [ 173 | { 174 | "alias": "concurrent.count", 175 | "bars": false, 176 | "lines": true, 177 | "stack": false, 178 | "yaxis": 2 179 | } 180 | ], 181 | "spaceLength": 10, 182 | "span": 12, 183 | "stack": true, 184 | "steppedLine": false, 185 | "targets": [ 186 | { 187 | "refId": "A", 188 | "target": "aliasByNode(elastic-flux.es.index.m1_rate, -1)" 189 | }, 190 | { 191 | "refId": "C", 192 | "target": "aliasByNode(perSecond(elastic-flux.es.failures.count), -2)" 193 | }, 194 | { 195 | "refId": "B", 196 | "target": "aliasByNode(elastic-flux.es.concurrent.count, -2, -1)" 197 | } 198 | ], 199 | "thresholds": [], 200 | "timeFrom": null, 201 | "timeShift": null, 202 | "title": "Indexing rate vs concurrency", 203 | "tooltip": { 204 | "shared": true, 205 | "sort": 0, 206 | "value_type": "individual" 207 | }, 208 | "transparent": true, 209 | "type": "graph", 210 | "xaxis": { 211 | "buckets": null, 212 | "mode": "time", 213 | "name": null, 214 | "show": true, 215 | "values": [] 216 | }, 217 | "yaxes": [ 218 | { 219 | "format": "rps", 220 | "label": null, 221 | "logBase": 1, 222 | "max": null, 223 | "min": null, 224 | "show": true 225 | }, 226 | { 227 | "format": "short", 228 | "label": null, 229 | "logBase": 1, 230 | "max": null, 231 | "min": null, 232 | "show": true 233 | } 234 | ] 235 | } 236 | ], 237 | "repeat": null, 238 | "repeatIteration": null, 239 | "repeatRowId": null, 240 | "showTitle": false, 241 | "title": "Dashboard Row", 242 | "titleSize": "h6" 243 | } 244 | ], 245 | "schemaVersion": 14, 246 | "style": "dark", 247 | "tags": [], 248 | "templating": { 249 | "list": [] 250 | }, 251 | "time": { 252 | "from": "now-5m", 253 | "to": "now" 254 | }, 255 | "timepicker": { 256 | "refresh_intervals": [ 257 | "5s", 258 | "10s", 259 | "30s", 260 | "1m", 261 | "5m", 262 | "15m", 263 | "30m", 264 | "1h", 265 | "2h", 266 | "1d" 267 | ], 268 | "time_options": [ 269 | "5m", 270 | "15m", 271 | "1h", 272 | "6h", 273 | "12h", 274 | "24h", 275 | "2d", 276 | "7d", 277 | "30d" 278 | ] 279 | }, 280 | "timezone": "", 281 | "title": "elastic-flux Dropwizard", 282 | "version": 9 283 | } -------------------------------------------------------------------------------- /src/main/docs/grafana-elastic-flux-micrometer.json: -------------------------------------------------------------------------------- 1 | { 2 | "__inputs": [ 3 | { 4 | "name": "DS_LOCAL_GRAPHITE", 5 | "label": "Local Graphite", 6 | "description": "", 7 | "type": "datasource", 8 | "pluginId": "graphite", 9 | "pluginName": "Graphite" 10 | } 11 | ], 12 | "__requires": [ 13 | { 14 | "type": "grafana", 15 | "id": "grafana", 16 | "name": "Grafana", 17 | "version": "4.4.3" 18 | }, 19 | { 20 | "type": "panel", 21 | "id": "graph", 22 | "name": "Graph", 23 | "version": "" 24 | }, 25 | { 26 | "type": "datasource", 27 | "id": "graphite", 28 | "name": "Graphite", 29 | "version": "1.0.0" 30 | } 31 | ], 32 | "annotations": { 33 | "list": [] 34 | }, 35 | "editable": true, 36 | "gnetId": null, 37 | "graphTooltip": 0, 38 | "hideControls": false, 39 | "id": null, 40 | "links": [], 41 | "refresh": "5s", 42 | "rows": [ 43 | { 44 | "collapse": false, 45 | "height": 250, 46 | "panels": [ 47 | { 48 | "aliasColors": { 49 | "p50": "#3F6833", 50 | "p75": "#7EB26D", 51 | "p95": "#CCA300", 52 | "p98": "#C15C17", 53 | "p99": "#E24D42", 54 | "p999": "#890F02" 55 | }, 56 | "bars": false, 57 | "dashLength": 10, 58 | "dashes": false, 59 | "datasource": "${DS_LOCAL_GRAPHITE}", 60 | "fill": 1, 61 | "hideTimeOverride": false, 62 | "id": 3, 63 | "legend": { 64 | "alignAsTable": false, 65 | "avg": false, 66 | "current": true, 67 | "max": false, 68 | "min": false, 69 | "rightSide": false, 70 | "show": true, 71 | "total": false, 72 | "values": true 73 | }, 74 | "lines": true, 75 | "linewidth": 1, 76 | "links": [], 77 | "nullPointMode": "null", 78 | "percentage": false, 79 | "pointradius": 5, 80 | "points": false, 81 | "renderer": "flot", 82 | "seriesOverrides": [], 83 | "spaceLength": 10, 84 | "span": 12, 85 | "stack": false, 86 | "steppedLine": false, 87 | "targets": [ 88 | { 89 | "refId": "A", 90 | "target": "aliasByNode(esTimer.p*, 1)" 91 | } 92 | ], 93 | "thresholds": [], 94 | "timeFrom": null, 95 | "timeShift": null, 96 | "title": "Index latency percentiles", 97 | "tooltip": { 98 | "shared": true, 99 | "sort": 2, 100 | "value_type": "individual" 101 | }, 102 | "transparent": true, 103 | "type": "graph", 104 | "xaxis": { 105 | "buckets": null, 106 | "mode": "time", 107 | "name": null, 108 | "show": true, 109 | "values": [] 110 | }, 111 | "yaxes": [ 112 | { 113 | "format": "ms", 114 | "label": null, 115 | "logBase": 1, 116 | "max": null, 117 | "min": null, 118 | "show": true 119 | }, 120 | { 121 | "format": "short", 122 | "label": null, 123 | "logBase": 1, 124 | "max": null, 125 | "min": null, 126 | "show": true 127 | } 128 | ] 129 | } 130 | ], 131 | "repeat": null, 132 | "repeatIteration": null, 133 | "repeatRowId": null, 134 | "showTitle": false, 135 | "title": "Dashboard Row", 136 | "titleSize": "h6" 137 | }, 138 | { 139 | "collapse": false, 140 | "height": 250, 141 | "panels": [ 142 | { 143 | "aliasColors": { 144 | "concurrent.count": "#C15C17", 145 | "elastic-flux.es.failures.count": "#890F02", 146 | "esConcurrent": "#E0752D", 147 | "failure": "#BF1B00", 148 | "failures": "#890F02", 149 | "perSecond(elastic-flux.es.failures.count)": "#BF1B00" 150 | }, 151 | "bars": true, 152 | "dashLength": 10, 153 | "dashes": false, 154 | "datasource": "${DS_LOCAL_GRAPHITE}", 155 | "fill": 0, 156 | "id": 2, 157 | "legend": { 158 | "avg": false, 159 | "current": false, 160 | "max": false, 161 | "min": false, 162 | "show": true, 163 | "total": false, 164 | "values": false 165 | }, 166 | "lines": false, 167 | "linewidth": 3, 168 | "links": [], 169 | "nullPointMode": "null", 170 | "percentage": false, 171 | "pointradius": 5, 172 | "points": false, 173 | "renderer": "flot", 174 | "seriesOverrides": [ 175 | { 176 | "alias": "esConcurrent", 177 | "bars": false, 178 | "lines": true, 179 | "stack": false, 180 | "yaxis": 2 181 | } 182 | ], 183 | "spaceLength": 10, 184 | "span": 12, 185 | "stack": true, 186 | "steppedLine": false, 187 | "targets": [ 188 | { 189 | "refId": "A", 190 | "target": "aliasByNode(esIndex.result.success.m1_rate, -1)" 191 | }, 192 | { 193 | "refId": "C", 194 | "target": "aliasByNode(esIndex.result.failure.m1_rate, -2)" 195 | }, 196 | { 197 | "refId": "B", 198 | "target": "esConcurrent" 199 | } 200 | ], 201 | "thresholds": [], 202 | "timeFrom": null, 203 | "timeShift": null, 204 | "title": "Indexing rate vs concurrency", 205 | "tooltip": { 206 | "shared": true, 207 | "sort": 0, 208 | "value_type": "individual" 209 | }, 210 | "transparent": true, 211 | "type": "graph", 212 | "xaxis": { 213 | "buckets": null, 214 | "mode": "time", 215 | "name": null, 216 | "show": true, 217 | "values": [] 218 | }, 219 | "yaxes": [ 220 | { 221 | "format": "rps", 222 | "label": null, 223 | "logBase": 1, 224 | "max": null, 225 | "min": null, 226 | "show": true 227 | }, 228 | { 229 | "format": "short", 230 | "label": null, 231 | "logBase": 1, 232 | "max": null, 233 | "min": null, 234 | "show": true 235 | } 236 | ] 237 | } 238 | ], 239 | "repeat": null, 240 | "repeatIteration": null, 241 | "repeatRowId": null, 242 | "showTitle": false, 243 | "title": "Dashboard Row", 244 | "titleSize": "h6" 245 | }, 246 | { 247 | "collapse": false, 248 | "height": 277, 249 | "panels": [ 250 | { 251 | "aliasColors": {}, 252 | "bars": false, 253 | "dashLength": 10, 254 | "dashes": false, 255 | "datasource": "${DS_LOCAL_GRAPHITE}", 256 | "fill": 1, 257 | "id": 4, 258 | "legend": { 259 | "avg": false, 260 | "current": false, 261 | "max": false, 262 | "min": false, 263 | "show": true, 264 | "total": false, 265 | "values": false 266 | }, 267 | "lines": true, 268 | "linewidth": 1, 269 | "links": [], 270 | "nullPointMode": "null", 271 | "percentage": false, 272 | "pointradius": 5, 273 | "points": false, 274 | "renderer": "flot", 275 | "seriesOverrides": [ 276 | { 277 | "alias": "jvmGcPause.action.end_of_minor_GC.cause.Allocation_Failure.m1_rate", 278 | "yaxis": 2 279 | }, 280 | { 281 | "alias": "jvmGcPromotionRate.m1_rate", 282 | "yaxis": 2 283 | }, 284 | { 285 | "alias": "jvmGcPromotionRate", 286 | "yaxis": 2 287 | } 288 | ], 289 | "spaceLength": 10, 290 | "span": 4, 291 | "stack": false, 292 | "steppedLine": false, 293 | "targets": [ 294 | { 295 | "refId": "A", 296 | "target": "aliasByNode(jvmGc*Rate.m1_rate, 0)" 297 | } 298 | ], 299 | "thresholds": [], 300 | "timeFrom": null, 301 | "timeShift": null, 302 | "title": "GC allocation and promotion rate", 303 | "tooltip": { 304 | "shared": true, 305 | "sort": 0, 306 | "value_type": "individual" 307 | }, 308 | "transparent": true, 309 | "type": "graph", 310 | "xaxis": { 311 | "buckets": null, 312 | "mode": "time", 313 | "name": null, 314 | "show": true, 315 | "values": [] 316 | }, 317 | "yaxes": [ 318 | { 319 | "format": "Bps", 320 | "label": null, 321 | "logBase": 1, 322 | "max": null, 323 | "min": null, 324 | "show": true 325 | }, 326 | { 327 | "format": "Bps", 328 | "label": null, 329 | "logBase": 1, 330 | "max": null, 331 | "min": null, 332 | "show": true 333 | } 334 | ] 335 | }, 336 | { 337 | "aliasColors": {}, 338 | "bars": false, 339 | "dashLength": 10, 340 | "dashes": false, 341 | "datasource": "${DS_LOCAL_GRAPHITE}", 342 | "fill": 1, 343 | "id": 5, 344 | "legend": { 345 | "avg": false, 346 | "current": false, 347 | "max": false, 348 | "min": false, 349 | "show": true, 350 | "total": false, 351 | "values": false 352 | }, 353 | "lines": true, 354 | "linewidth": 1, 355 | "links": [], 356 | "nullPointMode": "null", 357 | "percentage": false, 358 | "pointradius": 5, 359 | "points": false, 360 | "renderer": "flot", 361 | "seriesOverrides": [], 362 | "spaceLength": 10, 363 | "span": 4, 364 | "stack": false, 365 | "steppedLine": false, 366 | "targets": [ 367 | { 368 | "refId": "B", 369 | "target": "aliasByNode(jvmMemoryUsed.area.*.id.*, -1)" 370 | }, 371 | { 372 | "refId": "A", 373 | "target": "" 374 | } 375 | ], 376 | "thresholds": [], 377 | "timeFrom": null, 378 | "timeShift": null, 379 | "title": "Memory usage", 380 | "tooltip": { 381 | "shared": true, 382 | "sort": 0, 383 | "value_type": "individual" 384 | }, 385 | "transparent": true, 386 | "type": "graph", 387 | "xaxis": { 388 | "buckets": null, 389 | "mode": "time", 390 | "name": null, 391 | "show": true, 392 | "values": [] 393 | }, 394 | "yaxes": [ 395 | { 396 | "format": "bytes", 397 | "label": null, 398 | "logBase": 1, 399 | "max": null, 400 | "min": null, 401 | "show": true 402 | }, 403 | { 404 | "format": "short", 405 | "label": null, 406 | "logBase": 1, 407 | "max": null, 408 | "min": null, 409 | "show": true 410 | } 411 | ] 412 | }, 413 | { 414 | "aliasColors": {}, 415 | "bars": false, 416 | "dashLength": 10, 417 | "dashes": false, 418 | "datasource": "${DS_LOCAL_GRAPHITE}", 419 | "fill": 1, 420 | "id": 6, 421 | "legend": { 422 | "avg": false, 423 | "current": false, 424 | "max": false, 425 | "min": false, 426 | "show": true, 427 | "total": false, 428 | "values": false 429 | }, 430 | "lines": true, 431 | "linewidth": 1, 432 | "links": [], 433 | "nullPointMode": "null", 434 | "percentage": false, 435 | "pointradius": 5, 436 | "points": false, 437 | "renderer": "flot", 438 | "seriesOverrides": [ 439 | { 440 | "alias": "systemLoadAverage1m", 441 | "yaxis": 2 442 | } 443 | ], 444 | "spaceLength": 10, 445 | "span": 4, 446 | "stack": false, 447 | "steppedLine": false, 448 | "targets": [ 449 | { 450 | "refId": "A", 451 | "target": "jvmThreadsDaemon" 452 | }, 453 | { 454 | "refId": "B", 455 | "target": "jvmThreadsLive" 456 | }, 457 | { 458 | "refId": "C", 459 | "target": "systemLoadAverage1m" 460 | } 461 | ], 462 | "thresholds": [], 463 | "timeFrom": null, 464 | "timeShift": null, 465 | "title": "Threads & CPU", 466 | "tooltip": { 467 | "shared": true, 468 | "sort": 0, 469 | "value_type": "individual" 470 | }, 471 | "transparent": true, 472 | "type": "graph", 473 | "xaxis": { 474 | "buckets": null, 475 | "mode": "time", 476 | "name": null, 477 | "show": true, 478 | "values": [] 479 | }, 480 | "yaxes": [ 481 | { 482 | "format": "short", 483 | "label": null, 484 | "logBase": 1, 485 | "max": null, 486 | "min": null, 487 | "show": true 488 | }, 489 | { 490 | "format": "short", 491 | "label": null, 492 | "logBase": 1, 493 | "max": null, 494 | "min": null, 495 | "show": true 496 | } 497 | ] 498 | } 499 | ], 500 | "repeat": null, 501 | "repeatIteration": null, 502 | "repeatRowId": null, 503 | "showTitle": false, 504 | "title": "Dashboard Row", 505 | "titleSize": "h6" 506 | } 507 | ], 508 | "schemaVersion": 14, 509 | "style": "dark", 510 | "tags": [], 511 | "templating": { 512 | "list": [] 513 | }, 514 | "time": { 515 | "from": "now-5m", 516 | "to": "now" 517 | }, 518 | "timepicker": { 519 | "refresh_intervals": [ 520 | "5s", 521 | "10s", 522 | "30s", 523 | "1m", 524 | "5m", 525 | "15m", 526 | "30m", 527 | "1h", 528 | "2h", 529 | "1d" 530 | ], 531 | "time_options": [ 532 | "5m", 533 | "15m", 534 | "1h", 535 | "6h", 536 | "12h", 537 | "24h", 538 | "2d", 539 | "7d", 540 | "30d" 541 | ] 542 | }, 543 | "timezone": "", 544 | "title": "elastic-flux Micrometer", 545 | "version": 9 546 | } -------------------------------------------------------------------------------- /src/main/java/com/nurkiewicz/elasticflux/Config.java: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.databind.SerializationFeature; 5 | import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; 6 | import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; 7 | import org.elasticsearch.client.RestClient; 8 | import org.elasticsearch.client.RestHighLevelClient; 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 10 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | 14 | @Configuration 15 | class Config { 16 | 17 | @Bean 18 | ObjectMapper objectMapper() { 19 | final ObjectMapper objectMapper = new ObjectMapper(); 20 | objectMapper.findAndRegisterModules(); 21 | objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 22 | return objectMapper; 23 | } 24 | 25 | @Bean 26 | RestHighLevelClient restHighLevelClient(ElasticsearchProperties props) { 27 | return new RestHighLevelClient( 28 | RestClient 29 | .builder(props.hosts()) 30 | .setRequestConfigCallback(config -> config 31 | .setConnectTimeout(props.getConnectTimeout()) 32 | .setConnectionRequestTimeout(props.getConnectionRequestTimeout()) 33 | .setSocketTimeout(props.getSocketTimeout()) 34 | ) 35 | .setMaxRetryTimeoutMillis(props.getMaxRetryTimeoutMillis())); 36 | } 37 | 38 | 39 | @Bean 40 | @ConditionalOnProperty(value = "spring.metrics.binders.jvmthreads.enabled", matchIfMissing = true) 41 | @ConditionalOnMissingBean(JvmThreadMetrics.class) 42 | public JvmThreadMetrics jvmThreadMetrics() { 43 | return new JvmThreadMetrics(); 44 | } 45 | 46 | @Bean 47 | @ConditionalOnProperty(value = "spring.metrics.binders.jvmgc.enabled", matchIfMissing = true) 48 | @ConditionalOnMissingBean(JvmGcMetrics.class) 49 | public JvmGcMetrics jvmGcMetrics() { 50 | return new JvmGcMetrics(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/nurkiewicz/elasticflux/ElasticAdapter.java: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import io.micrometer.core.instrument.Counter; 6 | import io.micrometer.core.instrument.Metrics; 7 | import io.micrometer.core.instrument.Timer; 8 | import lombok.RequiredArgsConstructor; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.elasticsearch.action.ActionListener; 11 | import org.elasticsearch.action.get.GetRequest; 12 | import org.elasticsearch.action.get.GetResponse; 13 | import org.elasticsearch.action.index.IndexRequest; 14 | import org.elasticsearch.action.index.IndexResponse; 15 | import org.elasticsearch.client.RestHighLevelClient; 16 | import org.elasticsearch.common.xcontent.XContentType; 17 | import org.springframework.stereotype.Component; 18 | import reactor.core.publisher.Mono; 19 | import reactor.core.publisher.MonoSink; 20 | 21 | import java.util.concurrent.TimeUnit; 22 | import java.util.concurrent.atomic.LongAdder; 23 | 24 | @Component 25 | @Slf4j 26 | @RequiredArgsConstructor 27 | class ElasticAdapter { 28 | 29 | private final RestHighLevelClient client; 30 | private final ObjectMapper objectMapper; 31 | 32 | private final Timer indexTimer = Metrics.timer("es.timer"); 33 | private final LongAdder concurrent = Metrics.gauge("es.concurrent", new LongAdder()); 34 | private final Counter successes = Metrics.counter("es.index", "result", "success"); 35 | private final Counter failures = Metrics.counter("es.index", "result", "failure"); 36 | 37 | Mono findByUserName(String userName) { 38 | return Mono 39 | .create(sink -> 40 | client.getAsync(new GetRequest("people", "person", userName), listenerToSink(sink)) 41 | ) 42 | .filter(GetResponse::isExists) 43 | .map(GetResponse::getSource) 44 | .map(map -> objectMapper.convertValue(map, Person.class)); 45 | } 46 | 47 | Mono index(Person doc) { 48 | return indexDoc(doc) 49 | .compose(this::countSuccFail) 50 | .compose(this::countConcurrent) 51 | .compose(this::measureTime) 52 | .doOnError(e -> log.error("Unable to index {}", doc, e)); 53 | } 54 | 55 | private Mono countConcurrent(Mono mono) { 56 | return mono 57 | .doOnSubscribe(s -> concurrent.increment()) 58 | .doOnTerminate(concurrent::decrement); 59 | } 60 | 61 | private Mono measureTime(Mono mono) { 62 | return Mono 63 | .fromCallable(System::currentTimeMillis) 64 | .flatMap(time -> 65 | mono.doOnSuccess(response -> 66 | indexTimer.record(System.currentTimeMillis() - time, TimeUnit.MILLISECONDS)) 67 | ); 68 | } 69 | 70 | private Mono countSuccFail(Mono mono) { 71 | return mono 72 | .doOnError(e -> failures.increment()) 73 | .doOnSuccess(response -> successes.increment()); 74 | } 75 | 76 | private Mono indexDoc(Person doc) { 77 | return Mono.create(sink -> { 78 | try { 79 | doIndex(doc, listenerToSink(sink)); 80 | } catch (JsonProcessingException e) { 81 | sink.error(e); 82 | } 83 | }); 84 | } 85 | 86 | private void doIndex(Person doc, ActionListener listener) throws JsonProcessingException { 87 | final IndexRequest indexRequest = new IndexRequest("people", "person", doc.getUsername()); 88 | final String json = objectMapper.writeValueAsString(doc); 89 | indexRequest.source(json, XContentType.JSON); 90 | client.indexAsync(indexRequest, listener); 91 | } 92 | 93 | private ActionListener listenerToSink(MonoSink sink) { 94 | return new ActionListener() { 95 | @Override 96 | public void onResponse(T response) { 97 | sink.success(response); 98 | } 99 | 100 | @Override 101 | public void onFailure(Exception e) { 102 | sink.error(e); 103 | } 104 | }; 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/nurkiewicz/elasticflux/ElasticFluxApplication.java: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ElasticFluxApplication { 8 | 9 | public static void main(String[] args) throws Exception { 10 | SpringApplication.run(ElasticFluxApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/nurkiewicz/elasticflux/ElasticsearchProperties.java: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux; 2 | 3 | import lombok.Data; 4 | import org.apache.http.HttpHost; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.List; 9 | 10 | @Component 11 | @ConfigurationProperties(prefix = "elasticsearch") 12 | @Data 13 | public class ElasticsearchProperties { 14 | 15 | private List hosts; 16 | private int connectTimeout; 17 | private int connectionRequestTimeout; 18 | private int socketTimeout; 19 | private int maxRetryTimeoutMillis; 20 | 21 | HttpHost[] hosts() { 22 | return hosts 23 | .stream() 24 | .map(HttpHost::create) 25 | .toArray(HttpHost[]::new); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/nurkiewicz/elasticflux/Person.java: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux; 2 | 3 | import lombok.Value; 4 | 5 | import javax.validation.Valid; 6 | import javax.validation.constraints.NotBlank; 7 | import javax.validation.constraints.NotNull; 8 | import java.net.URL; 9 | import java.time.LocalDate; 10 | import java.util.List; 11 | 12 | @Value 13 | class Person { 14 | @NotNull 15 | @Valid 16 | private final Address address; 17 | 18 | @NotBlank 19 | private final String firstName; 20 | 21 | private final String middleName; 22 | 23 | @NotBlank 24 | private final String lastName; 25 | 26 | @NotBlank 27 | private final String email; 28 | 29 | private final String companyEmail; 30 | 31 | @NotBlank 32 | private final String username; 33 | 34 | @NotBlank 35 | private final String password; 36 | 37 | @NotBlank 38 | private final String sex; 39 | 40 | @NotBlank 41 | private final String telephoneNumber; 42 | 43 | @NotNull 44 | private final LocalDate dateOfBirth; 45 | 46 | @Valid 47 | private final Company company; 48 | 49 | @NotBlank 50 | private final String nationalIdentityCardNumber; 51 | 52 | private final String nationalIdentificationNumber; 53 | 54 | private final String passportNumber; 55 | 56 | } 57 | 58 | @Value 59 | class Address { 60 | @NotBlank 61 | private final String street; 62 | 63 | @NotBlank 64 | private final String streetNumber; 65 | 66 | private final String apartmentNumber; 67 | 68 | @NotBlank 69 | private final String postalCode; 70 | 71 | @NotBlank 72 | private final String city; 73 | 74 | private final List lines; 75 | } 76 | 77 | @Value 78 | class Company { 79 | @NotBlank 80 | private final String name; 81 | 82 | @NotBlank 83 | private final String domain; 84 | 85 | @NotBlank 86 | private final String email; 87 | 88 | @NotNull 89 | private final URL url; 90 | 91 | @NotBlank 92 | private final String vatIdentificationNumber; 93 | 94 | } -------------------------------------------------------------------------------- /src/main/java/com/nurkiewicz/elasticflux/PersonController.java: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import lombok.RequiredArgsConstructor; 5 | import org.elasticsearch.action.index.IndexResponse; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.PutMapping; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RestController; 14 | import reactor.core.publisher.Mono; 15 | 16 | import javax.validation.Valid; 17 | import java.util.Map; 18 | 19 | @RequiredArgsConstructor 20 | @RestController 21 | @RequestMapping("/person") 22 | class PersonController { 23 | 24 | private static final Mono> NOT_FOUND = Mono.just(ResponseEntity.notFound().build()); 25 | 26 | private final ElasticAdapter elasticAdapter; 27 | 28 | @PutMapping 29 | Mono>> put(@Valid @RequestBody Person person) { 30 | return elasticAdapter 31 | .index(person) 32 | .map(this::toMap) 33 | .map(m -> ResponseEntity.status(HttpStatus.CREATED).body(m)); 34 | } 35 | 36 | @GetMapping("/{userName}") 37 | Mono> get(@PathVariable("userName") String userName) { 38 | return elasticAdapter 39 | .findByUserName(userName) 40 | .map(ResponseEntity::ok) 41 | .switchIfEmpty(NOT_FOUND); 42 | } 43 | 44 | private ImmutableMap toMap(IndexResponse response) { 45 | return ImmutableMap 46 | .builder() 47 | .put("id", response.getId()) 48 | .put("index", response.getIndex()) 49 | .put("type", response.getType()) 50 | .put("version", response.getVersion()) 51 | .put("result", response.getResult().getLowercase()) 52 | .put("seqNo", response.getSeqNo()) 53 | .put("primaryTerm", response.getPrimaryTerm()) 54 | .build(); 55 | } 56 | 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | logging.level: 2 | com.nurkiewicz: TRACE 3 | 4 | elasticsearch: 5 | hosts: 6 | - http://localhost:9200 7 | connectTimeout: 10000 8 | connectionRequestTimeout: 10000 9 | socketTimeout: 10000 10 | maxRetryTimeoutMillis: 60000 11 | 12 | spring.metrics.export.graphite: 13 | host: 192.168.99.100 14 | port: 2003 15 | protocol: Plaintext 16 | step: PT1S -------------------------------------------------------------------------------- /src/test/java/com/nurkiewicz/elasticflux/PersonControllerTest.groovy: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux 2 | 3 | import org.springframework.beans.factory.annotation.Value 4 | import org.springframework.boot.test.context.SpringBootTest 5 | import org.springframework.boot.test.web.client.TestRestTemplate 6 | import org.springframework.http.HttpEntity 7 | import org.springframework.http.HttpHeaders 8 | import org.springframework.http.ResponseEntity 9 | import org.springframework.test.context.ContextConfiguration 10 | import spock.lang.Specification 11 | 12 | import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT 13 | import static org.springframework.http.HttpMethod.PUT 14 | import static org.springframework.http.HttpStatus.CREATED 15 | import static org.springframework.http.HttpStatus.OK 16 | import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8 17 | 18 | @ContextConfiguration 19 | @SpringBootTest(webEnvironment = RANDOM_PORT) 20 | class PersonControllerTest extends Specification { 21 | 22 | @Value('${local.server.port}') 23 | int port 24 | 25 | TestRestTemplate rest = new TestRestTemplate() 26 | 27 | def 'should index document'() { 28 | given: 29 | HttpEntity request = indexRequest(Samples.DOCUMENT) 30 | when: 31 | ResponseEntity response = rest.exchange(url(), PUT, request, String) 32 | then: 33 | response.statusCode == CREATED 34 | } 35 | 36 | def 'should find indexed document'() { 37 | given: 38 | assert rest.exchange(url(), PUT, indexRequest(Samples.DOCUMENT), String).statusCode == CREATED 39 | when: 40 | ResponseEntity response = rest.getForEntity(url() + "/" + Samples.USER_NAME, Map) 41 | then: 42 | response.statusCode == OK 43 | response.body.address.street == 'Washington Walk' 44 | } 45 | 46 | private String url() { 47 | return "http://localhost:$port/person" 48 | } 49 | 50 | HttpEntity indexRequest(String document) { 51 | HttpHeaders headers = new HttpHeaders() 52 | headers.setContentType(APPLICATION_JSON_UTF8) 53 | return new HttpEntity(document, headers) 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/com/nurkiewicz/elasticflux/Samples.groovy: -------------------------------------------------------------------------------- 1 | package com.nurkiewicz.elasticflux 2 | 3 | class Samples { 4 | 5 | static final String USER_NAME = "faithc6196467"; 6 | 7 | static final String DOCUMENT = """{ 8 | "address": { 9 | "street": "Washington Walk", 10 | "streetNumber": "161", 11 | "apartmentNumber": "", 12 | "postalCode": "90192", 13 | "city": "Washington", 14 | "lines": [ "161 Washington Walk", "Washington 90192" ] 15 | }, 16 | "firstName": "Faith", 17 | "middleName": "Sophie", 18 | "lastName": "Cole", 19 | "email": "cole@yahoo.com", 20 | "companyEmail": "faith.cole@beansassociates.com", 21 | "username": "$USER_NAME", 22 | "password": "fM8e57Jl", 23 | "sex": "FEMALE", 24 | "telephoneNumber": "332-328-013", 25 | "dateOfBirth": "1969-06-30", 26 | "company": { 27 | "name": "Beans Associates", 28 | "domain": "beansassociates.com", 29 | "email": "contact@beansassociates.com", 30 | "vatIdentificationNumber": "12-0003477", 31 | "url": "http://www.beansassociates.com" 32 | }, 33 | "nationalIdentityCardNumber": "702-84-9132", 34 | "nationalIdentificationNumber": "", 35 | "passportNumber": "7Jvhf1UKQ" 36 | } 37 | """ 38 | 39 | } 40 | --------------------------------------------------------------------------------