Skip to content
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
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
language: java
jdk: oraclejdk8
jdk:
- openjdk8

services:
- docker
- docker

script: mvn clean install
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build Status](https://travis-ci.org/ExampleDriven/spring-boot-docker-example.svg?branch=master)](https://travis-ci.org/ExampleDriven/spring-boot-docker-example)
# spring-boot-docker-example

This is the source code for the blog post
This is the source code for the blog post :

https://exampledriven.wordpress.com/2016/06/24/spring-boot-docker-example/

Expand All @@ -28,6 +28,7 @@ docker-compose scale echo-service=2
```

## Useful docker commands

```sh
##Starting multiple echo services
docker-compose scale echo-service=3
Expand All @@ -38,14 +39,12 @@ docker-compose stop echo-service
docker-compose up -d echo-service
```

Once all the services are up, the following URLs will be available
Once all the services are up, the following URLs will be available:

Address | Description
--- | ---
http://<\<docker-host>\>:8761 | Eureka service.
http://<\<docker-host>\>:8761 | Eureka service
http://<\<docker-host>\>:9090/routes | Zuul route definitions
http://<\<docker-host>\>:9090/api/echo-service/echo | Echo service through Zuul api gateway, looked up from Eureka registry
http://<\<docker-host>\>:9090/api/echo-service/echo/remote-echo | Echo service calling remote echo services
http://<\<docker-host>\>:9090/api/echo-service-by-dns/echo/remote-echo | Echo service through Zuul api gateway, located by DNS entry http://echo-service:9098


http://<\<docker-host>\>:9090/api/echo-service-by-dns/echo/remote-echo | Echo service through Zuul api gateway, located by DNS entry http://echo-service:9098
9 changes: 8 additions & 1 deletion echo-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>

</dependencies>

Expand Down Expand Up @@ -81,7 +86,9 @@
<configuration>
<imageName>${project.artifactId}</imageName>
<baseImage>java:8</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar", "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n"]</entryPoint>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar", "-Xdebug
-Xrunjdwp:server=y,transport=dt_socket,suspend=n"]
</entryPoint>
<!-- copy the service's jar file from target into the root directory of the image -->
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public class EchoServiceApplication {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(EchoServiceApplication.class, args);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.exampledriven.docker.echo;
package org.exampledriven.docker.echo.config;

import com.netflix.appinfo.AmazonInfo;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
public class HostFactory {

private HostFactory(){
}

public static HostInfo create(HttpServletRequest request) {
return new HostInfo(request.getLocalAddr(), request.getLocalPort(), getHostName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
package org.exampledriven.docker.echo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* Created by Peter_Szanto on 6/16/2016.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class HostInfo {

private String address;
private int port;
private String hostName;

public HostInfo() {
}

public HostInfo(String address, int port, String hostName) {
this.address = address;
this.port = port;
this.hostName = hostName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getHostName() {
return hostName;
}

public void setHostName(String hostName) {
this.hostName = hostName;
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
package org.exampledriven.docker.echo.rest;

import org.exampledriven.docker.echo.domain.HostInfo;
import org.exampledriven.docker.echo.domain.HostFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.exampledriven.docker.echo.domain.HostInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class EchoController {

@RequestMapping(value = "/echo", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/echo", produces = "application/json")
public HostInfo getHost(HttpServletRequest request) {

return HostFactory.create(request);

}

@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/", produces = "application/json")
public Map getFullInfo(HttpServletRequest request) {

HashMap result = new HashMap<>();

result.put("host", HostFactory.create(request));

result.put("env", System.getenv());

return result;

}


}
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
package org.exampledriven.docker.echo.rest;

import org.exampledriven.docker.echo.Config;
import lombok.RequiredArgsConstructor;
import org.exampledriven.docker.echo.config.Config;
import org.exampledriven.docker.echo.domain.HostFactory;
import org.exampledriven.docker.echo.domain.HostInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/echo")
@RequiredArgsConstructor
public class RemoteEchoController {

@Autowired
@Qualifier(Config.LOADBALANCED)
RestTemplate loadBalancedRestTemplate;
private final RestTemplate loadBalancedRestTemplate;

@Autowired
@Qualifier(Config.STANDARD)
RestTemplate restTemplate;
private final RestTemplate restTemplate;

@Autowired
RemoteHostInfoClient remoteHostInfoClient;
private final RemoteHostInfoClient remoteHostInfoClient;

@GetMapping(value = "/remote-echo", produces = "application/json")
public Map<String, HostInfo> getHost(HttpServletRequest request) {

Map result = new HashMap<String, HostInfo>();
Map<String, HostInfo> result = new HashMap<>();
result.put("local", HostFactory.create(request));

getRemoteHostInfo(result, "http://example_echo-service:9098/echo (Docker swarm service)", restTemplate, "http://example_echo-service:9098/echo");
getRemoteHostInfo(result, "http://echo-service:9098/echo (Docker compose DNS entry)", restTemplate, "http://echo-service:9098/echo");
getRemoteHostInfo(result, "http://echo-service/echo (Ribbon+Eureka)", loadBalancedRestTemplate, "http://echo-service/echo");

return result;

}

private void getRemoteHostInfo(Map result, String key, RestTemplate loadBalancedRestTemplate, String url) {
private void getRemoteHostInfo(Map<String, HostInfo> result, String key, RestTemplate loadBalancedRestTemplate, String url) {
HostInfo remoteHostInfo = null;
try {
remoteHostInfo = remoteHostInfoClient.getRemoteHostInfo(loadBalancedRestTemplate, url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
public class RemoteHostInfoClient {

public HostInfo getRemoteHostInfo(RestTemplate restTemplate, String url) {

HostInfo hostInfo = restTemplate.exchange(
return restTemplate.exchange(
url,
HttpMethod.GET,
null,
new ParameterizedTypeReference<HostInfo>() {})
.getBody();

return hostInfo;

new ParameterizedTypeReference<HostInfo>() {
})
.getBody();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
public class Application {
public class EurekaServerApplication {

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}

}