Build a Reactive app in Apache Cassandra™ with Spring Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome everybody for yet another cassandra developer workshop today i have i am lucky i have a very special guest maurice eschold um co-founder of typefox so moritz thank you for being with there with us can you please introduce yourself yeah thank you very much um cedric for for having me yeah i'm co-founder of typefox and we started several projects in recent years one of them is seer as an open source alternative to vs um to vs code which is governed by an open source foundation the eclipse foundation and also a few years ago we started the project gitpod that will play a very big role in today's um in today's workshop and gitpod is a is a great way of efficiently doing coding um in your web browser with um with all your with your workspace on the on the server and on the server side yeah and today you will use gitpod and you will see how good it is for any kind of development we are using gitbot every weeks but if it's the first time you attend you will see how cool it is so let's get started it's not only me and moritz sunscreen as usual there is a full crew behind the scene answering your question in the chat i already seen david and alex answering a couple of questions you can also ask longer questions on discord and let's get swirling oh by the way if you like the content we are doing don't hesitate to subscribe yeah i'm just saying moving on so today as usual we are live on youtube and twitch i think we try linkedin today so maybe we are linked in maybe the folks on the chat will tell me if you need to ask questions we are on the youtube chat and also on the disco and today we will do some little games and survey live as usual using mentee nothing to install simply open a new tab on your brother's melty.com and i will give you the link speaking of which okay so today we will show you how to build a reactive application using spring framework and to do so we will use as the database data stacks astra cloud database in the cloud and as the ide development environment git board there will be a back end and a front end moving on it's simply just getting started i know you want to get your hands dirty all right um and first you know don't wait let's explain you what is astra what is the database we will use today so astra is a manage service it's cassandra as a service in the cloud okay it's provided by datastax but there there is a free tier and it's a free forever tier so no credit card nothing you simply register even with your github account start the database it will be there forever five gigabytes for for free for you and on top of this database we do have tools and way to scale and api risk graphql api on top of the database okay to ease as much as possible the use of cassandra and this is the reason why we will use that today because it's free easy to use and you don't have to install anything so astra is software as a service yes so you know what let's get rolling so everybody out there ends on part what see today's end zone no slide there will be slide theory but we really want you to to get the most of this workshop and to start the app you want code today so if you're beginner totally fine i won't ask you to create a unit test to to debug now everything is called i will show you the code browse how it works but you should be able to start it and first well you need a database so go to dts6 dot io slash workshop or use the qr code you have on screen and it will route you to here how to start the database okay so and by the way so today we are using a github repo data stacks academy slash workshop sprint reactive the link of the github repo is below me below the video uh and it's fork from the the spring pet clinic i will explain that later on so in this repo not only you have the all the step to run the app so start the database okay how to fill every single field on astra okay click other database select the free tier okay when the free tier has been selected pick i mean the cloud provider has been selected for you because i know we are running this free tier on google cloud but hey if you can see that astra can run on any cloud and astra is now multi-region and astra can do vpc preparing with existing cloud so if you already have some application in the cloud is one of this cloud provider you can make astra part of your ecosystem with the vpc printing but by default the database are set up on our own vpc select a region pick the region that is the closest to you so i i am in paris so i pick europe west i leave the screenshot as you can tell and when you're done click configure then it will ask you to provide database name key space name username i'll give you here some sample values but you know you can pick the one you like simply please remember your password because even if you can reset your password still it's always a bit annoying to have to reset your password you will have to provide the password in the end please okay pick the url here so the url is just below the video but yes let me let me copy the url for you in the chat by the way in the chat i am data stacks developer guy one of the people behind and that is like developer so i i put the github repo and david put the link to create the astra database okay we do have time two hours is a long time let's take the time you need so when you do have the database ready to go i mean with the active state please do a sum up so for the key space name you pick what you like as a proposal we put pet clinic pet clinic either for database name or key space name but you know what the application will be clever enough to use the key space you provided so database name i put pet clinic db and key space name i put spring under the pet clinic the only constraints i do have for those is really for the password now i think you need to use at least one um number one i mean one digit one alphanumeric character character one copy uppercase letter and eight the password would be uh at least eight characters yes big bad clinic yeah as alex stated in the chat it's not very important but spring pet clinic yeah that's what we will do today okay and when you are okay please give me some thumb up i really want you to have the database ready because then i will go to a few slides but you know if you if you're okay that's better okay so and when you filled all the steps now you can click on create database can i make my screen bigger maybe there is no reason to keep this blank space on the left hand right and so it will take a few minutes to spin up so we are just providing a new container running cassandra for you on the platform so it may take a minute to start okay babu legendren tell me is okay cool so it's working so matt okay and yeah simply do uh part number one it's ends on one and it's okay and you should have some astra database see i'm signing with github everything is the git star today and see i do have my database pet clinic in active state cool so i will wait let's say three more minutes for you to be okay and let's move on oh started very fast cool cool cool and when the status is active see that you can now click on the db and see that it's free you do have five gigabyte of capacity you add a cluster id and you know database username pet clinic and your password so for mangda when you need to choose your plan you simply click free plan over here on the first part okay just say i don't want to pay anything and it will ask you to pick one of the region so pick the one closest to you and just click configure and just fill fill the the parameter as stated or as you wish so name of the database i put pet clinic db so let me copy that but you can put what you like and so on when we save for key space spring pet clinic and for user name pet clinic you can you will i yeah you may see my password during the end zone but it's not a big deal because there is a client id client secret that you won't have time to catch or i will change it after the the workshop because it's recorded right you can just pause and get my ids so maurice i i know you did that before how do you find the database creation process was it complicated or simple straightforward i think it was rather straightforward i mean didn't take us so much time and when i look at the chat it seems like people can follow yeah that's that's cool and that's that's yeah that's really the mean of this kind of live stream is really to make people do stuff with you easy cool and so okay i will wait just a bit more for everybody to have their database um so you can see now that the database you can access to elf which will you we will spawn a graph on our container and showing all the requests you are doing against your cluster you do have access to sql console which is uh which is the shell to interact with cassandra very famous in the cassandra world we will use that later you do have the studio which is a kind of jupiter notebook but for cassandra okay so let me enter my password here just the shell okay and describe the spaces you don't have to do that i just try to fill the blank during the time it's asking for credit card details so if it does that you did not pick the free tier so we need to really say free free free and if you already have a free database running you can reuse it for today but you can only have one free database in your aspar account okay what else can i show you in the meantime uh i can show you connect okay you do have a running cassandra now and you can either use cassandra drivers this is what we will do today i really want you to show how to use the cassandra drivers but on top of it you do have the rest api and graphql api if you know your front-end developer really like to use graphql then it's provided so it's not only cassandra as a service there are some tools around that and so not today but on the sample app gallery you can see that we do have a lot so spring data jump stack for your front end next okay gatsby spark and the spring pet clinic will be soon after this workshop we'll be there soon okay are we good should we move forward let's stay let's wait until 25 and we will move on okay documentation building database yeah i don't have a single this is my dashboard it's an activity b i use the key space playing pet clinic i am on gcp for free europe and capacity you need one which is the fritier okay one more minute and i will give you a little theory about reactive stuff okay i think we do have people ready yeah cool so people follow that's cool i do have 25 let's move on so today is all about reactive and so to introduce you to the reactive stack so just to remember the database we just created is astra we do have a back end using spring framework it's a spring rasty it's a rest api using the spring framework technical stack this is the part using the reactive and we do have a ui okay a client it's spring pet clinic angular which is one of the ui implemented on top of you know the for the default reference application in the spring community which is the pet clinic okay so first synchronous and this is for i mean for java developer this is what we use all the time okay when you want to use synchronous communication with cassandra you will connect okay initiate a session and you will start executing query against cassandra and you will get your response okay so you will send the parameter to the api the driver will create a query what we name a statement i could don't go into the details today you will bind the parameter that's simply mapping parameter to um to the query you execute the query against cassandra get back the the the result in an object called a result set and finally you get the result so what the issue here the issue here you can see on the slide is you need to wait during all that time okay so if you are running not performing queries and in consonant you can do that i for instance select star from a table without providing a where clause it can take a lot of time if you uh if you query for a lot of data or a big cluster okay so swot analysis so strength weakness opportunity threat if for for something we are doing every day synchronous is very simple but pretty soon you can eat the issue which is which the weakness which is a blocking communication so um cassandra is well known for high throughput high volume type of workload so meaning i through put if you need to wait for everything get insert to be completed yeah your high throughput is a bit compromised right so let's see if you can do better opportunity easy to test and maintain you need tests you know yeah that's very the vania type of code we are doing day to day okay easy to test easy to maintain issue scalability okay on a client you have limited amount of thread and so if most of your queries are blocked waiting for a result maybe at some points you won't be able to invoke cassandra anymore not because cassandra cannot scale because cassandra scale out a lot but be because of your client with limited number of threads so how to cope with this limitation so let's go asynchronous okay that's a promise so now with asynchronous you will do the small step in the beginning put the parameter prepare your queries bind the parameters to your queries and boom execute the query but instead of waiting for the result the customer and the driver will give you immediately an object called a completion stage which is in the family of completable feature in the of futures the future rpe in in the java 8 plus which is simply okay when the data will be ready you will get a call back coming from that and only then you can process the result you know on result boom you will process the result so you will get async result set and not only a result on cassandra every single query is paged and you will get the callback when the first page is loaded okay by default the size of the page is 5 000. this is the page size you can make it bigger or smaller for instance if you are working with a web ui there is no point to get 5 000 records yeah maybe you get the first page but yeah the callback is per uh page and you know when you start processing uh the page one when you eat the last element of page one under the hood the driver will fetch page two for you and you can still process but hey what's happened if you get an error you know processing the result of the completion stage is not that easy you do have two um two paths to under so first is success okay you get the result set i mean i think brazil said you're happy everything's fine and the second is a completion error which is you get an exception and so on the exception yeah you need to see okay do i throw an exception do i do i do some workaround okay you got the point so that's a bit tricky but okay that's still okay normal error it's kind of like a try catch right but the issue is in asynchronous you tends to chain one call after the other just to have nothing blocking i mean as soon as i do have the callback for the first i invoke the second as a callback and the third as a callback and then you chain the treatment and what happened is you nested every single code and so each time you nested a new service you have to manage two is it okay is it not okay and now you get the id you get a tree of all the possible case even the first level is failing okay no problem the second level is failing so should i throw the exception up to the first level and they'll add the first level or process the second novel or do a rank around there this is what we call the callback l so what is cool with asynchronous is now you are not blocking anymore you get immediately your futures so you can scale okay um you can scale much more now your web threads are not blocked but you you get the id now you can eat this callback l if the call are nested and you do have a maintainability error okay and moving on to the promise what about reactive okay i put it in green because reactive is cool right okay so this is the reactive manifesto written by some clever guy telling that okay these are synchronous hell is not what we like so let's try to put something better and so they created the reactive manifesto you do have the link on on top right hand corner and basically this is uh see maintainable extensible this is exactly what we were missing with the asynchronous only stack so what is the reactive service so what we try to achieve is first the service to be responsive that means the system always responds in a timely manner okay if it's success or not success you can expect the system to always and swell the same in the same time frame second is resilient okay the system stay responsive in the face of any failure third is elasticity the system stay responsive under via varying workload and adapt itself to variable ingestion rates so that's a bit more not really in the code but more scale out dynamic scalability things you can achieve with elastic load balancer on adws for instance or in you know kubernetes when you when you see that one of the pod is not responding get the health check not running yeah you may want to scale out this component that's part of the reactive what we want and message driven okay asynchronous is still at the heart of the system so the system relies on asynchronous messaging to achieve loose copy loose coupling and isolation okay that's the theory what about implementation well the leading implementation is the reactive stream api again i put you the link so reactive stream is really a specification okay and here you can see multiple implementation of this api so here you do have reactor vertex akka and this is a logo spring web flux leveraging reactor okay so what object do you find in this specification well um you you do have the subscriber and a publisher okay and so the subscriber will send a request to subscribe the publisher will get you an object name a subscription and you as a subscriber you will query your subscription so now you do have an object as a subscriber and you won't query the publisher you win query the the object you have the this question you execute everything asynchronous and then there is a callback on next okay to get the result on next and you know what now you don't have the notification i mean the callback for every single page but for every single record and this is why when you speak about reactive word you you think about nice ui that refresh or too much you know automatically because uh the item the item are sent back to the ui and the idea is anything happening on the db you get the callback you get the notification per record and so this notification can be bring back up to the api layer and framework like react angular any javascript now is able to read this notification coming through the ui you are probably as aware about the websocket and boom the ui can be refreshed automatically so it's now called back at the fine grain level and so you can do fast and small things with this new callback but also you can do more so if you good if you look at the cassandra code but you know it's quite reusing the same publisher subscriber mechanism and now when i execute uh when i bound my statement i get a flux so flux is one of the object used by reactor rector is the leading implementation of the reactive streams okay reactive stream specification reactor main implementation in java and so tools like web flux will leverage on this reactor object so in the code you will see either flux which is kind of a list in the reactive world and mono it's a single result in the reactive world you execute a query and now coming from cassandra you do have a callback for every single row okay and which is neat is if you can keep up you know um there is tons of things happening at the castle level and you as a client you are a subscriber and you cannot keep up with that well there is a back pressure mechanism to tell cassandra okay please please driver slow down i cannot keep up and that's the sweet that's you know that's really what's cool cassandra's famous ice report if you cannot keep up yeah now you can tell cassandra to slow down it's for read but not for right because there is no way cassandra could tell you stop writing uh i cannot keep up but it's not the the topic for today but on a single note of cassandra you can expect about 3 000 second and per core so on a machine with eight cpu it's about 20k requests per second per node and cassandra is a distributed system with basic production three nodes so basically very basic customer can absorb easily 60k requests per second so as a client you need to be pretty strong to to eat that value but anyway just telling you the back pressure is only available for read okay that was a lot of theory right okay and this reactive we will apply that to the reference application we feel which is in the spring community which is spring pet clinic so if you go to the spring.io you will find the reference implementation of the pet clinic but the pet clinic is so famous and so widely used and widely used as a reference app that now there are some people forking and changing the implementation in new way so i hide a little bit it's it's so you can see you can go on spring dash petclinic.github.io and you would see multiple implementation for this sample see you can see here the angular the micro service you know using all the spring cloud stack and i'm happy now the spring reactive is spark is part of this organization so again this organization is community oriented okay maintained by people like antoine ray very active on swearing you know ticket very fast so thank you to the community pretty happy to be part of it and if you start creating some ticket with the sample we are using today i will probably jump on it all right we do have the database we know that we will use reactive so let's move now to the development platform and on that i will let the expert speak i will let moritz take over and i see i will get back to my screen later on so let's see okay now maurice you share your screen yes i do do you see my screen yes we can see perfect um yeah thank you cedric so let's let's go a little bit deeper into a development environment that runs that runs inside the your browser that runs inside the cloud um so let's take a closer look at at gitpod here on the left we see it running we see a screenshot of it running this is basically the website so if you go to gitpod.io it's super easy to just click on this button try now and all you need is a is an account on github or gitlab or bitbucket.org and with gitport you get an ide that has quite a bunch of features that focus around um being ready to code immediately and that focus around collaborating with with colleagues and also that focus around um being powerful enough for professional developers and that focus around reproducibility so that everybody who's working on the project gets a reproducible workspace or multiple reproducible workspaces and in more detail so for for teams we we support code reviews and sharing of uh workspaces and exchanging workspaces snapshots and to be more to offer really really fast workspaces when you think of what you traditionally need to do when you start developing traditionally you would clone a project and check out a branch and study the readme and install the tools and run build and run tests and then after a few [Music] a few half an hour hours or even days you're finally ready to code and gitpod automates most of this so that from choosing a project to starting to code you can do this within within seconds and that's what we're gonna do and you can you can follow this along with me um i will provide you a link links in a in a minute um it's free for up to 50 hours a month and for people that want to use it more professionally or more extensively we also have have paid plans because um running a full-fledged workspace in the cloud is actually quite a heavy thing that also creates costs on our site so to to follow along you might you might open up um gitpod either on gitlab or on github by by following these um these qr codes and um as you do it i will i will do it as well so i will i will switch to interactive demo mode so for example if you go via gitlab here in yeah so the project is available on github gitlab so you can pick what you like yes exactly um for some reason for some reason so just a second right so when i'm on this project and when you're signed into gitlab you have probably seen this on this button web ide before and since a few weeks there is also a second button or another option in the dropdown named gitpod so if you click on that one then a gitpod workspace will open and we have this native support for gitpod in gitlab since a few weeks because we we get part we we started a partnership with with gitlab so we will be working together to make this work really really easily the same works on on github so if you [Music] go to the github project you can either install a vs code extension that installs the gitpod button here or if you do not have this vs code extension installed you can go to github.io or you can prefix the url with gitpod.io pound and press enter and then um a workspace will start on this project you may need to to log in to gitpod um that's something i did already you can do that with your your github github or your gitlab account and what's happening now is that gitpod starts a workspace for me for you um in the cloud and now we have a running workspace and when we take a look at the workspace here in the middle there's already a readme file the readme file from the repository that is open in an editor but i will close this for now here on the left hand side i have all the files from the repository and here on the bottom i have i have terminals and there's stuff happening already and if i open up a new terminal i can also see it's a bash we are under linux we can run any any linux commands and and we see that we have quite a powerful machine where we can just do stuff on the server side and here we see that our pet clinic project has already been compiled and we see that we see this message or we see that it has been compiled because here we see the message the last message from from maven that tells us the build was successful it took um 1 minutes 40 thank you and we know we didn't wait one minute 40. when we opened up the workspace this was already done and that's a feature of gitpod we call it prebuilds so what gitpod can do is gitpodcan can run such tasks ahead of time like before i start a workspace gitpoddoses like uh like a ci cd server so basically when you get pushed to your repository then gitpod gets notified builds a workspace for your new commit and stores it in the cloud and later when you need that workspace it's just there and ready to code and you see this nice message of oh you just saved two minutes of watching your code build because that ran ahead of time cool and same for same for angular and this application or this the application automatically runs so we can now open this up in a browser for example or i can um i can also open this here in a preview then i see it in get pot and now we see the spring the spring pet clinic app running live inside my workspace so the full of the full app so basically from i chose the repository click on a link have the running app in my have a running workspace with the app running in it takes less than a minute so that is i think record rating rating time or for going from zero to to being ready to code um but let's let's look a bit more into the details on how how we actually made this work so the entry point for this what gitpod uses is the the gitpod yaml file um so there's a configuration file that you can place in in your repository and it's it's very useful to place it in the repository because this way it's version controlled and shared with your team you call this deaf environment as code and the big advantage of this is that the setup of your dev environment is always consistent with your code so you can be sure you get the right version of vs code extensions and tools and so on and the first thing the github gitpod.yaml file does is it says um it instructs gitport to take this docker file at the basis for the workspace and when we take a look at this docker file we see oh it takes a workspace folder image installs angular stuff and installs even more angular stuff and when we started workspace what gitpod does is it takes this docker file the docker container from it and launch this as my workspace yes absolutely let's make that a bit bigger this is better yeah i think just waiting for feedback but all right yeah zoom in the screen here like uh command plus just to make that i can do that yeah perfect and make your yeah you can put back your brother full screen much too much overlap so what gitpod does is it it takes this docker file and like when you start a workspace it checks is there such a docker file in the repository and if that's the case it builds a docker container from this docker file and that will be launched as your workspace so basically everything that you install here inside the dockerfile will be inside your workspace so you have it for example accessible here in the indie shell and you don't need to base it on workspace full you can also base it on alpine or ubuntu or debian or other linux distributions and so that is a very very very powerful way of creating a deaf environment which is exactly tailored to your project and at the same time giving you a very convenient control over how you manage it because it gets version controlled with your with your code and inside the git port yammer what we can do is we can we can specify tasks that should run as a pre-built um so for example here or let's let's look at react here this is the command for for the prebuilt so for the pet clinic example we run the command maven clean package install as a pre-built so just like a c icd server if for example cedric keeps on developing on the app and pushes to the git repository then gitpod will run this command to create a fresh workspace from it store it in the cloud and later when cedric or i want to start a workspace on that commit then we get everything pre-compiled and can be happy about this this method um the comma then later the command actually gets executed when you start the workspace so um this command make sure that when when i just started the workspace that we can already see a running app yeah and this is why a question were asked to enter your credential we will do that in a minute this is setup.sh that will use the credential coming from astra initialize the app and start the app yes exactly i will um pass over to cedric in a while and then i assume you said recovering more into details about that one um there's a bit more conflict here about ports so for example the react apis or the pet clinic app is opening up on port 4200 we need to expose those ports to show them in a preview um config to enable the previews and also pre-install vs code extensions that i will talk about more more in a second um but first i want to show to you that this is actually um this is a real ide this is not a this is not a toy thing so um git port supports um vs code extensions and here we have java support installed via via escort extension so the java support that you see here in gitpod is exactly the same that you see in vs code because it's the same escort extension at answer vs code uses and the vs code extension actually packages the eclipse java tooling language server so when i for example hover over a command and i see the java doc that's exactly the same that you would see in an eclipse or if i open um class yes um go to some youtubes and press content assist and i get the real condenses so this is like exactly those functions that i can actually open up on this class this is not a not a while guess based on text scanning or if i do a control click and click on this then i can also navigate to the implementation and we see that this java code this is not from my repository but this is from um associated with the localdate.class so this this java code is somewhere deep in a java file in my in my workspace but nonetheless for convenience it's super easy to access it because it makes coding so much easier when you can actually read the codes that you're calling and what else um other features that we have we can also find references and see where this is being called from there's something that's not working um and one more thing that we can do is um we can also debug so for example i can go here to my spring application and i can set a breakpoint and then i can click here on on debug and then on the left hand side in the debug view i can see how how the application starts well that's a lot of information now i'm really running out of pixels with my resolution yeah and you didn't provide the incredible shots that are running in the java virtual machine and here you can see the call stack and now for example i could um click here to step into the method and now i'm starting to step into the java spring framework yeah that's cool i do have a couple of questions coming from the chat now so first is does the workspace you are using uh keep the states you know is stateful you can go back and yes absolutely um so the workspace as long as you run the workspace it's your docker container so you keep all the files all the processes and so on and when you close your browser window or when you click on stop workspace then the files from the workspace will be will be saved in your user profile and then in your user profile um you get a list of the workspaces that you launched recently so you can always go back to them and start them start them again you like our app and second question is about uh shortcuts so is there any uh shortcuts close to what we are we get used to eclipse or intellij absolutely absolutely so if you if you press command shift p or if you um or go to view find command it's just like in vs code so you get a list of all commands that you can run and you also see the shortcuts that are already associated with those commands so you can use all those those shortcuts and you can additionally you can install vs code extensions that change shortcuts so for example you could install a vs code extension for um for uh the eye shortcuts and thereby make um make it behave like yeah like vi vi whoa no that's yes you do it when you know how to exit but oh geez that's cool so it leverages on kind of vs code and you can install multiple extension by the way in this app you already have two extensions running that is very very true um so let's take a look at this so here we have um um we have an extensions view and um in this extensions view um i can for example search for i have the longbox extension installed and i have the spring boot extension installed and there's also a bunch of built-in extensions pre-installed and here i can for example simply search for other extensions so i can search for java and then i find a bunch of other extensions where i can simply click on install and then when i click on something to install um i don't know let's install the maven so making stuff then i can choose whether i want to install it for for this project when i install it for the project then goes it and goes into my um i get a marker in my git port yammer that will be shared with my team and when i install it for uh isolde so that's myself that goes into my user profile um that is where i should put my vi shortcuts or um the theme that uses coloring that not everybody might like um yeah then it goes into my user profile and will be there on my projects but it won't bother my my teammates and to show what these extensions can do [Music] um i already showed that we have the visual as a spring boot extension installed and if i go to resources and to application.yammer so this config file from um from spring and then press content assist here now it's not so super fast then then we see we get content assist for this gamer so that is this content so this the ide knows exactly which values are are acceptable here and that is provided by the escort extension for um for spring tools and i also just noticed that the find references that i wanted to show earlier it actually popped up here on the left hand side i expected it elsewhere that's why i was a bit confused so we the point reference feature actually works nicely and to really benefit from this when you work in a team there's a few ways that um that make it super or that are super powerful when you want to work with skid pod um one is um reviewing pull requests so for example now when i go to some other repository here is the repository of our um of our ide and open this then i will get a new workspace that starts and and it doesn't matter that i already have a workspace running so this is what i want to show is it's super convenient that here i don't need to interrupt my work just because i want to review a pull request elsewhere i can have several workspaces that just without problems run in parallel okay um question in the chat can you use like private git like aws azure code base or you know a git url you would provide um you can use private repositories from github.com gitlab.com and bitbucket so there we all have support for private repositories okay um we also have support for private you know for self-hosted gitlab installations so if you self-host gitlab somewhere we can also hook this up and on top of that we we offer um you can self-host git pod and and when your cell phones get caught then you can hook it up to mostly anything that supports warehouse and that can also be a github enterprise server or your self-hosted gitlab and when your cellphones get popped then of course you also choose each cloud infrastructure so then you can for example run it on google cloud platform or um or aws and we're working on azure support okay guys um and so now here the the workspace for my code reviews is almost there and here for example i see the files that have been changed or here's only a file that has been added and when i click on details i can also see the pr and then for example i can um i can click on review or i see the or the conversation from the pr and i can you conduct my review and inside the file i can also comment on individual lines and that all gets uploaded to um to github and even more to when you work in a team [Music] um let's say now i'm doing some coding and i'm stuck with some something on astra but i know that cedric can help me what i could do is i could click here on my on my avatar icon and now i know that cedric is with me so what i could do is click on share share running workspace so i could invite cedric to to enter my my running workspace and then we could together work on the same same code um that also works nicely for for educational settings or for mentoring and unblocking obviously but even if cedric would not be there with me right now but i know that he's super busy in another meeting at the very moment but hopefully later today he might have an hour um to look into or to help me out what i could do then is then i take her i take a snapshot from my workspace um so snapshot is like the current state of my workspace state persisted in the cloud and when i take a snapshot i get a um i get a link so now gitpod is taking the snapshot and once the and we have the link i can share i could share the link via email or in your chat and i've said i could click on it then set like would uh here's the link um would get an excel copy of my workspace so the copy is like fully detached from my workspace but it contains the files exactly as i left them um so that they could including environment variables and all the modifications you did yes yes yes that was super useful for asset work or for handing out exercises yeah or you know reports multiple state of your exercise you know you do have you know basic then step one step two step three if you're stuck go to link step three and everything is is done for you you can move on for your exercise so really for any university or in any school so not only students don't have to install anything on their laptop but you know you could have your courses ready to go with multiple commits and these workshops snapshots stated to each comment um yeah that's that's a big part of my or that's my my demo part which is cool which is cool thank you um there are more questions please post them now in the chat yeah not yet not yet but i will watch and if there are let's let's take the question so should i should i take over and finish up there's two more things i want to stress first as i've already mentioned you can use gitpod on gitpod.io or you can self-host it we have terraform scripts to install it on aws and gcp um and we have a hem chart to install it on kubernetes and we recently went open source so you can actually look look at the source code of gitport and compile it yourself try it run it um and of course um improve it and then contribute yeah is this i'd like to hand the lead over over eccentric all right all right so let me go there and there and let's do some little slides um yeah let's let's go with slides okay um there are just two questions maybe we should take now uh before running because it's question relating to snapshots and first is so the snapshot is just a copy it's not linked together with the original and so then is how do you merge a snapshot with the original you know back together if you need to oh this was good so so so if you um if you create a snapshot and therefore you basically branch ground off your workspace what you do is um you would commit the files that you want to keep to get and and push them to your repository and then use the mechanisms of git to to to align them so you would rebase or get merged um however it is necessary and and if that is not an option for some reason of course you can also just download files from your from your workspace to your hard drive and upload them to another workspace but um yeah i think get in the way okay that's cool that's cool pretty neat okay let's go into a little bit of spring and using everything we saw just to build the application will you we still have time so let's let's go into it so a couple of weeks a week ago we introduced you spring uh so it was two hours only on spring and spring boot uh so i won't go into the details today i will go fast but you know at the heart of spring the the mechanism that makes spring a superstar in the java community is really the inversion of control so now you define a single interface multiple implementations for your interface and when you need to use this service you only specify the interface and not the implementation doing that you introduce loose coupling between the user of your service and the service so it's very easy to do mocking to do multiple version of your service and you know even if you have a single version of the service most people doing that all the time now it's so cool to introduce a decoupling between layers of your applications so last time we introduced you the spring boot which is a runtime with everything included in spring and if you need more function you add some spring boot starters so spring boots starter data spring boot starter security spring boot starter ff4j why not i mean so today we will move to the reactive part okay so it's still running inside spring boots but now we are using something called spring web flux so this is spring boot with three layers presentation layer business layer persistent layer using inversion of control in between layers okay and spring boot will help you package your app as a standalone jar which is pretty convenient and easy to deploy as container for instance you don't have to deploy your app in application server and i told you adding some starters you add some metrics health check configuration by the way in the sample today in gitpod we are simply using the spring the rest api but if you go to application.yaml you see that you can enable spring security distributed stressing with zipkin and also prometheus grafana stack it's all there you simply have to enable the flag when you start the app so it's all due to spring boot and the spring cloud world providing all the dependencies you need simply attach what you need so speaking of now reactive spring so this is the key slide so what you used to do with spring mvc which is the web layers for servlets in the spring world both for rest api and controller in the mvc pattern you know model view controller where the view tends to be with free marker or time leaf in the spring world and the rest api of course uh using the the rest part of spring and you can only use rest controllers and have your ui using any javascript stack of course so end point it's moving from mvc to waveflux but that changed something if you introduce webflox in your classpath now all your application must be reactive that means spring will scan all your classes and if one method is not reactive it's synchronous for instance the application won't even start okay it will tell you there you do have a blocking call i'm not starting and so now because you introduce some reactive endpoints because all the nice features i showed you before a fine grain notification now you need to be reactive to the full stack so full stack mean at the database level when you interact with the database so at the connectivity meaning the driver levels then in each layer of your application repository service and controllers so for the connectivity because we are using cassandra we're of course using the cassandra drivers and the customer drivers provide now ways to execute method interactive way so then two way to work with these reactive connectivity stuff you can either use spring data reactive cassandra spring data reactive cassandra which is a wrapper around the the reactive driver from cassandra this is not what we are doing today today we are simply leveraging all the features from the driver not spring data because my point is spring data is very strong but it's like really building a boat inside a bottle so with spring data you do have nice repository okay where you simply have to define interface and entities and method with convention creating the proper method name will execute the proper query but what happened under the hood now what about batch statement but about paging what about consistency level all these features from the driver are hidden for you and it tends to forgot them and always use the default mechanism it's not the spring that i thought it because spring data is so easy to start with that you are doing the default method and it's not what you should do because there will be way way better things to do to me much much faster so what i did today is i simply use the driver to see everything it can it can do for you and later you can use spring data spring data reactive to to to get that um yeah to make that happen and you know with spring data when you get stuck with the repository you switch to custom in class called cassandra operation cassandra template and guess what you are using the class from the drivers okay so i do have this the implementation of the spring pet clinic app using spring that directive as well that could be another workshop so today let's dig into um let's dig into the the the real thing everything you can do with cassandra driver without being constrained by the of the over overlay spring data okay okay so back-end code review all right so first at the database level okay so my i implemented this app and so i took a model which is relational okay if you look at all the existing implementation uh of the pet clinic it just it's only relational sequel and so there are some joints all over the place so the business domain is pet clinic you don't have pets or you know you do have owner owning pets and the pets can go to visit and for the visit you will you will contact a vet which is quite as easy so totally understood foreign key between owners and pets a foreign key between pets and visit and you know integrity constraint visit pet vet in cassandra no joints no integrity constraint no acid transaction and so i simply use the you know the method that is explained in ds220 on this channel is thinking take your entities take all the query you need to build your app and try to first doing one table per request even if it's mean duplicate the data it's not a big deal in the case on our world i know it's surprising for people coming from rational this is how it work don't be afraid to duplicate the data and so after that i tried to to to find ways to maybe reduce the number of tables and it ended up during this data model okay so moving on okay then is connectivity to cassandra so connectivity to cassandra is done by uh the drivers maybe i will show you how it works more in the code and the repository service controller so i put here the the the schema you the architecture you do have the same in the github repo so what you simply need to realize is um you would you will have mostly a dao4 single for each entity and then four services you can have one services per dao or you can use the business service layer to aggregate and compose that are coming from multiple dao and what it's cool with the reactive is now if you need to get all the pets from owner you can you may still need to n plus one select so get details for every single pet but with the reactive stack you can do that all together and get the values for every single pet synchronously and get the result when you get all the requests all right okay so now let's go to the code okay all right so um we are the database it's running we are happy and we do have empty key space what i will do is i will copy my credentials just to have values to be used in in the git box so what i do so i follow the step written there it's pretty quick okay it's simply create a service account and copy the credential so if i go to my database i will go on the top click on my name okay this is what it has been asked scroll down to the bottom just to so just to see the security settings i do have here my security settings so if not i will add a service account and boom i do have a service account from there i will go here and say copy credentials okay i can maybe open can i yeah can i open yes uh this is not what i want yeah so this is what is in my clipboard it's a g-zone with my client id and client secret the thing you need to make pose and steal all my data in my database but yeah it's a demo database that would i will eventually terminate the database now after this workshop all right so with this credential in my clipboard let's get started i'm going to github okay and i will use the opening github opening git pod button i put there but because i use the extension shown by moritz i can also simply click on gitpod button over here and see look at the url it will open git pod and use my my kit so it's starting let me add this guy is it security credential per db per account true per service account per db i guess but now you are asking i should i should try if i cannot access another database with the same account because it's at the organization level so i can probably use contact multiple database with the same service account this is a question to ask to to to people at data stacks because you know what i always use the free tier so i always have a single i always have a single database okay so um two tabs one is the ui okay everything has been done for me and the ui is already running on paul 4200 so i can open the browser and the ui is there but the ui cannot contact the backend because the backend is not started so if you look at the backend tab okay i was asked to provide my credentials so guess what i will pass my credential which is json so this is a small setup.sh script i have created for you and what it's done is simply creating some environment variables oh it did not ask me for my password well that's so strange not that much because in the gitpod world even if you delete your even even if you delete your workspace you you can keep your environment variables and so astra db password was already there saved and so i i had no there is no point for me to provide it again okay so see so now the database has been started i can open the database okay first i show the full demo then i go into the code so now the backend is started and so one call you can make it's going to the pet type because it's a referential data so it has been populated for you so what the application does i start up is creating the expected tables and putting data where needed you know that's pretty cool so i can see what are the pet types in the db okay so i will go try it out and execute and see those are all the pet types available in my in my in my database and so during the type during the time you are starting your backend i can go to my database maybe here show you the sql console you don't have to do it simply i will wait for you to start the front the back end copy your credential copy in the terminal provide the password you're good to go and so remember gitpod has downloaded all the dependency did all the maven command for us and also execute the pre-build okay so describe key spaces is it big enough should i make it bigger okay describe key spaces and see i use spring pet clinic okay describe tables okay um yeah if you just created the database you don't have to provide user password this is because i'm using an older database and the no password to provide to open the sql console has been released a couple of days ago okay so these are all my tables and if i want to see all the pet types you know the one i show you in the swagger ui i will go here and i will do simply select from okay so now the pet types are those guys bird cat dog i'm sterilized snake all right i can create a new one from the swagger but you know that's not the point what is uh pretty neat is look at that gitbo just uh opened a public api so the p the api was expected to run on 996 so what it does is simply map into a 9966 on 80 and now everybody can access that you could access my my this url if you wish and so what we did in the beginning in the in the launch of the of the angular we simply evaluate with gp url 9966 what should be the backend url and like that cross finger if i click on pet type now the ui is communicated to the back end and i can add a new so let's see lizard.cat what's next so what's what's what kind of pet do you have maurice a lion okay oh turtles yeah why not okay so now let's go to the turtle so i'm editing the reference table if i go to the database and ask for the data of course it has been updated okay it's not a mock we are communicated from this ui to the database so and you know before going to code let's make a poll that's cool right we do a full working app with ui and backend and database and you don't have to install anything and it's free all right um okay moving on so let's go back now to the heart the code and to show the code guess what i will use my id no just kidding i will use gitmod okay so remember first let's go each step first the connectivity so if i go to main java to to everything is there conf okay conf and maybe application.yaml which is the springboot configuration file okay so this is the one where it's opened before let's make it bigger this is a configuration file i have enabled some actuator spring see you can enable zipkin by simply enable 2 you can enable using the secure spring security by enabling true and you can either use astra which is a default behavior i do have or you can use local cassandra if you want to lose if you want to use zipkin local cassandra prometheus grafana you need to start all these components using docker and the docker compose is there for you everything is there okay prometheus graffana everything is there so if you want to run everything locally do it the compose is there for you but today everything is in the cloud so let's see i do have application uh application.astra.com so to set up the datastax driver you simply need a single conf file application.astra.com okay and this file can have hundreds and hundreds of fine grain configuration key so first if you use spring data now you need to use the small keys i mean the limited number of keys available in application yaml in spring data and you have not access to all these stuff by default this is there is a way to do it but it's not well written in the documentation yeah you simply go to basics and do basics so first look at that you can so this is the astra conf okay so i'm using environment variables i have created when i've imported the json i do have the same file for local configuration and now i'm using docker cassandra 9042 okay so level zero configuration files okay level one config so what i do customer config okay i will define a bin sql session okay implementing synchronous asynchronous interactive calls okay simple as that and depending of the flag use a straw i will load one file or the other okay very easy very basic you got the id uh maybe one things to say about configuration before going further is i use spring security and i if you enable spring security in the application.tml you will use one class security enable config okay implementing a filter with spring and if you disable you don't have the spring security filter and same for the security swagger you know the small framework we are using to generate this ui uh if you if you disable the security this is what it look like if you enable the security there is a new button over here to provide a user as password as http authentication basic all right configure okay now what about dao one step above okay one step above dao so let's pick one of the entity you know they're all working the same uh pick one entity and see what's going on okay so let's pick uh one vets okay so vet at the db layer first create a entity okay so we are using lombok not to create any getter setter it's been it has this done for us at compile time it's an entity okay nothing much to say about that but we are using uh annotation or those annotation come from the default drivers not spring data i told you but you can do quite the same with spring data so here the partition key clustering column provide name for the columns and with that you do have a mapping from object to the tables so let's use that in a dao so like spring data in the default cassandra drivers there is a deo mechanism so called mapper where you simply define the interface and a code will be available for you as is okay and you create a mapper referencing the dao okay but now you do have access to the sql session so that means you can here easily create the tables so see in the mapper i create the different table using my secret session it's available for me right here and then and at the dao level i can define some you know advanced queries in the select over here okay that's entity level now let's move to service level oh by the way so at the driver level all the method will return map reactive result set and the reason is the driver won't force you to use either reactor or another reactive implementation like vertex so it provides you the publisher which is the interface the specification and so because spring webflux is using reactor at the service layer when i invoke my deal so this is my service okay injecting multiple dro over here and so do look at that i will say flux remember flux is really the class you want to use when you return a list of something so the do got me find all which is a you know map as a result ah mapped reactive result set and i map that as a flux and now i can move from any entities to the vet entity which is the entity used by the service layer remember when that's basic spring stuff at each layer you want to have dedicated entity at the outlayer i have that entity using annotation related to my implementation my database then at the service layer i don't want any dependency to my database so i create a new classes even if it's exactly the same but most of the time there are some mappings because this class we get some info coming from multiple deal so what about using ur m so in cassandra there is no r okay there is no relation so we are using object mapping because there is no relation joints does not exist in casama so this mapper or spring data are really the object mapping so it's kind of ebatis ibanet if you wish look at that if you look at the do interface i will you know select select delete update see i update and i provide my object which is really like connection that save in the ibernet world so but you know we do have an entity i save my entity um i update my db but no it's om not om that's kind of the same but it's not hooked it's not magically oh i will update my object and boom it will magically uh update my database um so i have used ibanet back in the day and you know the developer manipulating manipulating the these data this object layer and having some updates in the database i can guarantee that dba ate that because you're not aware of which kind of query you are firing against the database and this is one of the reasons that now people tend to put api on top of the database because they don't want you to execute any query because if you have fun with the true story okay so that's your dao let's move to the service layer so i i translate the result set from mono or flux depending of what i like and i'm using mapping to map the entity from their level to service level so if i go to the mapping let's see i do have some utils mapping that's okay stupid mapping feel to feel okay put that feeling that feel put that thing in that field there are some framework existing to help you with that mapping but you know my point of view it's is faster to do that like that instead of expecting some yet another layer of annotation to do the mapping not just me you can you can do whatever you like so i do have my services over here let's see what i have it's simply you know it's really code like you would call now if uh java 8 it's all about it's really close to using streams you know reactive streams once you get either a mono or a flux from the result now you can do map okay mapping from one object to another asynchronous for every single record you can flap map so you can do the same but in a dedicated asymptote for every single item and so at the controller level spring webflux we expect to return either mono or flux so make sure that at the service layer everything has been done for you just the last word on these classes is look at that at initialization of the class because i do have after set properties i have implemented initial design being just just uh spring stuff this is why the database is filled when you start and by the way the everything interactive is lazy if you simply create the mono if you don't have anybody to subscribe to this mono nothing will happen you need a terminal call and this is why i put a little subscribe in the end you do not want to do that block because that block will get you everything what you need but i can guarantee if you put some dot block in the code then spring web flux will go after you and say i don't start i saw some blocking code okay so to do it and now the controller layer and it's all working the same so now i keep reusing the same annotation from uh you know spring mvc spring rest so it still get swagger api are still the same now instead of returning a list of that i returned a flux of that like that so really um at that point controller is all about boilerplate and logging and error management because all the all the code to compose has been done at you know the service level and service level is all about aggregating data coming from multiple dio using the nice features coming from reactive you know the flat map and unreach the object populate the object from source coming from multiple data asynchronously and then see it's pretty close to what you have in uh default spring boot app simply now we need to move everything from list to flux anything from object to mono make sure and anything is blocking in your app and you know sometimes playing with all the stack dotmap.list.flatmap the defaultive empty uh in the end is pretty elegant code you know in two lines you you you do a lot of stuff but at the same time you know coming to this code sometimes it's a bit tricky so now which is neat with this application you do have a full working app doing code created with update delete uh both for single entities and on uh groups of entities and really it's not really about all the decisions i made to build the app easily you have a running app with spring security enable with distributed striking distributed trusting enable if you wish so use it as a template when you need to build a app and this is what you know the spring pet clinic community is there for and you can see that a lot of java developer and spring developer really use these uh when you use this uh those repo to uh what i would like you to sh ditcom.com slash this guy so this is a reactive sample okay pretty neat but here you do have the react gs for the ui angular for the ui micro service cutling and it's not the same you know cloud rest i use rest i my you know base code to work and i switch from this guy to the reactive one moving from mvc to webflux and changing the doctor model because i'm using a nosql database but here you do have graphql you have cloud config data gdbc micro service config so if tomorrow you wish to start the app on spring get a look at this community website and contribute if you can i think they are doing a great job by providing ready to go samples okay i was in speed mode uh two we have a question uh why not we modify the ui and db layer first thank you um [Music] yeah yeah because yeah yeah so i got your point so at the initialization in officer said properties uh because i i specif specifically invoke subscribe yeah the the overall starting process will wait for this to be to be to be done yes mono object flux list yeah true and uh flux or or model are really working like streams in the java 8. yeah you you get the map your reduce your uh you know and the you know sometimes you are you are very tempted to go back to streams and start doing uh because you can stream as well flux uh but no you should avoid stream and stay in can because um yeah you don't want to block at any point okay all right so um we we went to the through the cod the code review you do have the sample running on your uh on your cloud instance not your laptop uh i hope it has been uh helpful to you and nevertheless you do have a working app now for you to use as a template and so this is the logical architecture of the app okay this is the one i've used and this is the one completely built and started by gitpod for you and i uh what we did what moritz did is define this guy as the sub module of this guy and so when you open uh your workspace with that these guys are already there it has been pulled as a kitsoft module i was not aware it was possible but i found that pretty useful okay that's the database and that's stuff i did not enable on gitpod it's working with docker but you can do that if you start the app locally oh for in so question about integration test so for integration test of course you don't want them to block and so webflux provide a pretty nice web client so it's not the default http client or async http client it's called web client and you do have the sample in the app in the src test java thank you all for the session today and thank you to maurice especially to first to provide the nice keyboard platform we're all using every wednesday to demonstrate what can be done on top of customers and also to be there today and to show and you know explain what is gitbod and what you can do with it yeah thank you that i could be here it was that was fun i really liked it yeah that's great and you know what we both of you are doing the same in a couple of hours so until then thank you very much all and see you next time bye all right see you and as always don't forget to click that subscribe button and ring that bell to get notifications for all of our future upcoming workshops on the same page [Music] you
Info
Channel: DataStax Developers
Views: 1,100
Rating: 5 out of 5
Keywords:
Id: 1aRbndIcXV4
Channel Id: undefined
Length: 104min 22sec (6262 seconds)
Published: Wed Oct 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.