Building a Spring Boot server with Bazel

Spring is a very popular Java Framework that is often used to build servers. It’s widely used by many companies because it provides a rich toolbox that can help achieve a great variety of tasks.

In this article, we’re going to learn how to create a simple Spring server using Bazel as build system.

Building the server

If you have never used Bazel, I recommend you take a look at my introduction to Bazel to get familiar with it.

Spring has a tool called spring intializr that helps create a Spring Boot project from scratch. The problem is that at the time of this writing it only supports Maven and Gradle.

Read More

GKE - Insufficient regional quota to satisfy request: resource "IN_USE_ADDRESSES"

In noticed this error when I was trying to create a new GKE cluster:

ERROR: (gcloud.container.clusters.create) ResponseError: code=403, message=Insufficient regional quota to satisfy request: resource “IN_USE_ADDRESSES”: request requires ‘9.0’ and is short ‘1.0’. project has a quota of ‘8.0’ with ‘8.0’ available. View and manage quotas at https://console.cloud.google.com/iam-admin/quotas?usage=USED&project=test-123456

GCP by default has a limit of 8 static global IP addresses per project, we need to raise that limit to make this error go away.

We can see the current limit with this command:

1
gcloud compute project-info describe | grep STATIC_ADDRESSES -C 2

Sadly, there is currently no way to increase quotas from gcloud cli, so we need to use the web UI.

From the console, search for quotas and select the IAM & Admin result:

GCP Quotas Search

Read More

Introduction to Kubernetes

Kubernetes is an open source system used to automate management of containerized applications. A containerized application, simply means an application that runs inside a container. The management part refers, among other things to:

  • Deployment of changes
  • Rollbacks
  • Load balancing and discoverability
  • Secret and configuration management
  • Container placement
  • Scaling applications

Kubernetes clusters

A Kubernetes cluster is a set of machines that are configured in a way that allows us to tell Kubernetes that we need to run our application without having to worry about exactly where it runs. A Kubernetes cluster consists of two things:

  • Control Plane
  • Nodes

The Control Plane refers to hosts that are responsible for managing the cluster, while the Nodes are the hosts where applications are run.

Kubernetes Nodes need to have a container management system (Docker, for example) as well as an agent used for communicating with the Control Plane. The name of this agent is Kubelet.

Read More

Introduction to Protocol Buffers and gRPC

In the beginning of time RPC (Remote Procedure Calls) was the standard way of communicating between remote services. Some time passed and REST (REpresentational State Transfer) took over as the king.

REST and JSON became popular because they made it easy to understand the communication between clients and servers, since JSON is easy for humans to read.

With the rise of Microservices and systems that are increasingly chatty, JSON became a considerable overhead. Transmiting data in a human readable format, as well as serializing and deserializing this data turned out to be very slow. For this reason, different teams started working in more efficient serialization formats (e.g. protobuf, thrift, etc). As part of this revolution, gRPC was born.

gRPC is a recursive acronym that stands for gRPC Remote Procedure Call. It’s a framework for that is supported by many programming languages and provides many features for advanced use-cases.

Read More

Introduction to JDBI

JDBI is a Java library that can be used to interact with a relational database. JDBI provides a more friendly interface than JDBC, which makes it easier to write code that is correct and safe.

Getting started

The easiest way to create a JDBI instance is by passing a DataSource:

1
2
3
final MysqlDataSource ds = new MysqlDataSource();
datasource.setURL("jdbc:mysql://0.0.0.0:3306/test?user=root&password=my-secret-pw");
final Jdbi jdbi = Jdbi.create(ds);

Once we have a Jdbi object we can use it to get handles. A handle represents an active database connection. Handles need to be closed once they are not needed, or the database might be overwhelmed by open connections. A simple example of using a handle to execute an insert operation:

Read More

Introduction to JDBC

JDBC (Java DataBase Connectivity) is a part of the JDK (Java Development Kit) that provides methods to interact with databases.

The api can be found under java.sql and javax.sql.

Connecting to a database

In order to communicate with a database we need to first stablish a connection. The preferred way to get a database connection is using a DataSource:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package example;

import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;

public class JdbcExample {
  private static DataSource createDataSource() {
    final MysqlDataSource datasource = new MysqlDataSource();
    datasource.setPassword("my-secret-pw");
    datasource.setUser("root");
    datasource.setServerName("0.0.0.0");

    return datasource;
  }

  public static void main(String[] args) throws SQLException {
    final Connection con = createDataSource().getConnection();
  }
}
Read More

Dependency injection with Dagger and Bazel

Dependency Injection

Dependency injection refers to a technique for building objects in an object oriented language. The idea of this technique is to abstract the process of creating an object from clients of these objects.

There are four roles in dependency injection:

  • Client - This is an object that needs to use other objects to achieve a task
  • Interface - The client doesn’t know which objects it receives, it interacts with those objects only through a known interface
  • Service - An object that implements an interface. This object is passed to the client
  • Injector - An object that takes care of constructing objects and passing them to clients
Read More

Introduction to Bazel

Bazel is a build system (analogous to make, ant, etc.) that promises better dependency analysis, faster builds and better reproducibility. One of the goals of Bazel is to make it easy to be extended to support multiple languages; Currently extensions exist for most of the popular programming languages.

Bazel is a fork of Blaze, Google’s internal build system, so it has been battle tested on very large codebases.

Installation

For up-to-date instructions, visit the official Bazel documentation

To install in Ubuntu based systems, we need to first register the software source:

1
2
3
4
sudo apt install curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

And then install it:

1
sudo apt update && sudo apt install bazel
Read More

Introduction to HTML Canvas

Canvas is an HTML element that can be used to draw graphics in the browser. The HTML element renders a rectangular area where we can draw using JavaScript. Adding a canvas to a page is as easy as:

1
<canvas></canvas>

https://jsfiddle.net/ex8u0rgc/

Drawing context

To draw on a canvas, we need to get a reference to the canvas’ drawing context. Here is an example of drawing a rectangle:

1
2
3
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
context.fillRect(0, 0, 50, 50);
Read More

React refs

React was designed with the mentality that parent components interact with their children by sending them props. This works in most scenarios, but not all.

Changing a prop in a child component will make it render in a different way, but it doesn’t allow us to tell the component to do something (trigger an event in a component). If we want to trigger an event in a component, we need to get a reference to it. Unfortunately, it’s not always easy to do this.

ref

The React documentation warns against the use of refs and suggests the use of props instead:

For example, instead of exposing open() and close() methods on a Dialog component, pass an isOpen prop to it.

Read More