├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── build.sh ├── consul-template.service ├── docker-build.sh ├── docker-compose-example ├── docker-compose.yml ├── docker.example.md └── nginx-setup │ ├── conf │ └── config.hcl │ └── templates │ └── template.ctmpl ├── docker-push.sh ├── example.md ├── example ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── Dockerfile ├── mvnw ├── mvnw.cmd ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── ru │ │ └── zhenik │ │ └── akka │ │ └── example │ │ ├── AppConfiguration.java │ │ ├── Boot.java │ │ ├── infrastructure │ │ └── discovery │ │ │ └── DiscoveryAgentActor.java │ │ └── interfaces │ │ └── rest │ │ └── AppResource.java │ └── resources │ └── application.conf └── nginx.service /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | services: 4 | - docker 5 | 6 | script: 7 | - ./docker-build.sh 8 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:1.14.0 2 | 3 | MAINTAINER Ladislav Gazo 4 | MAINTAINER Nikita Zhevnitskiy 5 | 6 | RUN DEBIAN_FRONTEND=noninteractive \ 7 | apt-get update -qq && \ 8 | apt-get -y install wget runit unzip && \ 9 | rm -rf /var/lib/apt/lists/* 10 | 11 | # vim - debug purposes 12 | RUN apt-get update && apt-get -y install vim 13 | RUN wget https://releases.hashicorp.com/consul-template/0.19.5/consul-template_0.19.5_linux_amd64.zip 14 | # install consul-template 15 | RUN unzip -d /usr/local/bin consul-template_0.19.5_linux_amd64.zip && rm consul-template_0.19.5_linux_amd64.zip 16 | 17 | ADD nginx.service /etc/service/nginx/run 18 | ADD consul-template.service /etc/service/consul-template/run 19 | 20 | RUN mkdir /etc/consul-template && chmod +x /etc/service/nginx/run && chmod +x /etc/service/consul-template/run 21 | 22 | CMD ["/usr/bin/runsvdir", "/etc/service"] 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Seges s.r.o. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/seges/docker-nginx-consul.svg?branch=master)](https://travis-ci.org/seges/docker-nginx-consul) 2 | 3 | [![](https://badge.imagelayers.io/seges/nginx-consul:1.9.9.svg)](https://imagelayers.io/?images=seges/nginx-consul:1.9.9 'Get your own badge on imagelayers.io') 4 | 5 | TODO: update img tag 6 | 7 | # Nginx with Consul Template 8 | 9 | This Docker image follows official Nginx image extended with Consul Template to allow to refresh configuration based on the changes in Consul repository 10 | 11 | ## Example 12 | 13 | [How to run example](./example.md) 14 | 15 | [Application readme](./example/readme.md) 16 | 17 | [Docker-compose explanation](./docker-compose-example/docker.example.md) 18 | 19 | ## Volumes 20 | 21 | * /etc/nginx/conf.d -> nginx configuration 22 | * /etc/consul-template/conf -> Consul Template configurations taken and merged alphabetically - https://github.com/hashicorp/consul-template 23 | * /etc/consul-template/templates -> a good place to situate your templates defined in the configuration 24 | 25 | ## Run 26 | 27 | ``` 28 | docker run --rm --volumes-from=yourdata-image -ti seges/nginx-consul:1.14.0 29 | ``` 30 | 31 | ## Refs to blog 32 | 33 | [Blog1](https://medium.com/@ladislavGazo/easy-routing-and-service-discovery-with-docker-consul-and-nginx-acfd48e1a291) -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | cd example 3 | ./mvnw clean install -------------------------------------------------------------------------------- /consul-template.service: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | exec consul-template -config "/etc/consul-template/conf" 4 | -------------------------------------------------------------------------------- /docker-build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | docker build --rm -t seges/nginx-consul:1.14.0 . 4 | 5 | -------------------------------------------------------------------------------- /docker-compose-example/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | 5 | # consul cluster 6 | consul1: 7 | image: consul 8 | command: consul agent -server -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/tmp/consul -ui 9 | hostname: consul1 10 | ports: 11 | - "8301:8300" 12 | - "8401:8400" 13 | - "8501:8500" 14 | - "8601:8600" 15 | - "9001:9001" 16 | 17 | consul2: 18 | image: consul 19 | command: consul agent -server -client=0.0.0.0 -bootstrap-expect=3 -rejoin -retry-join=consul1 -data-dir=/tmp/consul -ui 20 | hostname: consul2 21 | links: 22 | - consul1 23 | ports: 24 | - "8302:8300" 25 | - "8402:8400" 26 | - "8502:8500" 27 | - "8602:8600" 28 | - "9002:9001" 29 | depends_on: 30 | - consul1 31 | 32 | consul3: 33 | image: consul 34 | command: consul agent -server -client=0.0.0.0 -bootstrap-expect=3 -rejoin -retry-join=consul1 -data-dir=/tmp/consul -ui 35 | hostname: consul3 36 | links: 37 | - consul1 38 | ports: 39 | - "8303:8300" 40 | - "8403:8400" 41 | - "8503:8500" 42 | - "8603:8600" 43 | - "9003:9001" 44 | depends_on: 45 | - consul1 46 | 47 | # replicas 48 | backend1: 49 | build: ../example 50 | image: zhenik/akka-http-example 51 | environment: 52 | - DISCOVERY_HOST=consul1 53 | - APPLICATION_HOST=backend1 54 | #uncomment ports for debug 55 | # ports: 56 | # - "8881:3000" 57 | depends_on: 58 | - consul1 59 | 60 | backend2: 61 | build: ../example 62 | image: zhenik/akka-http-example 63 | environment: 64 | - DISCOVERY_HOST=consul2 65 | - APPLICATION_HOST=backend2 66 | depends_on: 67 | - consul2 68 | 69 | backend3: 70 | build: ../example 71 | image: zhenik/akka-http-example 72 | environment: 73 | - DISCOVERY_HOST=consul3 74 | - APPLICATION_HOST=backend3 75 | depends_on: 76 | - consul3 77 | 78 | nginx: 79 | build: ../ 80 | image: seges/nginx-consul 81 | ports: 82 | - "80:80" 83 | - "8080:8080" 84 | - "443:443" 85 | volumes: 86 | # templates 87 | - "./nginx-setup/templates/template.ctmpl:/etc/consul-template/templates/template.ctmpl" 88 | # config 89 | - "./nginx-setup/conf:/etc/consul-template/conf" 90 | depends_on: 91 | - consul1 -------------------------------------------------------------------------------- /docker-compose-example/docker.example.md: -------------------------------------------------------------------------------- 1 | # Docker-compose example 2 | 3 | * Consul cluster: 3 nodes 4 | * Example app: 3 replicas, each replica connect to different consul node in cluster 5 | * Nginx + consul-template node. It is used as gateway & loadbalancer 6 | 7 | ## Notes 8 | 9 | A. Each example instance needs consul address, which provided as a ENV variable. 10 | 11 | ``` 12 | backend2: 13 | build: ../example 14 | image: zhenik/akka-http-example 15 | environment: 16 | - DISCOVERY_HOST=consul1 17 | - APPLICATION_HOST=backend2 18 | ``` 19 | 20 | B. Nginx + consul-template requires 2 volumes. 21 | 22 | ``` 23 | nginx: 24 | build: ../ 25 | image: seges/nginx-consul 26 | ports: 27 | - "80:80" 28 | - "8080:8080" 29 | - "443:443" 30 | volumes: 31 | # templates 32 | - "./nginx-setup/templates/template.ctmpl:/etc/consul-template/templates/template.ctmpl" 33 | # config 34 | - "./nginx-setup/conf:/etc/consul-template/conf" 35 | depends_on: 36 | - consul1 37 | ``` 38 | 39 | - Volume with templates. Templates contains instruction how to render target files. 40 | 41 | ``` 42 | upstream app-example { 43 | least_conn; 44 | {{range service "example-app"}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1; 45 | {{else}}server 127.0.0.1:65535; # force a 502{{end}} 46 | } 47 | server { 48 | listen 8080; 49 | server_name localhost; 50 | 51 | location / { 52 | proxy_pass http://app-example; 53 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 54 | proxy_set_header Host $host; 55 | proxy_set_header X-Real-IP $remote_addr; 56 | } 57 | } 58 | ``` 59 | 60 | - Volume with config for `consul-template` 61 | 62 | All config can be found in [consul-template documentation](https://github.com/hashicorp/consul-template#configuration-file-format) 63 | 64 | ``` 65 | consul{ 66 | address = "consul1:8500" // which consul connect to 67 | } 68 | 69 | template { 70 | source = "/etc/consul-template/templates/template.ctmpl" // source file for tmpl 71 | destination = "/etc/nginx/conf.d/default.conf" // target file with rendered data 72 | command = "/etc/init.d/nginx reload" 73 | command_timeout = "60s" 74 | } 75 | ``` 76 | 77 | ## If you want extend solution 78 | 79 | ### Problems 80 | 81 | ``Problem A``: What if `consul1` is down, but cluster is resilient and `consul2` `consul3` are accessible. 82 | 83 | ``Problem B``: Volumes with templates, point `explicitly` to specific file. How to add more templates. 84 | 85 | ``Problem C``: Gateway is bottleneck. It is only one node. 86 | 87 | ### Possible solutions 88 | 89 | ``Problem A1``: Bootstrap consul cluster. 90 | 91 | ``Problem A2``: Setup consul-agent on `seges/nginx-consul` image. Solution from [documentation](https://github.com/hashicorp/consul-template#configuration-file-format) 92 | 93 | `address = "127.0.0.1:8500"` 94 | > This is the address of the Consul agent. By default, this is 95 | 127.0.0.1:8500, which is the default bind and port for a local Consul 96 | agent. It is not recommended that you communicate directly with a Consul 97 | server, and instead communicate with the local Consul agent. There are many 98 | reasons for this, most importantly the Consul agent is able to multiplex 99 | connections to the Consul server and reduce the number of open HTTP 100 | connections. Additionally, it provides a "well-known" IP address for which 101 | clients can connect. 102 | 103 | 104 | ``Problem B1``: Use directories for `consul-template` config and [render multiple templates](https://github.com/hashicorp/consul-template#command-line-flags). 105 | 106 | ``Problem C1 draft``: Create cluster of gateways. Redirect if not accessible. -------------------------------------------------------------------------------- /docker-compose-example/nginx-setup/conf/config.hcl: -------------------------------------------------------------------------------- 1 | consul{ 2 | address = "consul1:8500" 3 | } 4 | 5 | template { 6 | source = "/etc/consul-template/templates/template.ctmpl" 7 | destination = "/etc/nginx/conf.d/default.conf" 8 | command = "/etc/init.d/nginx reload" 9 | command_timeout = "60s" 10 | } -------------------------------------------------------------------------------- /docker-compose-example/nginx-setup/templates/template.ctmpl: -------------------------------------------------------------------------------- 1 | upstream app-example { 2 | least_conn; 3 | {{range service "example-app"}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1; 4 | {{else}}server 127.0.0.1:65535; # force a 502{{end}} 5 | } 6 | server { 7 | listen 8080; 8 | server_name localhost; 9 | 10 | location / { 11 | proxy_pass http://app-example; 12 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 13 | proxy_set_header Host $host; 14 | proxy_set_header X-Real-IP $remote_addr; 15 | } 16 | } -------------------------------------------------------------------------------- /docker-push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -ne 1 ]; then 4 | echo "Provide version" 5 | exit 42 6 | fi 7 | 8 | version=":$1" 9 | 10 | docker push seges/nginx-consul$version 11 | -------------------------------------------------------------------------------- /example.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | ## How to build 4 | 5 | Execute `./build.sh` 6 | 7 | 8 | ## How to up 9 | 10 | from `./docker-compose-example` directory 11 | execute `docker-compose up -d` 12 | 13 | ## Check container statuses 14 | 15 | `docker-compose ps` 16 | 17 | ## Check nginx logs 18 | 19 | `docker-compose logs -f nginx` 20 | 21 | ## Testing part 22 | 23 | Perform several times 24 | 25 | `curl localhost:8080/health` OR from browser [localhost:8080/health](localhost:8080/health) 26 | 27 | Verify that load-balancer works properly and returns for each request one of three replicas 28 | 29 | Stop one replica application replica `docker-compose stop backend2` 30 | 31 | Perform requests on [localhost:8080/health](localhost:8080/health) 32 | 33 | Verify that load-balancer returns for each request one of two replicas 34 | 35 | Up replica back and repeat requests 36 | 37 | `docker-compose up -d backend2` 38 | 39 | ## Notes 40 | 41 | Consul UI is accessible on [localhost:8501](localhost:8501) 42 | -------------------------------------------------------------------------------- /example/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seges/docker-nginx-consul/19e7be1b69c017bc4916b30d72238e9f61235e03/example/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /example/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip -------------------------------------------------------------------------------- /example/Dockerfile: -------------------------------------------------------------------------------- 1 | # recommendation: change with alpine tag (ligthweight) 2 | FROM openjdk:8 3 | COPY ./target/example-1.0-SNAPSHOT.jar . 4 | EXPOSE 3000 5 | CMD java -jar example-1.0-SNAPSHOT.jar -------------------------------------------------------------------------------- /example/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 | -------------------------------------------------------------------------------- /example/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 | -------------------------------------------------------------------------------- /example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | ru.zhenik.akka 8 | example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 1.8 14 | 1.8 15 | 1.3.3 16 | 1.2.1 17 | 10.1.3 18 | 2.5.14 19 | 20 | 21 | 22 | 23 | 24 | 25 | com.typesafe 26 | config 27 | ${config.version} 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | com.typesafe.akka 36 | akka-http_2.12 37 | ${akka.http.version} 38 | 39 | 40 | com.typesafe.akka 41 | akka-http-core_2.12 42 | ${akka.http.version} 43 | 44 | 45 | 46 | com.typesafe.akka 47 | akka-actor_2.12 48 | ${akka.actor.version} 49 | 50 | 51 | 52 | com.typesafe.akka 53 | akka-stream_2.12 54 | ${akka.actor.version} 55 | 56 | 57 | 58 | 59 | 60 | 61 | com.orbitz.consul 62 | consul-client 63 | ${consul.client.version} 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.apache.maven.plugins 74 | maven-compiler-plugin 75 | 3.5.1 76 | 77 | 1.8 78 | 1.8 79 | 80 | 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-shade-plugin 86 | 3.1.1 87 | 88 | 89 | 90 | package 91 | 92 | shade 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | ru.zhenik.akka.example.Boot 101 | 102 | 103 | reference.conf 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /example/readme.md: -------------------------------------------------------------------------------- 1 | ## Application example 2 | 3 | RESTapi service. 4 | 5 | ### Tech stack 6 | 7 | * [Akka (actor, http, stream)](https://akka.io/) 8 | * [Consul-client java](https://github.com/rickfast/consul-client) 9 | * [Shade plugin](https://maven.apache.org/plugins/maven-shade-plugin/) for fat-jar 10 | * Java 8 11 | * Docker 12 | 13 | ### REST 14 | 15 | | REQUEST | RESPONSE | 16 | | ------------- |:-------------: | 17 | | GET /health | 200 OK and app-id | 18 | | POST /app | 200 OK | 19 | 20 | 21 | ### Notes 22 | 23 | Pay attention to config file `application.conf`, it is loaded by [typesafe config](https://github.com/lightbend/config). 24 | 25 | `DiscoveryAgentActor` responsible for registration this app instance in consul and send each (10 sec default) health check to consul. 26 | 27 | 28 | -------------------------------------------------------------------------------- /example/src/main/java/ru/zhenik/akka/example/AppConfiguration.java: -------------------------------------------------------------------------------- 1 | package ru.zhenik.akka.example; 2 | 3 | import com.typesafe.config.Config; 4 | import com.typesafe.config.ConfigFactory; 5 | 6 | public class AppConfiguration { 7 | public final String appId; 8 | public final String serviceName; 9 | public final String host; 10 | public final Integer port; 11 | public final ServiceDiscoveryConfiguration serviceDiscoveryConfiguration; 12 | 13 | private AppConfiguration(final String appId){ 14 | // load application.conf file 15 | Config config = ConfigFactory.load(); 16 | 17 | this.appId = appId; 18 | this.serviceName = config.getString("service.name"); 19 | this.host = config.getString("application.host"); 20 | this.port = config.getInt("application.port"); 21 | this.serviceDiscoveryConfiguration = 22 | new ServiceDiscoveryConfiguration( 23 | config.getString("discovery.host"), 24 | config.getInt("discovery.port"), 25 | config.getLong("discovery.healthcheck-timeout") 26 | ); 27 | 28 | } 29 | 30 | public static AppConfiguration loadConfig(final String appId) { 31 | return new AppConfiguration(appId); 32 | } 33 | 34 | public final static class ServiceDiscoveryConfiguration { 35 | public final String host; 36 | public final int port; 37 | public final long healthCheckTimeout; 38 | private ServiceDiscoveryConfiguration(String host, int port, long healthCheckTimeout) { 39 | this.host = host; 40 | this.port = port; 41 | this.healthCheckTimeout = healthCheckTimeout; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /example/src/main/java/ru/zhenik/akka/example/Boot.java: -------------------------------------------------------------------------------- 1 | package ru.zhenik.akka.example; 2 | 3 | import akka.NotUsed; 4 | import akka.actor.ActorRef; 5 | import akka.actor.ActorSystem; 6 | import akka.http.javadsl.ConnectHttp; 7 | import akka.http.javadsl.Http; 8 | import akka.http.javadsl.ServerBinding; 9 | import akka.http.javadsl.model.HttpRequest; 10 | import akka.http.javadsl.model.HttpResponse; 11 | import akka.stream.ActorMaterializer; 12 | import akka.stream.Materializer; 13 | import akka.stream.javadsl.Flow; 14 | import java.util.UUID; 15 | import java.util.concurrent.CompletionStage; 16 | import ru.zhenik.akka.example.infrastructure.discovery.DiscoveryAgentActor; 17 | import ru.zhenik.akka.example.interfaces.rest.AppResource; 18 | 19 | public class Boot { 20 | 21 | public static void main(String[] args) { 22 | // config 23 | final String appId = UUID.randomUUID().toString(); 24 | final AppConfiguration appConfig = AppConfiguration.loadConfig(appId); 25 | 26 | // actor system init 27 | final ActorSystem system = ActorSystem.create(); 28 | final Materializer materializer = ActorMaterializer.create(system); 29 | 30 | // service discovery actor 31 | final ActorRef serviceDiscoveryActor = system.actorOf(DiscoveryAgentActor.props(appConfig), "example-app-consul-service"); 32 | 33 | // http init 34 | final Flow routeFlow = new AppResource(appConfig).routes().flow(system, materializer); 35 | final CompletionStage binding = Http 36 | .get(system) 37 | .bindAndHandle( 38 | routeFlow, 39 | ConnectHttp.toHost(appConfig.host, appConfig.port), 40 | materializer 41 | ); 42 | 43 | // exception handling 44 | binding.exceptionally(failure -> { 45 | System.err.println("Something very bad happened! " + failure.getMessage()); 46 | system.terminate(); 47 | return null; 48 | }); 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /example/src/main/java/ru/zhenik/akka/example/infrastructure/discovery/DiscoveryAgentActor.java: -------------------------------------------------------------------------------- 1 | package ru.zhenik.akka.example.infrastructure.discovery; 2 | 3 | import akka.actor.AbstractActor; 4 | import akka.actor.ActorRef; 5 | import akka.actor.Props; 6 | import com.google.common.net.HostAndPort; 7 | import com.orbitz.consul.AgentClient; 8 | import com.orbitz.consul.Consul; 9 | import com.orbitz.consul.NotRegisteredException; 10 | import com.orbitz.consul.model.agent.ImmutableRegistration; 11 | import com.orbitz.consul.model.agent.Registration; 12 | import java.util.Collections; 13 | import java.util.concurrent.TimeUnit; 14 | import ru.zhenik.akka.example.AppConfiguration; 15 | import scala.concurrent.duration.FiniteDuration; 16 | 17 | /** 18 | * 19 | * */ 20 | public class DiscoveryAgentActor extends AbstractActor { 21 | 22 | public static Props props(AppConfiguration config) { 23 | return Props.create(DiscoveryAgentActor.class, () -> new DiscoveryAgentActor(config)); 24 | } 25 | 26 | private final AppConfiguration configuration; 27 | private final Consul consul; 28 | private final AgentClient agentClient; 29 | private final FiniteDuration SCHEDULED_WORK_DELAY; 30 | 31 | public DiscoveryAgentActor(AppConfiguration configuration){ 32 | this.configuration = configuration; 33 | this.SCHEDULED_WORK_DELAY = new FiniteDuration(configuration.serviceDiscoveryConfiguration.healthCheckTimeout, TimeUnit.SECONDS); 34 | 35 | // todo: terminate system if error occur while connecting to consul 36 | // get consul connection 37 | this.consul = Consul 38 | .builder() 39 | .withHostAndPort( 40 | HostAndPort.fromParts( 41 | configuration.serviceDiscoveryConfiguration.host, 42 | configuration.serviceDiscoveryConfiguration.port) 43 | ) 44 | .build(); 45 | 46 | // get agent 47 | agentClient = consul.agentClient(); 48 | // set registration config 49 | Registration service = ImmutableRegistration.builder() 50 | .id(configuration.appId) 51 | .name(configuration.serviceName) 52 | .port(configuration.port) 53 | .address(configuration.host) 54 | .check(Registration.RegCheck.ttl(configuration.serviceDiscoveryConfiguration.healthCheckTimeout)) 55 | .tags(Collections.singletonList("tag1")) 56 | .meta(Collections.singletonMap("version", "1.0")) 57 | .build(); 58 | 59 | // register service 60 | agentClient.register(service); 61 | // check in with Consul, serviceId required only. client will prepend "service:" for service level checks. 62 | // Note that you need to continually check in before the TTL expires, otherwise your service's state will be marked as "critical". 63 | } 64 | 65 | @Override 66 | public void preStart() { 67 | getSelf().tell("Do Scheduled Work", ActorRef.noSender()); 68 | } 69 | 70 | @Override 71 | public Receive createReceive() { 72 | return receiveBuilder() 73 | .matchEquals("Do Scheduled Work", work -> { 74 | sendHealthCheck(); 75 | context().system() 76 | // send each (10seconds default) health-check to consul 77 | .scheduler() 78 | .schedule( 79 | // delay before 1st request 80 | new FiniteDuration(5, TimeUnit.SECONDS), 81 | SCHEDULED_WORK_DELAY, 82 | healthCheck(), 83 | getContext().dispatcher() 84 | ); 85 | }) 86 | .build(); 87 | } 88 | 89 | private void sendHealthCheck() { 90 | try { 91 | agentClient.pass(configuration.appId, configuration.serviceName +" alive and reachable"); 92 | } catch (NotRegisteredException e) { 93 | e.printStackTrace(); 94 | getContext().getSystem().terminate(); 95 | } 96 | System.out.println("Health check has been sent"); 97 | } 98 | 99 | private Runnable healthCheck() { 100 | return this::sendHealthCheck; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /example/src/main/java/ru/zhenik/akka/example/interfaces/rest/AppResource.java: -------------------------------------------------------------------------------- 1 | package ru.zhenik.akka.example.interfaces.rest; 2 | 3 | import static akka.http.javadsl.server.Directives.complete; 4 | import static akka.http.javadsl.server.Directives.get; 5 | import static akka.http.javadsl.server.Directives.path; 6 | import static akka.http.javadsl.server.Directives.post; 7 | import static akka.http.javadsl.server.Directives.route; 8 | 9 | import akka.http.javadsl.model.ContentTypes; 10 | import akka.http.javadsl.model.HttpEntities; 11 | import akka.http.javadsl.model.StatusCodes; 12 | import akka.http.javadsl.server.Route; 13 | import ru.zhenik.akka.example.AppConfiguration; 14 | 15 | public final class AppResource { 16 | 17 | public final AppConfiguration config; 18 | public final String healthResponse; 19 | 20 | public AppResource(AppConfiguration config) { 21 | this.config = config; 22 | this.healthResponse = String 23 | .format("ok \nservice-name:[%s]\napp-id:[%s]\nhost&port:[%s]", config.serviceName, 24 | config.appId, config.host + ":" + config.port); 25 | } 26 | 27 | public Route routes() { 28 | 29 | Route managementRoutes = path("health", () -> 30 | get(() -> 31 | complete(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, healthResponse)) 32 | ) 33 | ); 34 | Route appRoutes = path("app", () -> 35 | post(() -> 36 | complete(StatusCodes.OK) 37 | ) 38 | ); 39 | 40 | return route(managementRoutes, appRoutes); 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /example/src/main/resources/application.conf: -------------------------------------------------------------------------------- 1 | application { 2 | // use 0.0.0.0 instead localhost 3 | // https://github.com/moby/moby/issues/23082 4 | host = "0.0.0.0" 5 | host = ${?APPLICATION_HOST} 6 | port = 3000 7 | port = ${?APPLICATION_PORT} 8 | } 9 | // will be use when register in consul 10 | service.name = "example-app" 11 | 12 | // self registration pattern 13 | // https://microservices.io/patterns/self-registration.html 14 | discovery { 15 | host = "0.0.0.0" 16 | host = ${?DISCOVERY_HOST} 17 | port = 8500 18 | port = ${?DISCOVERY_PORT} 19 | healthcheck-timeout = 10 20 | healthcheck-timeout = ${?DISCOVERY_HEALTH_TIMEOUT} 21 | } 22 | -------------------------------------------------------------------------------- /nginx.service: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | /usr/sbin/nginx -c /etc/nginx/nginx.conf -t && \ 4 | exec /usr/sbin/nginx -c /etc/nginx/nginx.conf -g "daemon off;" 5 | --------------------------------------------------------------------------------