Spring Boot 3.1.0 New Feature πŸš€ | Docker Compose Module | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java techie recently spring boot developers updated the version of spring boot to 3.1.0 and it has so many things quite interesting as part of this release one of the interesting feature I found that is spring boot Docker compose module which was newly introduced to manage containers for our application so let's go to the release documentation to understand how this Docker compose module will help us so if you go to the official page this is the release documentation so if you scroll little bit you can find the section called Docker compose it clearly says that spring boot Docker compose provides integration with Docker compose when your application is starting up the docker compose integration will look for a configuration file in your current working directory so you can Define your file name as any of these four option compass.iml compass.yml Docker iPhone compass dot yaml Docker hyphen compass.iml you can Define these four pattern apart from this pattern if you want to customized it or if you want to follow the non-standard file then you must need to add this key in your properties file okay so once you created the compose file then by default the service declared in the configuration file or compose file will be started up using Docker compose op command and the connection details bin for those services will be added to the application context so that the service can be used without any further configuration for example let's say I have defined MySQL or postgresql as a database service in my compose file so when I will start my application spring boot internally will fire the docker compose op command on the Fly and kick the way DB instance or DB container for me and this DB configuration will automatically load it to my application context so that explicitly I no need to Define any data source configuration or I no need to create any data source bin in my application okay so again when I will stop my application the service will then shut down automatically using the command Docker compose down so this entire lifecycle skill will be managed by Spring boot itself we no need to manually start up the docker compose and shut down the docker compose enter lifecycle will be managed by Spring boot 3.1 version itself this is really out of the box isn't it so without any further delay let's quickly demonstrate this feature in action so let's get started [Music] thank you [Music] so let's go to the IntelliJ IDEA then I'll create a new project click next I need to use the jdk 17 since I am using the spring boot 3.0 then you can specify the group ID let me fill all the field next you can Define all required dependency but before that make sure our application should be create based on 3.1.0 okay now let me add the lumbar dependency I need to add web dependency not this one spring web I want jpa then I will use the post the SQL since MySQL already installed on my machine so again I might need to change the port so I'll go with the post basically okay you can give a try with any supported services so you can find all those operated service here elasticsearch mariadb mongodb MySQL post basically rabbit mq Radisson Zipkin any of these you can give a try now click on next just import this project once it imported you can go to the SRC Main and you can create couple of package so I will create a simple application I'll create one entity and I will just perform create and retrieve operation okay so that I can show you that using Docker compose I can able to up my database instance and I can able to persist few record and I was able to retrieve okay just to understand how this Docker compose internally Works in springboot 3.1.0 okay so what I'll do I'll create a package I'm not going to create any service and controller class I will use this class as a controller class but before that first let me create the entity a limit let's say book then I'll Define couple of field here fine since we added the lombok dependency I can Define at the Red Data at the rate all argument Constructor at the rate no argument Constructor and since this is my entity I need to Define at the rate entity annotation and this is my ID right I can Define ID If You observe the input statement came from Jakarta this is the enhancement in springboot 3.0 that's fine now let's create a repository class okay new Java class I'll name it this should be interface I need to extend it from JP repo that's fine okay we have entity we have reposited with us now I will create or I will take this main class as a controller class so what I'll do I will simply annotate here at the red rest controller then I'll Define another request mapping I'll Define the root URL slash books okay next simply I will just Define the two endpoint before that first I need to inject my repo here private next I'll Define the endpoint to save the book object and to retrieve the book object so what I can do I'll write the endpoint public will return me the book object Save Book take the book object as argument and I need to annotate here at the rate request body then I need to annotate your at the rate post mapping I will simply use return Repository dot save single book object right I can give book similarly I will write another endpoint to retrieve the book object from DV so I will write public then it will return list of book object I need to input this then simply I can call the repository dot find all I need to annotate here either at gate mapping that's it now we need to connect our application to the remote DB using the docker compose okay so that is the reason we have not specified any data source in our code I mean I am not going to point it to my local DB rather I want to connect to the remote DB so for that what you can do as you understand in this documentation you can create a Docker compose file by considering these four pattern if not specify this key with the path of your file so I am going to use this Docker hyphen compass.yml copy this it must need to be part of your working directory so I am creating it so once you created the docker compose file then you need to define the service what you want to use as part of your project in our case we want to use the DB service so we need to specify which DB image we want to part of our project so those things you can Define in your compose dot ml file so first you need to define the version I will specify the version as 3.1 next you need to Define what service you want to use okay so you can Define Services let me Zoom this as part of the services you need to specify I want to use the DV so I will specify DV now we need to Define which DB you want to use so specify his images I want to use postgres okay next you can Define the environment where you can specify the username of your DB password of your DB and name of your DV so I can Define post similarly I can Define the password next you can Define the DV I mean name of your DV I'll name it book so you can change this username and password to the valid one let me specify the user as postgres password as a password so once you define the environment credential username password and DV next you need to specify the ports I mean host port and your must import so what you can do you can Define here so I will Define 5432 which is the default Port of postgres that's it okay so if You observe the compose file is very simple we have defined the version then I have defined the services in Services we are specifying which service we want to use and specify his images then for that specific service what are the variable you want to set to the environment and on which Port you want this service to be up and running so that is what we are just defining here so if You observe here I have option here to you can see this right left Green Arrow Mark right I can click on this it will run the docker compose op command for me but I don't want to do that I want to take the help from this Docker compose module given by Spring boot as part of 3.1.0 so for that what I can do I will just simply run my application but before I run my application If You observe carefully in the resource folder I have not specified any data source related properties but still I can able to connect to my DV even I don't have any config package or I didn't create the data source bin manually that is what it says in this documentation right so if You observe here they have specified here connection details bin for the service will be added to the application context automatically so you no need any additional configuration but I want this jpa need to create the table for me I can avoid the data source specific properties but I can specify the hibernate related properties right I want this table to be created so I can specify this specific property that's fine I don't have any additional configuration let's run the application and we'll see how this Docker compose help us using the springboot 3.1 so go to the main class I'll simply run this we are getting the error what this error says it is unable to find the driver class this is expected error because we are trying to up our Docker compose file but we have not specified the spring boot Docker Compass module given by springboot 3.1 then how can I include that module as part of my project simply go to the palm.xml you need to add that dependency so I will just add it here dependency artifact is simple one spring hyphen boot iPhone compose its Docker compose okay and the group ID will be this let's update this all good there is no error let me rerun the code okay it is trying to pull the images now can you see here but to execute this particular application make sure your Docker should be up and running behind the system okay you must need to start your Docker first before you start your application otherwise this compose will not work now if You observe it is still downloading the images can you see here my Docker desktop is running okay so let's go to the console if you scroll down it is able to pull the images it was able to create the network container and you can see here we are able to successfully connect to the postgresql DV and application is open running on port 8080 all looks good just go to the control of class and let's try to add a book to the DB and we'll retrieve all the books present in our DV just go to the postman so before we try accessing the endpoint let's verify the image in our Docker container and will verify whether container is up and running or not just go to the terminal I'll type docker images let me Zoom this can you see here the post Place image is there now to check whether container is up and running or not Docker PS so the postgres service or the container is up and running all looks good so what you can do go back to the postman and try accessing the books endpoint just hit the endpoint slash books then I need to this will be post request right I want to add the book object change it to the Json pass the request body send the request the record guard inserted now let me add another book I'll name it let's say spring boot I'll add another book let's add another book okay so total we added four object to the DV now to verify whether really those four object is inserted to our DV or not we can fire the gate API call just go to the browser can you see here all the four book object what we inserted were able to retrieve them in this case I am using the spring boot Docker compose module to up my services which is postgres services without manually configuring the bean of data source and within without manually triggering the command Docker compose open Docker compost down now if I'll stop my application the docker compose down command will be automatically triggered by springboot itself okay so this is how you can use this particular springboot Docker compose model given in springboard 3.1.0 just give a try with other services we have understand with the DV MySQL or postgresql similarly you can give a try for rabbit mq or radius or Zipkin anything okay do let me know in a comment section if you guys have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 17,476
Rating: undefined out of 5
Keywords:
Id: -7b_jG9RY78
Channel Id: undefined
Length: 16min 52sec (1012 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.