In this blog post, I will show you how to configure your Micronaut application to use Google Cloud Sql for MySQL as a datastore.
Context
I have been a huge fan of Spring and Spring boot for a long time until I discovered Micronaut. Micronaut is one of the first Java frameworks that address the challenges of building and deploying microservices to the cloud. Not only does it support many cloud providers natively, but it also enjoys a blazing fast startup time.
At the opposite of Spring who relies on reflection to wire Java beans and construct your application at runtime, Micronaut leverages compile-time AOP processing that enables it to run faster and with lower memory footprint. Additionally, Micronaut supports Reactive and Non-blocking paradigms.
At Veamly, we heavily rely on spring-boot as the foundation for all our microservices. However, we were impressed by the capabilities of Micronaut, and we started experimenting with it since its first release. In this example, I will also cover how to configure Liquibase and Hikari along side with Google Cloud SQL for MySQL.
Hikari
Hikari is production-ready connection pool that outperforms other popular connection pools like: Dbcp2, Tomcat, and Vibur. To add Hikari to your Micronaut application, you need to declare the following dependency:
compile "io.micronaut.configuration:micronaut-jdbc-hikari"
To use hibernate, you need add this (You can also use hibernate-gorm if you prefer):
compile "io.micronaut.configuration:micronaut-hibernate-jpa"
Liquibase
Liquibase is a database agnostic Java library that add versioning to your database. It allows to track changes to your database schema over time, making it easier to propagate database changes across environments or rollback them. While this is an optional step, I encourage you to start experimenting with Liquibase if you have not already done so.
compile "io.micronaut.configuration:micronaut-liquibase:1.0.0.RC1"
Cloud SQL for MySQL
Connecting to managed MySQL database on Google Cloud requires loading an additional library that alleviate the burden of dealing with IP whitelisting and SSL certificates. For this, you need to add the following Jar.
compile "com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.11"
Note that this library provides a custom SocketFactory that you need to specify in the connection url.
First, you need to add the following snippet to application.yml. Then, you need change [DATABASE_NAME] and [Db_SERVER_INSTANCE] with the name of your database and server instance name respectively. Finally, make sure to set the username and password using the following environment variables: DB_USER, DB_PASSWORD
---
datasources:
default:
jdbcUrl: jdbc:mysql://google/[DATABASE_NAME]?cloudSqlInstance=[Db_SERVER_INSTANCE]&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false
username: "${DB_USER}"
password: "${DB_PASSWORD}"
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
maximumPoolSize: 15
minimumIdle: 5
jpa:
default:
packages-to-scan:
- "com.veamly.service"
properties:
hibernate:
hbm2ddl:
auto: none
show_sql: false
If your database has a private ip and SSL connections activated, you need to change the jdbcUrl to the following:
jdbc:mysql://[IP_ADDRESS]/[DATABASE_NAME]?cloudSqlInstance=[Db_SERVER_INSTANCE]&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=true
Although Micronaut is still in its early days, our experiment showed very satisfying results. This is not surprising, since the team behind Grails is the one who built Micronaut. Stay tuned if you interested in learning more about building production-grade applications with Micronaut.