A seed for building microservices with Scalatra, MongoDB and Docker.
Project goals and assumptions:
- provide a project template based on technologies, that are easily accessible for most Java developers (e.g. Scalatra and Jetty).
- showcase selected features of Scala, sbt and Docker.
For rapid development feedback use the sbt-revolver plugin:
sbt ~re-start
It will respawn the server process whenever a project file changes and a fresh build is required.
This section provides a step-by-step guide for hosting the service in a virtual Docker environment, including a standalone MongoDB instance. Prerequisites:
- Install Docker.
- Install Docker Machine, if it isn't included in the Docker distribution (useful for managing multiple servers in your local environment).
Create a virtual machine for each component of the system:
docker-machine create --driver virtualbox service1
docker-machine create --driver virtualbox mongo1
You'll need a MongoDB instance and some sample data to get started. Make sure, that your Docker client is connecting to the Mongo server:
eval "$(docker-machine env mongo1)"
Spawn a new Mongo process:
docker run -d --restart=always --name mongo -p 27017:27017 mongo:latest
You can test your connection from your development box by running:
mongo $(docker-machine ip mongo1)/test
Don't worry, if you don't have a Mongo client available locally. You can always launch it directly in the container:
docker exec -it mongo mongo test
Import sample data and create some custom indexes:
docker exec -i mongo mongoimport -d test -c locations --jsonArray < data/sample_locations.json
docker exec -i mongo mongo test < data/sample_location_indexes.js
Switch your Docker client to the target environment:
eval "$(docker-machine env service1)"
Build an image using the sbt-docker plugin:
sbt docker
You can verify the list of available images by running:
docker images
Run the image in a new Docker container as such:
docker run \
--name=location-provider \
-d --restart=always \
-e MONGO_HOST=$(docker-machine ip mongo1) \
-e MONGO_PORT=27017 \
-e MONGO_DB=test \
-p 8080:8080 \
com.mintbeans/scalatra-mongodb-seed:v0.1-SNAPSHOT
The -d
switch implies detaching the process from the current session. You can always follow the logs by executing:
docker logs -f location-provider
Check if everything works correctly by fetching the list of locations:
curl -v http://$(docker-machine ip service1):8080/locations
Swagger should be integrated by default. Point Swagger UI to http://localhost:8080/api-docs/ to interact with all exposed methods and to find out their specs.