This posting is based on a quick customer demo that I did the other week. It demonstrates running Kotlin code in different public clouds. To be more precise: We use Kotlin together with Spring Boot to create a somehow minimalistic REST-like application that runs on multiple load balanced instances on AWS Beanstalk or Oracle ACCS.
Kotlin is one of the upcoming, trendy JVM-based languages. Its syntax is close to Java, but just sometimes more convenient. The language is backed by two industry giants: Pivotal announced support for Spring and Google officially supports Android development with Kotlin.
The Kotlin language follows Java coding conventions, but makes the semicolon mostly optional (great!) and adds null-pointer safety (you never had a problem with this in Java, did you?) and string interpolation on top of that. Although Kotlin packages are the same as in Java in reverse order, in Kotlin, package names don't have to match the folder structure (great!). Interestingly enough, most Kotlin control structures are expressions.
We will use Kotlin together with Spring Boot in this post.
For the purpose of this demo, we will create the smallest possible application that is still helpful to learn about the synergy of Kotlin, Spring Boot, and the cloud. To get started, we create a maven project for Spring Boot with the spring.io initializer.
The project will contain an empty Spring Boot application and the maven POM file, amongst others.
Add a Controller
To get some meaningful output, we will add another Kotlin class: A controller with three functions.
- The hello(World) function that simply echoes a name.
- A func that shows how to access java.util.Date from Java and returns the time.
- An info func that show a bit of load balancing magic when deployed on ACCS.
Build the Project
We can build the project with a mvn package from the command line (or from the IDE, as shown in the webcast down below).
IDE Support for Kotlin
Kotlin in the Clouds
This little demo is more real life than you might think. I will show you how to run Kotlin in two different clouds. These days, I usually see multiple clouds with my customers. Simply choose the one you like most.
ACCS is an Oracle Public Cloud PaaS service that provides several language runtimes. ACCS is based on Docker containers, but don't worry, you won't see any of them. With ACCS, you can easily deploy Java, Java EE, as well as Python, Ruby, Node.js, and even Go code. Also, it supports spinning up multiple instances with load balancing.
Deploying a .jar file on ACCS is as straightforward as it gets:
- Zip it.
- Upload it.
- Run it.
However, there is one more thing: To successfully run the .jar file, we need to add a manifest.json file that contains the exact command of how the JAR file will be started in the Java container. It should be obvious to all watching the webcast. Check my other posting to learn about ACCS and Java EE.
The AWS Beanstalk deployment isn't more complicated than the ACCS one. Actually, even the manifest.json file can be omitted. Beanstalk implicitly understands how to run a .jar file (which, of course, is easy in most cases:
java -jar demo.jar). So all you need to do is upload to Beanstalk. However, with Beanstalk, we have to make sure to include a SERVER_PORT variable and set its value to 5000. This is required since Beanstalk is internally listening to port 5000, but Spring Boot uses 8080.
I created a webcast for you with all the details.
Some additional resources that you might find useful: