Pages

Wednesday, February 13, 2019

Quick Microservice With Gradle Spring Boot Docker

A few years ago if you would have asked me to create a quick POC backend service with some rest APIs I would have just picked up NetBeans and created the REST APIs using jersey/glassfish (reference implementation of JSR 311 RESTful Web Services) from the UI itself.   While the above is an example of creating REST APIs bottom-up from the Database layer, once can plug business logic by adding few more java classes or a service layer. Lately, I have been using Spring Boot a lot in production and it really makes the developer's life easy by not having to write a lot of boilerplate code. Annotations take care of Dependency injection, singleton, REST API mappings etc. It even has the embedded Jetty web server to run the service locally without really having to install a bulk of binary like Glassfish/Tomcat server and deploy them (comes integrated with Netbeans tough - one could right click and run the service). However, Spring Boot + Gradle makes the code ultraportable. All you need to do is check-out from the code repo and run Gradle/Spring Boot command to build or run. Even the testing tools can be easily added to gradle and both Unit and Integration test can be written. There are so may example of hello world with Spring Boot and Gradle on the internet, but I thought of keeping a copy of simple skeleton of service which I can use to bootstrap any app in future. Even better - dockerize the app and create an image out of it which can run anywhere irrespective of the host machine and its setup. Later we can also push it to a public container registry and use it in Minikube or Kubernetes in the cloud (probably in a separate post).

I assume one has installed Java and gradle (only needed for creating the project but the subsequent setup will be taken care by gradlew, the wrapper which can download gradle itself)

Code

To create a skeleton
gradle init  --type java-application
Now add the spring boot dependencies in build.gragle. 

plugins {
...
id 'com.gradle.build-scan' version '1.16'
id 'org.springframework.boot' version '2.0.5.RELEASE'
}


dependencies {
// This dependency is found on compile classpath of this component and consumers.
implementation 'org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE'
}

Create the launcher application which can start the server.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

Now tell gradle (in build.gradle)which one is the main class

bootJar {
mainClassName = 'hello.App'
}

The whole code can be checked out from
git clone https://github.com/neilghosh/simple-gradle-springboot-service

Build 

Now one can build the jar with all the dependencies
./gradlew bootJar
the jar will be created in the ./build directory.

Run


Now we can run the app by either
./gradlew bootRun
or run the jar itself
java -jar build/libs/simple-gradle-springboot-service .jar

Dockerize 

We can create the following simple Dockerfile to import the OpenJDK base image and copy the jar to an appropriiate directory and run command.

FROM openjdk:8-jdk-alpine
RUN mkdir -p /app/
ADD build/libs/simple-gradle-springboot-service.jar /app/app.jar
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

Build the docker image
docker build . -t gradle-spring-boot-app
 Check the local image created
docker images
REPOSITORY                            TAG                 IMAGE ID              CREATED               SIZE
gradle-spring-boot-app                latest                21572d4766d3        About an hour ago   124MB

And run the docker image exposing the port to host
docker run -p 8080:8080 gradle-spring-boot-app:latest
Profit
$ curl http://localhost:8080/
Hello Gradle!
 Next, we will see how to push the local cached imaged to a docker registry and run in cloud or Minikube

Wednesday, February 6, 2019

I am now a Google Developers Expert !

I was recently recognised as a Google Developers Expert in Cloud. This is a program run by Google Developers to recognise developers of Google technologies as well as outstanding professionals in product strategy, UX/UI, marketing, growth hacking and monetization.

While this is a great hour I would definitely thank the local community leaders and Google DevRel folks for providing me with the opportunity over the years to get involved with the communities and nominating me for this program.



Google Cloud Platform is a public cloud where I have worked most professionally and personally in side projects. I was fortunate to discover this early on was just when it was just Google AppEngine with only python and datastore support. This was a big leap over finding a free shared Linux hosting with LAMP stack and cPanel where I also hosted manage my blog in WordPress. This was probably the only way I could host some server-side scripting / dynamic web page for free before AppEngine was there. Even if you are proficient in Java/python, you could not really host anything publically.

Java support in-app engine was a game changer. This kind of defined PaaS in textbooks (like EC2 as IaaS and Salesforce as SaaS). It was a big plus over AWS (bean stock ?).

At work, this was heavily used as the platform to host a game server, taking maximum advantage of stateless nature, auto-scaling, Memcache and datastore.

While a lot of APIs like file I/O was restricted for security, Google soon introduced Google Cloud Storage for central/distributed persistence. Similarly, task queues and backends were a great tool for asynchronous communication and background processing Slowly more and more services were added and it evolved as Google Cloud Platform.

Later GCP was also a go-to platform creating enterprise business SaaS application at my work leveraging BigQuery, AppEngine Flex, Pub/Sub,, Dataflow and many more services which are unique to GCP.

While I had been involved projects involving AWS and Azure I can say GCP is where I spend most of my time.

Communities -

While I involved with the local IEEE chapters like GOLD (Yong engineers) and Computer Society, I got an opportunity to interact and influence a lot of students and professionals in the state. This leads to a large network of people who were involved in other communities. When Google Developers Group was formed in Hyderabad I got several opportunities to attend and talk Devfestsover the years which led to the growth of network developers and change to speak on other communities like Silicon India and Free code camp.

Just a reminder, you can follow me at

Twitter - LinkedIn - GCDC Hyderabad

Tuesday, January 15, 2019

Analysing Google Cloud Platform Billing Data

No matter if you are a large enterprise or small startup, or even individual developers, you probably need to to know what you are paying for when it comes to cloud bill. in GCP you can export your billing data to Google BigQuery and then run queries on it to know where you are spending more which can help you control the cost you are incurring to run the infrastructure and app.

Daily spend
SELECT
  India_date,
   ROUND(SUM(cost)) cost
FROM (
  SELECT
    DATE(DATETIME(usage_end_time,"Asia/Kolkata")) India_date,
    cost
  FROM
    `YOUR_BILLING_EXPORT_TABLE`
  WHERE
    project.id = 'YOUR_PROJECT_ID'
    AND _PARTITIONTIME >= TIMESTAMP("2019-01-16")
    AND _PARTITIONTIME <= TIMESTAMP("2019-01-20") )
GROUP BY
  India_date
Order by India_date
Result
GCP Cost per day, taking timezone into account.


 Cost By Services
    SELECT
      item,
      cost
    FROM (
          SELECT
            service.description item,
            ROUND(SUM(cost)) cost
          FROM
            `YOUR_BILLING_EXPORT_TABLE`
          WHERE
               project.id = 'YOUR_PROJECT_ID'
            AND _PARTITIONTIME >= TIMESTAMP("2018-11-01")
            AND _PARTITIONTIME <= TIMESTAMP("2018-11-30")
          GROUP BY
            service.description )
    ORDER BY
      cost desc;
Result

Cost by GCP Service


Similarly, other queries can be executed for drilling further down within a service. i.e. the App Engine cost includes Cloud Datastore, networking, disks etc. One can find the breakdown with the following query.

 Cost by SKU for a specific service 
 SELECT
      item,
      cost
    FROM (
          SELECT
            sku.description item,
            ROUND(SUM(cost)) cost
          FROM
            `YOUR_BILING_EXPORT_TABLE`
          WHERE
             project.id = 'YOUR_PROJECT_ID'
            AND _PARTITIONTIME >= TIMESTAMP("2018-11-01")
            AND _PARTITIONTIME <= TIMESTAMP("2018-11-30")
            AND  service.description = 'App Engine'
          GROUP BY
            sku.description  )
    ORDER BY
      cost desc;
Result

Saturday, January 5, 2019

Poco Phone F1 - Moving from Motorola to Mi

I have been using Motorola Android phones for a while now. Moto G, Moto X Play, Moto G 4 Plus, Moto G5 S Plus, Moto E and So on. They are not only considered to be the robust phone but also they run stock Android OS. It was part of Google and Nexus series for a while. However recently I gave up on them because of their camera. Even today they don't have a good phone with camera comparable with other phones in the same price range.

I bought the Poco Phone F1 recently and it has a really good camera comparable to the flagship phones like One Plus, Samsung Galaxy S* etc. Initially I was skeptical about the Xiomi phones because they were new, cheap and it is a Chinese company but I changed my opinion after hearing from friends who have been using the same Xiomi Mi phones since more than 2 years and have excellent photos from its camera for half of the price you pay for flagship phones. Poco was there best phone so far from the specification wise (probably except Mi Mix).

Here are the good things I found after switching from Moto G series phones.
  1. Poco F1 has pretty much all sensors that Moto G silently compromises. Moto phones did not have the gyro and compass sensors which were a problem while watching VR and using stargazing apps.
  2. Excellent camera, no grain. good night light photos.
  3. Face unlock (even in dark) yay!
  4. Snapdragon 845 processor and 6 GB of ram, I am not expecting it to slow down when I install a lot of apps.   
  5. The battery is long lasting.
What am I missing?
  1. Because of the notch, I miss out on a lot of notifications after they arrive in the top bar. Even Bluetooth connected or not, you have to slide down.
  2. No front flash, the bright screen does not make a lot of light.
  3. No shake to turn on flashlight or launch camera. (I had to set the flashlight on double press power button gesture)
  4. No NFC! 
  5.  It's USB C-Type. All my chargers and cables at home are micro-USB. I bought some cheap adapters.
  6. Google Now in the left to the home screen. (I found a workaround - Install Nova launcher and a companion pack)
Maybe the bad points are just about getting used to. 

Yi 4K Action Camera

This is probably one of the first reviews I have ever written. You would find a lot of content about gadgets in youtube (with so many tech channels these days) but the only problem I have with that is I can not skim-through the video content just to see if there anything relevant that I need to know. I need to see/hear the full specifications and comparison what the video has to say. With this post (and possibly a series of posts with other gadgets that I own ) I am trying to have some notes that are usually not in the Amazon listing or manual or any overall review videos.





Yi 4K Action Camera one of the affordable action camera. Following are the observation.
  • The build quality is really good and quite compact.
  • The time lapse works great, here is a sample. This is the most detailed version i.e. one frame per 0.5 seconds. For slow moving scenes like the cloud or sea, one can use even slower settings like one frame per 10 seconds and beyond.
  • I also tested for the other extreme - slow motion. It highest setting that it has is 8x slower and I compared the slo-mo of a fast moving ceiling fan shot by this action camera and POCO phone F1, video from this one looked better.



  • It has a really wide angle field of view which sets action cameras apart from the mobile cameras.
I am happy with most of the things with the camera, except
  • The wifi works for AP mode where you can connect your phone to the camera via wifi bit the station mode does not work. Neither I could get any documentation what is it supposed to do.
  • You may have to spend extra on accessories like waterproof cover, tripod, remote etc. (The voice action vacation is cool tough if you don't want to use a remote.)
Update -

I thought it's worth mentioning the following couple of tools which I found out while creating this post.

1 - Online video editor -  I could crop and resize videos using ezgif.com/crop.

2. Using FFmpeg I could stitch the slo-mo videos in command line itself to show them side by side. Thanks to StackExchange.

ffmpeg \
  -i input1.mp4 \
  -i input2.mp4 \
  -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' \
  -map [vid] \
  -c:v libx264 \
  -crf 23 \
  -preset veryfast \
  output.mp4