diff --git a/hello/springbootfabric8/pom.xml b/hello/springbootfabric8/pom.xml index a3cc9ae..0583d72 100644 --- a/hello/springbootfabric8/pom.xml +++ b/hello/springbootfabric8/pom.xml @@ -44,7 +44,7 @@ io.fabric8 fabric8-maven-plugin - 3.5.40 + 3.5.41 fmp diff --git a/hello/springbootjib/pom.xml b/hello/springbootjib/pom.xml new file mode 100644 index 0000000..c71e51a --- /dev/null +++ b/hello/springbootjib/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.burrsutter + boot-demo + 0.0.1 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + com.google.cloud.tools + jib-maven-plugin + 0.9.11 + + + + \ No newline at end of file diff --git a/hello/springbootjib/readme.txt b/hello/springbootjib/readme.txt new file mode 100644 index 0000000..27df32b --- /dev/null +++ b/hello/springbootjib/readme.txt @@ -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 + diff --git a/hello/springbootjib/src/main/java/com/burrsutter/HellobootApplication.java b/hello/springbootjib/src/main/java/com/burrsutter/HellobootApplication.java new file mode 100644 index 0000000..e9523f1 --- /dev/null +++ b/hello/springbootjib/src/main/java/com/burrsutter/HellobootApplication.java @@ -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); + } +} diff --git a/hello/springbootjib/src/main/java/com/burrsutter/MyRESTController.java b/hello/springbootjib/src/main/java/com/burrsutter/MyRESTController.java new file mode 100644 index 0000000..bc2323a --- /dev/null +++ b/hello/springbootjib/src/main/java/com/burrsutter/MyRESTController.java @@ -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 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() { + + // ..svc.cluster.local + String url = "http://mynode.yourspace.svc.cluster.local:8000/"; + + ResponseEntity 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); + } + +} \ No newline at end of file