Skip to content

Commit

Permalink
Merge pull request spring-projects#28 from mszarlinski/docker-compose
Browse files Browse the repository at this point in the history
closes spring-projects#2 Starting services with docker-compose
  • Loading branch information
mszarlinski committed Jan 9, 2017
2 parents d23ceb1 + 5a39df8 commit 82939fc
Show file tree
Hide file tree
Showing 23 changed files with 453 additions and 31 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@

This microservices branch was initially derived from [AngularJS version](https://github.com/spring-petclinic/spring-petclinic-angular1) to demonstrate how to split sample Spring application into [microservices](http://www.martinfowler.com/articles/microservices.html). To achieve that goal we used [Spring Cloud Netflix](https://github.com/spring-cloud/spring-cloud-netflix) technology stack.

## Starting services locally
## Starting services locally without Docker
Every microservice is a Spring Boot application and can be started locally using IDE or `mvn spring-boot:run` command. Please note that supporting services (Config and Discovery Server) must be started before any other application (Customers, Vets, Visits and API). Tracing server startup is optional.
If everything goes well, you can access the following services at given location:
* Discovery Server - http://localhost:8761
Expand All @@ -15,6 +15,14 @@ You can tell Config Server to use your local Git repository by using `local` Spr
`GIT_REPO` environment variable, for example:
`-Dspring.profiles.active=local -DGIT_REPO=/projects/spring-petclinic-microservices-config`

## Starting services locally with docker-compose
In order to start entire infrastructure using Docker, you have to build images by executing `mvn clean install -PbuildDocker`
from a project root. Once images are ready, you can start them with a single command
`docker-compose up`. Containers startup order is coordinated with [`wait-for-it.sh` script](https://github.com/vishnubob/wait-for-it).
After starting services it takes a while for API Gateway to be in sync with service registry,
so don't be scared of initial Zuul timeouts. You can track services availability using Eureka dashboard
available by default at http://localhost:8761.

## Understanding the Spring Petclinic application with a few diagrams
<a href="https://speakerdeck.com/michaelisvy/spring-petclinic-sample-application">See the presentation here</a>

Expand Down
86 changes: 86 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,86 @@
version: '2'
services:
config-server:
image: mszarlinski/spring-petclinic-config-server
container_name: config-server
ports:
- 8888:8888

discovery-server:
image: mszarlinski/spring-petclinic-discovery-server
container_name: discovery-server
links:
- config-server
depends_on:
- config-server
entrypoint: ["./wait-for-it.sh","config-server:8888","--timeout=60","--","java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8761:8761

customers-service:
image: mszarlinski/spring-petclinic-customers-service
container_name: customers-service
links:
- config-server
- discovery-server
depends_on:
- config-server
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8081:8081

visits-service:
image: mszarlinski/spring-petclinic-visits-service
container_name: visits-service
links:
- config-server
- discovery-server
depends_on:
- config-server
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8082:8082

vets-service:
image: mszarlinski/spring-petclinic-vets-service
container_name: vets-service
links:
- config-server
- discovery-server
depends_on:
- config-server
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8083:8083

api-gateway:
image: mszarlinski/spring-petclinic-api-gateway
container_name: api-gateway
links:
- config-server
- discovery-server
- customers-service
- visits-service
- vets-service
depends_on:
- config-server
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8080:8080

tracing-server:
image: mszarlinski/spring-petclinic-tracing-server
container_name: tracing-server
links:
- config-server
- discovery-server
depends_on:
- config-server
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 9411:9411
3 changes: 3 additions & 0 deletions pom.xml
Expand Up @@ -30,6 +30,9 @@
<assertj.version>3.5.2</assertj.version>
<spring-cloud.version>Camden.SR1</spring-cloud.version>
<java.version>1.8</java.version>

<docker.image.prefix>mszarlinski</docker.image.prefix>
<docker.plugin.version>0.4.13</docker.plugin.version>
</properties>

<dependencyManagement>
Expand Down
61 changes: 36 additions & 25 deletions spring-petclinic-api-gateway/pom.xml
Expand Up @@ -82,7 +82,6 @@
</dependencies>

<build>
<finalName>petclinic</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -127,31 +126,43 @@
</generateGitPropertiesFilename>
</configuration>
</plugin>

<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${docker.image.prefix}/springboot-petclinic</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<forceTags>true</forceTags>
<imageTags>
<imageTag>${project.version}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<useConfigFile>true</useConfigFile>
</configuration>
</plugin>
</plugins>
</build>


<profiles>
<profile>
<id>buildDocker</id>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<buildArgs>
<ARTIFACT_NAME>${project.build.finalName}</ARTIFACT_NAME>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
10 changes: 10 additions & 0 deletions spring-petclinic-api-gateway/src/main/docker/Dockerfile
@@ -0,0 +1,10 @@
FROM java:8
VOLUME /tmp
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh wait-for-it.sh
RUN bash -c 'chmod +x wait-for-it.sh'
ARG ARTIFACT_NAME
ADD ${ARTIFACT_NAME}.jar /app.jar
ENV SPRING_PROFILES_ACTIVE docker
RUN bash -c 'touch /app.jar'
EXPOSE 8081
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
6 changes: 6 additions & 0 deletions spring-petclinic-api-gateway/src/main/resources/bootstrap.yml
Expand Up @@ -4,3 +4,9 @@ spring:
uri: http://localhost:8888
application:
name: api-gateway
---
spring:
profiles: docker
cloud:
config:
uri: http://config-server:8888
37 changes: 36 additions & 1 deletion spring-petclinic-config-server/pom.xml
Expand Up @@ -40,5 +40,40 @@
</plugins>
</build>


<profiles>
<profile>
<id>buildDocker</id>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<buildArgs>
<ARTIFACT_NAME>${project.build.finalName}</ARTIFACT_NAME>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
7 changes: 7 additions & 0 deletions spring-petclinic-config-server/src/main/docker/Dockerfile
@@ -0,0 +1,7 @@
FROM java:8
VOLUME /tmp
ARG ARTIFACT_NAME
ADD ${ARTIFACT_NAME}.jar /app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8888
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
37 changes: 36 additions & 1 deletion spring-petclinic-customers-service/pom.xml
Expand Up @@ -91,5 +91,40 @@
</plugins>
</build>


<profiles>
<profile>
<id>buildDocker</id>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<buildArgs>
<ARTIFACT_NAME>${project.build.finalName}</ARTIFACT_NAME>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
10 changes: 10 additions & 0 deletions spring-petclinic-customers-service/src/main/docker/Dockerfile
@@ -0,0 +1,10 @@
FROM java:8
VOLUME /tmp
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh wait-for-it.sh
RUN bash -c 'chmod +x wait-for-it.sh'
ARG ARTIFACT_NAME
ADD ${ARTIFACT_NAME}.jar /app.jar
ENV SPRING_PROFILES_ACTIVE docker
RUN bash -c 'touch /app.jar'
EXPOSE 8081
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Expand Up @@ -4,3 +4,9 @@ spring:
uri: http://localhost:8888
application:
name: customers-service
---
spring:
profiles: docker
cloud:
config:
uri: http://config-server:8888
37 changes: 36 additions & 1 deletion spring-petclinic-discovery-server/pom.xml
Expand Up @@ -44,5 +44,40 @@
</plugins>
</build>


<profiles>
<profile>
<id>buildDocker</id>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<buildArgs>
<ARTIFACT_NAME>${project.build.finalName}</ARTIFACT_NAME>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
10 changes: 10 additions & 0 deletions spring-petclinic-discovery-server/src/main/docker/Dockerfile
@@ -0,0 +1,10 @@
FROM java:8
VOLUME /tmp
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh wait-for-it.sh
RUN bash -c 'chmod +x wait-for-it.sh'
ARG ARTIFACT_NAME
ADD ${ARTIFACT_NAME}.jar /app.jar
ENV SPRING_PROFILES_ACTIVE docker
RUN bash -c 'touch /app.jar'
EXPOSE 8761
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Expand Up @@ -4,3 +4,9 @@ spring:
uri: http://localhost:8888
application:
name: discovery-server
---
spring:
profiles: docker
cloud:
config:
uri: http://config-server:8888

0 comments on commit 82939fc

Please sign in to comment.