Skip to content

Commit

Permalink
adding jib example
Browse files Browse the repository at this point in the history
  • Loading branch information
burrsutter committed Oct 3, 2018
1 parent 4ba78a2 commit ca5ed80
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hello/springbootfabric8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.40</version>
<version>3.5.41</version>
<executions>
<execution>
<id>fmp</id>
Expand Down
41 changes: 41 additions & 0 deletions hello/springbootjib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>com.burrsutter</groupId>
<artifactId>boot-demo</artifactId>
<version>0.0.1</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>

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

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.11</version>
</plugin>
</plugins>
</build>
</project>
13 changes: 13 additions & 0 deletions hello/springbootjib/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This example builds a linux container image, without a Dockerfile, via jib

https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#example


eval $(minishift docker-env)

mvn compile jib:dockerBuild -Dimage=9stepsawesome/myboot:v1

docker run -it -p 8080:8080 9stepsawesome/myboot:v1

curl $(minishift ip):8080

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.burrsutter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HellobootApplication {

public static void main(String[] args) {
SpringApplication.run(HellobootApplication.class, args);
}
}
107 changes: 107 additions & 0 deletions hello/springbootjib/src/main/java/com/burrsutter/MyRESTController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.burrsutter;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

@RestController
public class MyRESTController {
@Autowired
private Environment environment;

final String hostname = System.getenv().getOrDefault("HOSTNAME", "unknown");
String greeting = "Aloha";

private int count = 0; // simple counter to see lifecycle

RestTemplate restTemplate = new RestTemplate();

@RequestMapping("/")
public String sayHello() {
greeting = environment.getProperty("GREETING","Aloha");
System.out.println(greeting + " from " + hostname);
return greeting + " from Spring Boot! " + count++ + " on " + hostname + "\n";
}

@RequestMapping("/sysresources")
public String getSystemResources() {
long memory = Runtime.getRuntime().maxMemory();
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("/sysresources " + hostname);
return
" Memory: " + (memory / 1024 / 1024) +
" Cores: " + cores + "\n";
}

@RequestMapping("/consume")
public String consumeSome() {
System.out.println("/consume " + hostname);

Runtime rt = Runtime.getRuntime();
StringBuilder sb = new StringBuilder();
long maxMemory = rt.maxMemory();
long usedMemory = 0;
// while usedMemory is less than 80% of Max
while (((float) usedMemory / maxMemory) < 0.80) {
sb.append(System.nanoTime() + sb.toString());
usedMemory = rt.totalMemory();
}
String msg = "Allocated about 80% (" + humanReadableByteCount(usedMemory, false) + ") of the max allowed JVM memory size ("
+ humanReadableByteCount(maxMemory, false) + ")";
System.out.println(msg);
return msg + "\n";
}

@RequestMapping(method = RequestMethod.GET, value = "/health")
public ResponseEntity<String> health() {
// if (count++ < 5) {
// return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Bad");
// } else {
return ResponseEntity.status(HttpStatus.OK)
.body("I am fine, thank you\n");
// }
}

@RequestMapping("/configure")
public String configure() {
String databaseConn = environment.getProperty("DBCONN","Default");
String msgBroker = environment.getProperty("MSGBROKER","Default");
greeting = environment.getProperty("GREETING","Default");
String love = environment.getProperty("LOVE","Default");
return "Configuration for : " + hostname + "\n"
+ "databaseConn=" + databaseConn + "\n"
+ "msgBroker=" + msgBroker + "\n"
+ "greeting=" + greeting + "\n"
+ "love=" + love + "\n";
}

@RequestMapping("/callinganother")
public String callinganother() {

// <servicename>.<namespace>.svc.cluster.local
String url = "http://mynode.yourspace.svc.cluster.local:8000/";

ResponseEntity<String> response
= restTemplate.getForEntity(url, String.class);

String responseBody = response.getBody();
System.out.println(responseBody);

return responseBody;
}

public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit)
return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

}

0 comments on commit ca5ed80

Please sign in to comment.