>> Azure Cosmos DB Liftoff.
This session is about
Planetary Scale Postgres for Spring Boot Apps,
whereas I like to call it a fast and
frictionless migration path for developers.
My name is Mark Heckler and I'm
a Principal Cloud Advocate of Java
and JVM languages with Microsoft.
If you have any questions, comments, or feedback,
I can be reached via email
a markheckler@Microsoft.com or better yet, on Twitter @mkhec.
I check email occasionally, I live on Twitter,
so please reach out to me,
my DMs are open as well.
A bit about me.
I am an architect and developer.
I'm also obviously an advocate.
I've also co-authored and
authored some books and you can check those out.
Follow me on Twitter and you'll find out more.
I am a Java Champion,
Java 1 rockstar or Kotlin developer expert,
and a licensed instrument-rated pilot.
But that's enough about me.
Everybody needs a hobby,
and everybody needs to stay busy versus bored.
What's on today's agenda,
we're going to be developing a Spring Boot application and
connecting it to a local PostgreSQL database.
That's meant to in a small-scale mimic what is probably going on
in a lot of our organizations in which we
have Spring Boot applications,
talking with and using
external PostgreSQL databases on
a local level or private Cloud or what have you.
We're then going to tackle one of
the most difficult problems that developers face many times,
migration to a much larger,
more scalable, more resilient data store in the back end.
We'll be using Azure Database for PostgreSQL
or as I like to call it Planetary Postgres.
We're going to do all this by the way,
in under 15 minutes. What could go wrong?
With that, let's code.
We're going to start our project
using the spring initializer and we're going to
use the standard build system of
maven and we're going to use Java for our language,
and the current version of Spring Boot which is 2.7.4.
We're going to change the group because, why not?
I can, and we're going to just call
this our planetary-Postgres application.
I'm going to use Java packaging because after all,
it's current-day, why not?
We're going to use Java 17 because it's
the current LTS version of Java, but you have options.
Once again, I'm going to bring in
a few basic dependencies here so
that we can create a web-based application.
I'm going to be using JPA,
the Java Persistence API to access our database backend,
and we'll use the Postgres driver to access Postgres,
obviously behind the scenes.
Then I'm going to bring in Lombok because I'm a lazy developer,
but lazy in the good ways,
lazy when it counts not in the bad ways.
We're going to further reduce
our boilerplate code so that we
can focus on the important things today
and not just the extra stuff that we might have
to deal with it with a lot of boilerplate, we don't want that.
I'm going to generate our project here.
We will save that.
I'll just save that to my desktop and we'll open it from there.
I find the zip file that we've
downloaded with the project we'll open that.
For today's coding, I'm going to be using intelligent.
Now notice that, yes,
I work at Microsoft and I'm using
intelligent on a mac to develop Java code.
That's because Microsoft wants to be the ultimate destination,
the best destination for your applications,
your data, anywhere in the world, in the Cloud.
Whatever your toolchain choice,
whatever your platform choice,
whatever your language choice, it's all there.
We're happy to help you have
the best possible outcomes you can have.
With that, I'm going to launch in and just take
a quick look at my build file, my palm.xml.
We see that we're using Java 17,
we're using the current version of boot 2.7.4.
We see our dependencies here that we included in our project.
I'm also going to open up my application.properties,
which allows me to provide
some suitable defaults for
a few values that we'll need in our application.
I'm going to define here our data source URL.
I'm once again going to be using JDBC and PostgreSQL.
I'm going to first access a local database, as I mentioned,
and I'm going to be pointing that
to a database I'll have up and running
currently on localhost called Postgres, clever.
I'm going to specify the username.
In this case, the username is just Postgres.
Then I'm going to also specify the password.
Please don't tell anyone,
but I guess we all know that everything ends on caffeine anyway.
Obvious, it's not a big secret.
Then I'm going to define one more thing which is
the DDL-auto perimeter for GPA hibernate.
I'm going to set that to create a drop.
Now, this is a wonderful parameter to set for development.
Great for devs or dev,
not for prod, because obviously,
we don't want to create our table structures,
and when our application starts and we
don't want to delete them when
our application ends and production.
But for development, this helps
focus on the developer first experience and get us up
and running quickly and efficiently so that we can
concentrate on the business at hand,
which is to develop our application.
Again, I'm going to keep this pretty simple
based on our short time we have here together today.
But I'm going to define a data class here
for us to store the important things and retrieve them.
I'm going to set this as an entity,
but to define this as an entity and I'm going to
use Lombok to make this a data class.
I'm going to also have Lombok
provide a Noargs constructor for me because
GPA requires a Noargs constructor in order to function properly.
I'm going to ask that Lombok
provides us a required Noargs constructor.
Now Lombok is a byte code generator so that
allows us to keep everything nice
and tight and clean for the demo.
Some folks use it in production, some don't.
But we're going to use it here
today because that way I can focus on
the key bits and not get
bogged down once again in the boilerplate.
I couldn't think of anything else to
save so I'm just going to save some marks.
Every demo needs some target, that's us.
I'm going to define our ID field here and ask
that Postgres generates the value for that.
I'm going to make
this a long value and just call this cleverly NFID,
I'm going to specify the next couple of fields should be
NonNull and NonNull combined with
this required Args constructor annotation
for Lombok means Lombok will create
a constructor with a parameter for each of
the member variables indicated to have required values.
In this case, we have the first name and the last name.
Lombok will create a constructor with just these two parameters.
I think that does everything that we needed to do for now.
I'm also going to define a mark repository interface.
>> We're going to extend
the defined CrudRepository interface that's in Spring Data.
We can store objects of Type Mark with IDs I've typed long.
This is enough for Spring Boots AutoConfig to
see this interphase extending a Spring Data interface,
and creating proxy bean to allow us to
interact with our table, with our database.
In this case, the Postgres database and store and
retrieve marks using a set of CRUD operations,
create, read, update, and delete.
That's sufficient to get us up and running.
I'm going to now create a bean so we can load
some data into this and
retrieve it and see that it's actually working.
It's just going to use a CommandLineRunner bean,
and we'll say load Mark Data here,
and we will load some marks.
I'm going to inject our MarkRepository here so that we can use
that once again to access our database behind the scenes.
CommandLineRunner is a functional interface
with a single abstract method,
and as such, since Java 8,
we've been able to just specify that as a lambda,
which we're doing now,
I'm going to use a repo that we're injecting.
As a matter of good habit,
deleting all values in case I've left anything in there before.
Once again, this actually shouldn't be required.
This delete all because
if we're doing a create drop, we shouldn't need it.
But again this is a good practice and habit to get
into so I'm going to continue with the habit.
I'm then going to specify some marks to save to our DataStore.
I'll just do this again rather quickly.
I'm going to save maybe two or three of these.
Let's go with three why not. We'll save these.
I'm actually going to bring those in as a list of and of course,
let's see, I'll go ahead and redo this, some new marks.
There we go. Let's start off with Mark Brown.
Mark Brown leads the charge with
the Azure database for Postgres and
Cosmos DB engine development back here at Microsoft.
Mark Heckler kind of celebrity in my eyes.
He's also the host of Cosmos DB learned TV,
probably seen him, you probably heard of him.
Then I'll throw me in because now I'm here, why not.
Then also going to bring in another Mark.
Probably nobody's heard of this one.
Mark Hamill, he's just an up-and-coming actor nowadays I hear.
Then I'm going to just do a repo.
findAll forEach and we'll just print this
out to make sure we have working what we think we have working.
It's always good to test, trust, and verify.
I'm going to restart this or start
this application and we'll see what we get.
What should happen is we should open up
a connection to Postgres and we should save
these marks and then we should retrieve them and print them out.
Of course, it works because things always go perfectly
in live demos. Well, that aside.
The next thing I'm going to do, because up to this point,
this is just meant to simulate
your normal Spring Boot development cycles.
Where you're using a local database,
you're developing your application,
you're iterating quickly and you're getting things
ready to go to production,
but they're not there yet.
The next thing I'm going to do is tackle one of
the hardest things about development work, which is migrations.
Now, I do want to just so we can
prove what's going on here do a couple of changes.
Now how do you get this to use a planetary scale Postgres?
I'm going to copy this,
and instead of the data source we have here.
I need to point to a planetary scale Postgres installation.
The easy way to do that,
since I already have things set up just in the interest of time,
I've created an Azure database
for PostgreSQL single server instance.
Then I can go here to see my connection strings.
Here we see the connection string for JDBC.
Let me zoom in on that just a bit and we see that we
can grab that connection string and plug that in and use that.
I'm going to copy that to
clipboard and go back to my ID and just paste that in.
Now you can see that there are some variables
here that we need to provide
values for the database and of course,
the password, we're requiring,
SSL, so on and so forth.
But we have all the basic pieces in place that we need.
Now, I do want to do one more thing first,
just in the interest of proving
that we're doing what we say we're doing.
I'm just going to get me out of here.
I'm certainly not essential to this demo data.
I'm here after all. I'm going to add in another famous Mark.
Well, that we have Mark Brown,
Mark Gatiss, Mark Hamill now.
Three famous Marks.
Mark Gatiss was a showrunner and contributor to BBC's Dr.
Who and Sherlock don't know if you've
heard of him, but probably so.
By the way, I do that just so we
can verify that things have switched,
that we are going to be using Azure database for Postgres.
I have an environment variable set
behind the scenes so you can't see my password.
But what I'm going to do now,
is just go in and build application here at the command line.
Then I'll run it from the command line and we'll see that
everything hopefully will be connected to a much more scalable,
resilient, robust version of Postgres running in Azure.
We got a hint of that right up there in the build.
I'm going to go ahead and do a java- jar target.
We'll just run it from here and see what we get.
Then, of course, everything works because again,
things never fail in live demos,
we see that we have Mark Brown, Mark Gatiss,
and Mark Hamill and I don't think you can
get any better than that.
With that, owing to our short time we have here together,
I'm going to go back and finish up and
share some resources that you might find handy.
For starters, the repos,
if you want to see what scripts are unnecessary,
which they're very small, very lean.
But they are scripts that I use to create
an instance of the Azure database for Postgres in Azure.
Of course, I use the Azure command line interface,
which I'll get to in a moment,
but the scripts repos there as well as
the demo repo for the Spring Boot Application.
You can examine the dependencies and what I
used and maybe wonder why I didn't use something by all means,
that's where the bottom part comes in.
If you have any questions, comments, or feedback,
please reach me at my e-mail or better yet on Twitter.
The middle section is the key
I feel like because if you want to know more
about Cosmos DB aka.ms/cosmosdb.
If you want to know more about the Azure command-line interface
that I used for my scripting.
It's there, you certainly have options as you saw,
you can go through the portal and go through and use
the portal to create your Azure instances.
You also can use other tools as well.
I just, I'm fond of scripting and the ASI CLI.
With that, we're done.
Good luck and go forth and Postgres [MUSIC]