forked from burrsutter/9stepsawesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ba78a2
commit ca5ed80
Showing
5 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
12 changes: 12 additions & 0 deletions
12
hello/springbootjib/src/main/java/com/burrsutter/HellobootApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
hello/springbootjib/src/main/java/com/burrsutter/MyRESTController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |