Make your “Hello World” service production ready

Starting from the prototype presented in a the previous post, you enhanced it with all the business logic your service requires, you tested it and now it is ready to reach production in order to serve requests. If you have reached that point, you probably wonder what else to do before deploying it. There are at least 2 things to do:
1. Make it runs on multiple hosts, ensuring in that way it is resilient to hardware errors and available during deployments, Previous blog post mentioned that it uses a setup based on a single elastic IP. This means that behind an IP is only one single host – which could be OK for testing environment, where a small downtime is acceptable, but for production, we have to change/create a new environment that has multiple hosts behind a load balancer.
EIP vs ELB Architecture

To do this, just open the Beanstalk environment and under Configuration – Scaling section, change the environment type from “Single Instance” to “Load Balancer, auto scaling”. By default, Beanstalk runs a Spring Boot application at port 5000, so you have to update your configuration to reflect that by adding in Configuration – Software Configuration a new environment variable SERVER_PORT, with value 5000. If you are interested, you can find here more details.

Change enviroment type from Elastic IP to Elastic Load Balancer

2. Make it configurable: this means that application will use distinct configuration values in each stage where it is deployed. For example, database URL will have different values on local environment and on production. This requirement is very easy to accomplish by using properties files injected into application by Spring. You can find here the enhanced version of the prototype presented in the previous post. Browsing the code, under the src/main/resources/properties are 2 files: config-dev.properties and config-prod.properties. As you probably guess, their role is to provide the initialization values for different stages. Therefore, we have to indicate on startup which file to load. On local host, this is done via a system property:

java -Dspring.profiles.active=prod -jar target/demo-java-service-1.0-SAMPLE.jar
And to test it:
curl -0 http://localhost:8080/hello?name=John
Hello, John. This is my first REST service! Simple text message from production stage

But, if we’ll upload and deploy this code on our Elastic Beanstalk environment, we’ll get an error similar to this:

017-05-03 12:43:39.008 ERROR 27266 --- [           main] o.s.boot.SpringApplication               : Application startup failed

....
Caused by: java.io.FileNotFoundException: class path resource [properties/config-${spring.profiles.active}.properties] cannot be opened because it does not exist
  at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
  at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:154) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
  at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
  at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:176) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
  at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:157) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
  at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
  ... 19 common frames omitted

12:43:53.369 [main] INFO com.cirrustech.demo.CirrusCloudDemo - Starting application... 
12:43:53.386 [main] INFO com.cirrustech.demo.CirrusCloudDemo - Active Spring profile: null

In order to fix it, we’ll add a new environment variable named JAVA_TOOL_OPTIONS, with value -Dspring.profiles.active=dev, like you can see below. Save and wait for several minutes until the new variable is propagated and test if the service is successfully started.
AWS Beanstalk Environment properties

curl -0 http:///hello?name=John 
Hello, John. This is my first REST service! Simple text message from dev stage

Unfortunately, the JAVA_TOOL_OPTIONS variable is very poor documented in the official documentation, but it offers a very powerful way to customize your environment. We’ll see a future post what other parameters to add in order to optimize your application.
If you have any question or if you want to discuss a specific topic in a future post, please add your comments below!

2 thoughts on “Make your “Hello World” service production ready

  1. 同样,如果我们去跟个人的写手去购买的话,你会发现在价格方面会降低很多,因为团队是团队,个人是个人,团队也是找写手来写的,他们在寻找选手的时候也会把价格往上面压低一些,从而赚取差价。如果我们去找写手的话,这样的差价自然会不存在,是我们跟写手的直接对接,自然不用担心价格方面会很高。 https://essayshark.cn/publications-of-paper-for-professional-title.html

  2. Hey there would you mind letting me know which webhost you’re using?
    I’ve loaded your blog in 3 completely different internet browsers and I
    must say this blog loads a lot faster then most. Can you suggest a good internet hosting provider
    at a honest price? Cheers, I appreciate it!

Comments are closed.