Tuesday, 24 February 2015

Spring Boot - Hello World from the command line to IBM Bluemix in 1 minute

Here is how simple Spring Boot makes saying Hello World web based application with no IDE and no no need to package it up. Nearly as easy as NodeJS

1. Firstly install the Spring Boot CLI. From mac use brew as follows

pas@192-168-1-4:~$ brew tap pivotal/tap
Cloning into '/usr/local/Library/Taps/pivotal/homebrew-tap'...
remote: Counting objects: 366, done.
remote: Total 366 (delta 0), reused 0 (delta 0), pack-reused 366
Receiving objects: 100% (366/366), 60.09 KiB | 84.00 KiB/s, done.
Resolving deltas: 100% (195/195), done.
Checking connectivity... done.
Tapped 8 formulae
pas@192-168-1-4:~$ brew install springboot
==> Installing springboot from pivotal/homebrew-tap
==> Downloading https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/1.2.1.RELEASE/spring-boot-cli-1.2.1.RELEASE-bin.tar.gz
######################################################################## 100.0%
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/springboot/1.2.1.RELEASE: 6 files, 8.6M, built in 98 seconds


2. Now create a simple groovy file called hello.groovy as follows

  
@RestController
class HelloWorld {
  @RequestMapping("/")
  String home() {
    return "Hello World!"
  }
}

3. Run using the spring boot CLI as follows

pas@pass-mbp:~/ibm/software/spring/spring-boot/groovy-example$ spring run hello.groovy

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.1.RELEASE)

2015-02-24 21:13:13.544  INFO 7565 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on 192-168-1-4.tpgi.com.au with PID 7565 (/Users/pas/.m2/repository/org/springframework/boot/spring-boot/1.2.1.RELEASE/spring-boot-1.2.1.RELEASE.jar started by pas in /Users/pas/ibm/software/spring/spring-boot/groovy-example)
2015-02-24 21:13:13.667  INFO 7565 --- [       runner-0] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3b4b551f: startup date [Tue Feb 24 21:13:13 AEDT 2015]; root of context hierarchy
2015-02-24 21:13:14.189  INFO 7565 --- [       runner-0] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-02-24 21:13:14.663  INFO 7565 --- [       runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-02-24 21:13:14.796  INFO 7565 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-02-24 21:13:14.797  INFO 7565 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.15
2015-02-24 21:13:14.849  INFO 7565 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader  : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@463a88aa class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2015-02-24 21:13:14.868  INFO 7565 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-02-24 21:13:14.868  INFO 7565 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1203 ms
2015-02-24 21:13:15.304  INFO 7565 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-02-24 21:13:15.308  INFO 7565 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-02-24 21:13:15.308  INFO 7565 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-02-24 21:13:15.474  INFO 7565 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3b4b551f: startup date [Tue Feb 24 21:13:13 AEDT 2015]; root of context hierarchy
2015-02-24 21:13:15.513  INFO 7565 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String HelloWorld.home()
2015-02-24 21:13:15.514  INFO 7565 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-02-24 21:13:15.515  INFO 7565 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-02-24 21:13:15.537  INFO 7565 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-02-24 21:13:15.537  INFO 7565 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-02-24 21:13:15.568  INFO 7565 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-02-24 21:13:15.927  INFO 7565 --- [       runner-0] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-02-24 21:13:15.964  INFO 7565 --- [       runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-02-24 21:13:15.965  INFO 7565 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 2.568 seconds (JVM running for 3.81)


4. Invoke in a browser and you have your "Hello World" or just use "curl" as follows

pas@192-168-1-4:~/ibm/software/spring/spring-boot/groovy-example$ curl http://localhost:8080
Hello World!


5. Finally lets create a runnable JAR we can then deploy to IBM Bluemix

pas@192-168-1-4:~/ibm/software/spring/spring-boot/groovy-example$ spring jar hello.jar hello.groovy

6. Finally deploy to IBM Bluemix using the runnable JAR file as follows

pas@192-168-1-4:~/ibm/software/spring/spring-boot/groovy-example$ cf push hello-spring-boot -i 1 -m 256M -p ./hello.jar
Creating app hello-spring-boot in org pasapi@au1.ibm.com / space apple as pasapi@au1.ibm.com...
OK

Creating route hello-spring-boot.mybluemix.net...
OK

Binding hello-spring-boot.mybluemix.net to hello-spring-boot...
OK

Uploading hello-spring-boot...
Uploading app files from: ./hello.jar
Uploading 855.6K, 95 files
Done uploading
OK

Starting app hello-spring-boot in org pasapi@au1.ibm.com / space apple as pasapi@au1.ibm.com...
-----> Downloaded app package (15M)
-----> Liberty Buildpack Version: v1.13-20150209-1122
-----> Avoid Trouble: Specify a minimum of 512M as the Memory Limit for your apps when using IBM JDK.
-----> Retrieving IBM 1.7.1_sr2fp1ifx-20141220 JRE (ibm-java-jre-7.1-2.1-pxa6470_27sr2fp1ifx-20141220_02-sfj.tgz) ... (0.0s)
-----> Retrieving App Management Agent 2015.02.04_102631 (com.ibm.ws.cloudoe.app-mgmt-proxy-agent.zip) ... (0.0s)
         Expanding App Management to .app-management (0.0s)
-----> Downloading Auto Reconfiguration 1.7.0_RELEASE from https://download.run.pivotal.io/auto-reconfiguration/auto-reconfiguration-1.7.0_RELEASE.jar (0.8s)
-----> Liberty buildpack is done creating the droplet


0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App hello-spring-boot was started using this command `$PWD/.java/jre/bin/java -Xtune:virtualized -Xmx192M -Xdump:none -Xdump:heap:defaults:file=./../dumps/heapdump.%Y%m%d.%H%M%S.%pid.%seq.phd -Xdump:java:defaults:file=./../dumps/javacore.%Y%m%d.%H%M%S.%pid.%seq.txt -Xdump:snap:defaults:file=./../dumps/Snap.%Y%m%d.%H%M%S.%pid.%seq.trc -Xdump:heap+java+snap:events=user -Xdump:tool:events=systhrow,filter=java/lang/OutOfMemoryError,request=serial+exclusive,exec=./.buildpack-diagnostics/killjava.sh $JVM_ARGS org.springframework.boot.loader.JarLauncher --server.port=$PORT`

Showing health and status for app hello-spring-boot in org pasapi@au1.ibm.com / space apple as pasapi@au1.ibm.com...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: hello-spring-boot.mybluemix.net
last uploaded: Tue Feb 24 10:19:56 +0000 2015

     state     since                    cpu    memory           disk          details
#0   running   2015-02-24 09:21:03 PM   0.0%   128.8M of 256M   99.7M of 1G



No comments: