Docker Normal Akka Application

Docker Normal Akka Application

1 Configuration Setting
First of all, I am using a normal Akka system application. It is not spray, not play framework. So how to work on the configuration, that is the first question.

I am using a trait to handle the configuration, all the configuration should be here.
package com.sillycat.jobsconsumer.utilities

import com.typesafe.config._

trait IncludeConfig extends IncludeLogger{

  val config = ConfigFactory.load()

  /** all the configuration */
  /** third-party service */
  val classifierEndpoint = config.getString("api.classifier.endpoint")
  val geoServerEndpoint = config.getString("api.geoserver.endpoint")

  /** actor related */
  val actorRawJobCount = config.getInt("actor.supervisor.rawjob.count")
  val actorRedisJobCount = config.getInt("actor.supervisor.redisjob.count")
  val actorSolrJobCount = config.getInt("actor.supervisor.solrjob.count")
  val actorPullingDurationMax = config.getInt("actor.supervisor.pulling.duration.max")

}

In the src/main/resources/, I have these configuration files.
application-local.conf
application.conf

The parameters to pick up the configuration file and log4j file.
-Dconfig.file=conf/application-stage.conf -Dlog4j.configuration=file:conf/log4j-stage.properties

2 Log4j Configuration
Just normal log4j configuration
log4j.rootCategory=WARN,stdout, R

# rolling log file ("jobs-consumer.log")
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern='.'yyyy-MM-dd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%5p [%t] %d{ISO8601} %F (line %L) %m%n
log4j.appender.R.File=logs/jobs-consumer.log

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] %d{ISO8601} %F (line %L) %m%n

# Device unregistration settings
log4j.logger.com.sillycat.jobsconsumer=DEBUG
log4j.logger.com.sillycat.services=DEBUG

It will be like this log4j-stage.properties, log4j.properties.
Command will be as follow parameters.
-Dconfig.file=conf/application-stage.conf -Dlog4j.configuration=file:conf/log4j-stage.properties

3 Build.sbt to Build Release
Plugins Configuration
logLevel := Level.Warn
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.6")

build.sbt Configuration
enablePlugins(JavaServerAppPackaging)

name := "jobs-consumer"

version := "1.0"

//for redis testing
resolvers += "rediscala" at "http://dl.bintray.com/etaty/maven"

//for solr-core testing
resolvers += "Restlet Repositories" at "http://maven.restlet.org"

libraryDependencies ++= Seq(
  "com.amazonaws" % "aws-java-sdk" % "1.10.6",                //
  "org.slf4j" % "slf4j-api" % "1.7.12",                       // MIT
  "org.slf4j" % "slf4j-log4j12" % "1.7.12",
  "com.typesafe" % "config" % "1.3.0",
  "com.etaty.rediscala" %% "rediscala" % "1.4.2",
  "org.msgpack" % "msgpack" % "0.6.12",
  "org.apache.solr" % "solr-solrj" % "5.3.0",
  "mysql"      %   "mysql-connector-java"      % "5.1.36",
  "com.thoughtworks.xstream" % "xstream" % "1.4.8",
  "commons-lang" % "commons-lang" % "2.6",                    //html escape
  "org.joda" % "joda-convert" % "1.7",
  "com.gu" %% "prequel" % "0.3.12",
  "io.spray" %% "spray-client" % "1.3.1",
  "io.spray" %% "spray-can" % "1.3.1",
  "io.spray" %% "spray-http" % "1.3.1",
  "io.spray" %% "spray-httpx" % "1.3.1",
  "io.spray" %% "spray-util" % "1.3.1",
  "io.spray" %% "spray-json" % "1.3.1",
  "com.typesafe.akka"   %% "akka-actor" % "2.3.2",
  "com.typesafe.akka"   %% "akka-testkit" % "2.3.2",
  "com.typesafe.akka"   %% "akka-transactor" % "2.3.2",
  "com.typesafe.akka"   %% "akka-kernel" % "2.3.2",
  "com.google.guava" % "guava" % "19.0",
  "com.google.code.findbugs" % "jsr305" % "3.0.1",
  "org.scalatest" %% "scalatest" % "2.2.0" % "test",          // Apache v2
  "org.mockito" % "mockito-all" % "1.9.5" % "test",           // MIT
  "com.orange.redis-embedded" % "embedded-redis" % "0.6" % "test",
  "org.apache.solr" % "solr-core" % "5.3.0" % "test"
)

mainClass in Compile := Some("com.sillycat.jobsconsumer.ServerBoot")

mappings in Universal ++= {
  val base = baseDirectory.value
  val confDir = base / "src" / "main" / "resources"

  for {
    (file, relativePath) <-  (confDir.** ("*.conf" || "*.properties") --- confDir) x relativeTo(confDir)
  } yield file -> s"conf/$relativePath"
}

After doing that, this command can generate the binary file.
>sbt clean update compile package universal:packageZipTarball

The docker related configuration will be as follow:
Dockerfile
#Run a Simple REST API based on playframework

#Prepre the OS
FROM            centos:7
MAINTAINER      Carl Luo <[email protected]>

ENV             DEBIAN_FRONTEND noninteractive

#Install Java
RUN             yum install -y java-1.8.0-openjdk

#Install the Application
RUN             mkdir /share/
WORKDIR         /share/

ADD             target/universal/jobs-consumer-1.0.tgz /share/

#Start the Application
RUN     mkdir -p /app/
ADD     start.sh /app/
WORKDIR /app
CMD    [ "./start.sh" ]

Makefile
IMAGE=sillycat/jobs-consumer
TAG=1.0
NAME=jobs-consumer
REPOSITORY=registry.sillycat.com

push-local:
    docker push  $(REPOSITORY)/$(IMAGE):$(TAG)

app-build:
    sbt clean update compile package universal:packageZipTarball

docker-context:

build: docker-context
    sudo docker build -t $(REPOSITORY)/$(IMAGE):$(TAG) .

run-stage:
    sudo docker run -v /opt/jobs-consumer/logs:/share/jobs-consumer-1.0/logs -d -e RUNNING_ENV=stage --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG)

debug:
    sudo docker run -v /opt/jobs-consumer/logs:/share/jobs-consumer-1.0/logs -ti -e RUNNING_ENV=stage --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG) /bin/bash

clean:
    sudo docker stop ${NAME}
    sudo docker rm ${NAME}

logs:
    sudo docker logs ${NAME}

publish:
    sudo docker push ${IMAGE}

start.sh
#!/bin/sh -ex

APPLICATION_SECRET="nosessionshere"

cd /share/jobs-consumer-1.0

bin/jobs-consumer \
        -Dconfig.file=conf/application-${RUNNING_ENV}.conf \
    -Dlog4j.configuration=file:conf/log4j-${RUNNING_ENV}.properties

References:
http://sillycat.iteye.com/blog/2123896

https://github.com/muuki88/sbt-native-packager-examples

https://github.com/muuki88/sbt-native-packager-examples/tree/master/assembly-one-jar

猜你喜欢

转载自sillycat.iteye.com/blog/2276825