jueves, junio 07, 2018

Spring Boot + Cockroach DB in Kubernetes/OpenShift


In my previous post, I showed why CockroachDB might help you if you need a cloud native SQL database for your application. I explained how to install it in Kubernetes/OpenShift and how to validate that the data is replicated correctly.

In this post, I am going to show you how to use Cockroach DB in a Spring Boot application. Notice that Cockroach DB is compatible with PostgresSQL driver, so in terms of configuration is almost the same.

In this post, I assume that you have already a Cockroach DB cluster running in Kubernetes cluster as explained in my previous post.

For this example, I am using Fabric8 Maven Plugin to smoothly deploy a Spring Boot application to Kubernetes without having to worry so much about creating resources, creating Dockerfile and so on. Everything is automatically created and managed.

For this reason, pom.xml looks like:


Notice that apart from defining Fabric8 Maven Plugin I am also defining to use Spring Data JPA to make the integration between Spring Boot and JPA easier from the point of view of the developer.

Then you need to create a JPA entity and Spring Data Crud repository to interact with JPA.

Also, we need to create a controller who is responsible to get incoming requests, use the repository to make queries to DB and return results back to the caller.

Finally, you need to configure JPA to use the desired driver and dialect. In case of Spring Boot this is done in application.properties file.


The most important part here is that we need to use the PostgeSQL94 dialect. Notice that in url, we are using the postgresql jdbc url form. That's fine, since Cockroach uses the Postgres driver.

Now we need to create the database (customers) and the user (myuser) as configured in application.properties. To make it so, you just need to run cockroach shell and run some SQL commands:


Finally, you can deploy the application by running mvn clean fabric8:deploy. After that, the first time might take longer since needs to pull Docker images, you can start sending queries to the service.

As you can see it is really easy to start using a cloud-native DB like Cockroach DB in Spring Boot. If you want you can do exactly the same as in my previous post and start running queries to each of the nodes to validate that data is available correctly.

Code: https://github.com/lordofthejars/springboot-cockroach

We keep learning,
Alex.
Dôme épais, le jasmin, à la rose s'assemble, rive en fleurs, frais matin, nous appellent ensemble. (Flower Duet - Lakmé - Leo Delibes)
Music: https://www.youtube.com/watch?v=Vf42IP__ipw
Follow me at https://twitter.com/alexsotob



11 comentarios:

sandy dijo...


ان النقل الدولى هام ويحتاج الى مهارة ودقة ليتم على اكمل وجه اعالى الخليج تقدم افضل خدمات النقل الدولى لانها افضل
شركة نقل عفش من الرياض الى الامارات شركة نقل عفش من الرياض الى الامارات
وتوفر ايضا النقل الدولى الى الاردن
شركة نقل عفش من الرياض الى الاردن شركة شحن عفش من الرياض الى الاردن

skan81 dijo...

clinique de chirurgie plastique
reduction mammaire
lipoaspiration
chirurgien esthetique tunisie
chirurgie plastique
chirurgie esthetique tunisie
chirurgie esthetique en tunisie

skan81 dijo...

voiture occasion
discotheque a Paris
discotheque a Paris
voiture vintage

bet paris
entreprise genie energetique
renovation appartement paris
prix ouverture mur porteur
ouverture mur porteur prix

skan81 dijo...

entreprise generale de batiment
Agence Marketing Digital
Entreprise de revetement
VRD
ouvrage d'art

skan81 dijo...

diagnostic de structure
bet structure idf
Maçonnerie paris
bureau d’etudes techniques pluridisciplinaire
Ouvrir un mur porteur

skan81 dijo...

chirurgien esthetique tunisie
photo chirurgie esthetique tunisie
aesthetic tunisia
video chirurgie esthetique tunisie
blog tunisie
referenceur Tunisie

skan81 dijo...


جراحة التجميل تونس
chirurgie esthetique
greffe de cheveux en tunisie
changer les protheses mammaire
reduction des seins tunisie
operation esthetique tunisie

skan81 dijo...

plastic surgery tunisia
chirurgie des dents tunisie
nymphoplastie
dr malek
operation esthetique tunisie
chirurgie esthetique prix

skan81 dijo...

Cirugía estética Túnez
blanchiment des dents en tunisie
couronne dentaire en tunisie
facette dentaire en tunisie
docteur khalfaoui

yanmaneee dijo...

lebron shoes
nike epic react flyknit
nike air max 97
michael kors outlet
retro jordans
moncler outlet
curry 6
air jordan
lebron james shoes
michael kors handbags

sindhuja cynixit dijo...

Thanks for your interesting ideas.the information's in this blog is very much useful for me to improve my knowledge.
Kubernetes Online Training