Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separating the services into different POMs and adding docker #7

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 88 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
*.class
.classpath
.project

### Eclipse template
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# Eclipse Core
.project

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Java annotation processor (APT)
.factorypath

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse

target/

Expand All @@ -23,3 +61,51 @@ desktop.ini

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,44 @@ You may find it easier to view the different applications by running them from a

To do this, open three CMD windows (Windows) or three Terminal windows (MacOS, Linux) and arrange so you can view them conveniently.

1. In each window, change to the directory where you cloned the demo.
1. In each window, change to the directory where you cloned the demo and then to the folder containing the relevant service.
1. In the first window, build the application using `mvn clean package`
1. In the same window run: `java -jar target/microservice-demo-0.0.1-SNAPSHOT.jar registration`
1. Switch to the second window and run: `java -jar target/microservice-demo-0.0.1-SNAPSHOT.jar accounts`
1. In the third window run: `java -jar target/microservice-demo-0.0.1-SNAPSHOT.jar web`
1. In the same window run: `spring-boot:run`
1. Switch to the second window and run: `spring-boot:run`
1. In the third window run: `spring-boot:run`
1. In your favorite browser open the same two links: [http://localhost:1111](http://localhost:1111) and [http://localhost:3333](http://localhost:3333)

You should see servers being registered in the log output of the first (registration) window.
As you interact you should logging in the second and third windows.

1. In a new window, run up a second account-server using HTTP port 2223:
* `java -jar target/microservice-demo-0.0.1-SNAPSHOT.jar accounts 2223`
1. Allow it to register itself
1. Kill the first account-server and see the web-server switch to using the new account-server - no loss of service.
## Using Docker
You may want to run the services in Docker containers. The following instructions will help you run the demo using three containers on a single host:
To use this instructions you should have docker installed and a host running the Docker deamon.

- change directory to the root directory of the project
- run `mvn clean package`

Step 1 - build the docker images:
- to create the image for the discovery service run: `docker build -t spring-cloud-demo-discovery discovery-service`
- to create the image for the account service run: `docker build -t spring-cloud-demo-account account-service`
- to create the image for the web service run: `docker build -t spring-cloud-demo-web web-service`

Step 2 - create a network for the containers
- run: `docker network create spring-cloud-demo`

Step 3 - run the containers:
In 3 different terminal windows run the following commands
- to run the container for the discovery service run: `docker run -it --net=spring-cloud-demo -p 1111:1111 --name discovery --hostname=discovery spring-cloud-demo-discovery`
- to run the container for the account service run: `docker run -it --net=spring-cloud-demo --name account --hostname=account spring-cloud-demo-account`
- to run the container for the web service run: `docker run -it --net=spring-cloud-demo -p 80:3333 --name web --hostname=web spring-cloud-demo-web`

Step 4 - use the application using your browser:
- find the ip of the host machine. to do so type `docker-machine ls` and find the ip of the host you are using.
- browse [http://your-host-ip](http://your-host-ip) to access the web service
- browse [http://your-host-ip:1111](http://your-host-ip:1111) to access the discovery service console

Step 5 - run another account service and check the load balancing and failover:
- to run the container for the account service run: `docker run -it --net=spring-cloud-demo --name account-2 --hostname=account-2 spring-cloud-demo-account`
- browse [http://your-host-ip](http://your-host-ip) to access the web service
- browse [http://your-host-ip:1111](http://your-host-ip:1111) to access the discovery service console

4 changes: 4 additions & 0 deletions account-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM java:8
COPY target/microservice-demo-accounts-0.0.1-SNAPSHOT.jar /usr/src/microservice-demo-accounts-0.0.1-SNAPSHOT.jar
WORKDIR /usr/src
CMD java -jar -Dspring.profiles.active=docker microservice-demo-accounts-0.0.1-SNAPSHOT.jar
61 changes: 61 additions & 0 deletions account-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.service.service</groupId>
<artifactId>microservice-demo-accounts</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.samples.service.service</groupId>
<artifactId>microservice-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<start-class>io.pivotal.microservices.services.accounts.AccountsServer</start-class>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<!-- Setup Spring Data common components -->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>


<dependency>
<!-- Setup Spring Data JPA Repository support -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<!-- In-memory database for testing/demos -->
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
spring:
application:
name: accounts-service # Service registers under this name
freemarker:
enabled: false # Ignore Eureka dashboard FreeMarker templates
thymeleaf:
cache: false # Allow Thymeleaf templates to be reloaded at runtime
prefix: classpath:/accounts-server/templates/ # Trailing / mandatory
Expand All @@ -22,6 +20,17 @@ eureka:
# HTTP Server
server:
port: 2222 # HTTP (Tomcat) port



---

spring:
profiles: docker
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://discovery:1111/eureka/


4 changes: 4 additions & 0 deletions discovery-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM java:8
COPY target/microservice-demo-discovery-0.0.1-SNAPSHOT.jar /usr/src/microservice-demo-discovery-0.0.1-SNAPSHOT.jar
WORKDIR /usr/src
CMD java -jar microservice-demo-discovery-0.0.1-SNAPSHOT.jar
19 changes: 19 additions & 0 deletions discovery-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.service.service</groupId>
<artifactId>microservice-demo-discovery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.samples.service.service</groupId>
<artifactId>microservice-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<start-class>io.pivotal.microservices.services.registration.RegistrationServer</start-class>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.pivotal.microservices.services;


import io.pivotal.microservices.services.registration.RegistrationServer;


import org.springframework.boot.SpringApplication;

/**
* Allow the servers to be invoke from the command-line. The jar is built with
* this as the <code>Main-Class</code> in the jar's <code>MANIFEST.MF</code>.
*
* @author Paul Chapman
*/
public class Main {

public static void main(String[] args) {

if (args.length > 0)
System.setProperty("server.port", args[1]);


SpringApplication.run(RegistrationServer.class, args);


}

protected static void usage() {
System.out.println("Usage: java -jar ... <server-name> [server-port]");
System.out.println(" where server-name is 'registration', "
+ "'accounts' or 'web' and server-port > 1024");
}
}
26 changes: 26 additions & 0 deletions discovery-service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- configuration file for LogBack (slf4J implementation)
See here for more details: http://gordondickens.com/wordpress/2013/03/27/sawing-through-the-java-loggers/ -->
<configuration scan="true" scanPeriod="30 seconds">

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>

<!-- To enable JMX Management -->
<jmxConfigurator/>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${PID}: %-5level %logger{0} - %msg%n</pattern>
</encoder>
</appender>

<!-- Specify logging levels -->
<logger name="org.springframework" level="warn"/>
<logger name="org.hibernate" level="warn"/>

<root level="info">
<appender-ref ref="console"/>
</root>
</configuration>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions discovery-service/src/main/resources/public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* - - - - - - - - - - - *
Local Styles
* - - - - - - - - - - - */

body {
font-family: helvetica;
font-size: 13pt;
}

.navbar, .navbar > a {
font-family: helvetica;
font-size: 15pt;
}

ul.openlist li {
margin-bottom: 1em;
}

a {
font-family: helvetica;
}

code {
background-color: white;
border-radius: 0px;
color: #80A080;
font-size: 100%;
font-family: monospace;
padding: 0px;
white-space: nowrap;
}

footer {
margin-top: 3em;
border-top: 1px black solid;
text-align: center;
color:#6DB33F; /* Spring Green */
font-style: italic;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ eureka:
server:
port: 1111 # HTTP (Tomcat) port

# Discovery Server Dashboard uses FreeMarker. Don't want Thymeleaf templates
spring:
thymeleaf:
enabled: false # Disable Thymeleaf
Loading