Sign in

Alexander Ang
A curious software developer who loves to write.
Jira Logo

Introduction

I am a forgetful person, not gonna lie. I wrote everything down, otherwise I will forget it instantly. Also, keeping a note on what you need to do can free up spaces on your head. I always think of our brain like how a computer RAM (Random Access Memory or just memory, to keep things simple) works. If you memorize things, it will take your “RAM” space (short term memory), and will impact the performance of your brain. The longer you’re trying to memorize, the impact will be bigger.

I start writing on to-do list from when I was in…


WebSocket + SpringBoot
source: https://blog.ascendingdc.com/a-quick-tutorial-of-springboot-with-websocket-capabilities/

Spring.io WebSocket guide: https://spring.io/guides/gs/messaging-stomp-websocket/

This post is highly referenced from the WebSocket guide above by spring.io. The difference is, in this guide, I will show you how to do it across multiple applications. When you use microservices architecture, usually you will have an app as a “front” containing a user interface, and an app on the “back” to process business logic and database-related operations. For example, the use case is: you want your “back” app to send a notification to “front” when the order is completed so that the user can get a pop-up notification alert.

What I’m going to…


Prerequisite: More about redis here

In this post, I will show how to use redis caching across microservices.

I have 2 simple projects:

  • learn-redis-api (I will call it front from now on) that acts as an external part of a microsevices, handling incoming requests.
  • learn-redis-core (I will call it core) that acts as an internal part of a microservices, handling database operations.

First, the dependencies. The important ones are highlighted, other than that are the usual basic dependencies.

Dependencies for the API:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>…


source: https://miro.medium.com/max/4000/1*77Vo1RFQ-5DcLKdeHbb2-A.png

What does Redis actually mean?

It means REmote DIctionary Server.

Introduction

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

You can run atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set…


AOP (Aspect Oriented Programming) adalah cara untuk menambahkan behavior baru ke dalam code tanpa mengubah isi dari code yang sudah ada sebelumnya.Jenis AOP yang akan kita implementasi adalah annotation driven, contoh: @Transactional.

Dengan cukup menambahkan anotasi, kita tidak perlu menyentuh dan mengotori business logic yang sudah rapi. Oleh karena itu dalam artikel ini kita akan membuat custom annotation yang fungsinya adalah untuk menghitung processing time saat method dijalankan.

Pertama, tambahkan dependency berikut:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- optional untuk logging -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

Kedua, buat interface untuk anotasi tersebut. Nama interface akan menjadi…


OKD adalah sebuah platform untuk mengembangkan dan menjalankan aplikasi yang containerized. OKD didesain untuk membuat aplikasi dan data center yang terhubung dapat bekembang dari hanya beberapa mesin dan aplikasi menjadi ribuan mesin yang melayani jutaan clients. Dengan pondasinya di Kubernetes, OKD menggunakan teknologi yang sama yang berperan sebagai engine untuk berbagai macam aplikasi.

OpenShift (OKD) Client membantu kita untuk develop, build, deploy, dan run aplikasi pada platform OpenShift atau Kubernetes.

Dengan OKD command line interface (CLI), kita bisa membuat aplikasi dan mengatur project OKD melalui terminal. CLI ini sangat berguna dalam situasi seperti:

  • Mengerjakan source code project
  • Membuat script untuk…


Kubernetes

Definition

Kubernetes, atau disingkat K8s (8 ini sepertinya dari jumlah karakter antara K dan S, seperti internationalization yang disingkat menjadi i18n), adalah sebuah open-source container orchestration engine yang digunakan untuk mengotomatisasi deployment, scaling, dan management dari aplikasi yang sudah dikemas (containerized). Maksud dari container orchestration engine adalah mesin atau engine ini dapat mengelompokkan container untuk dapat membuat aplikasi menjadi unit-unit logis agar lebih mudah diatur dan ditemukan. Kubernetes sudah dibangun dengan pengalaman selama 15 tahun dengan production workload di Google, digabungkan dengan ide dan praktek terbaik dari komunitasnya.

sumber: https://docs.bytemark.co.uk/wp-content/uploads/2019/02/Monolithic-Application.png


Prerequisite: https://medium.com/@alexanderang.24/spring-boot-cache-implementation-for-database-operations-cf846d6d8e3c

Dependency needed on pom.xml (+ from prerequisite):

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

Add these redis configuration to application.properties:

# Redis Config
spring.redis.host=localhost
spring.redis.port=6379
spring.cache.type=redis //set application cache type to redis
spring.cache.redis.time-to-live=60000 //cache expiration time in miliseconds
spring.cache.redis.cache-null-values=false //allow redis to cache null values
spring.cache.redis.use-key-prefix=true //to enable prefix in cache key name
spring.cache.redis.key-prefix=My Cache: //key name prefix

PeopleDto needs to implements Serializable. …


Prerequisite: https://medium.com/@alexanderang.24/spring-boot-cache-implementation-for-database-operations-cf846d6d8e3c

Dependency needed on pom.xml (+ from prerequisite):

<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.2</version>
</dependency>

ehcache.xml (put this on resources folder)

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<cache alias="findAllCache">
<expiry>
<ttl unit="seconds">5</ttl>
</expiry>
<listeners>
<listener>
<class>com.example.demo.config.CacheEventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>…


Cache implementation was done using built-in cache function on spring boot framework.

First, you need to add the following maven dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

To enable caching, add @EnableCaching annotation on any configuration class, for example the main class (in this case the DemoApplication class).

Other way of enabling cache is by using XML configuration:

<beans>
<cache:annotation-driven />

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
name="addresses"/>
</set>
</property>
</bean>
</beans>

Caching with spring boot is much simpler by using annotations:

@Cacheable

  • enable caching on a method, usage: @Cacheable(“cacheName”). …

Alexander Ang

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store