IntelliJ IDEA Conf 2022 | Kubernetes Native Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] good to be here how are you I am enjoying all the sessions I am just uh in the backstage listening to all the amazing speakers and consuming all the amazing content yeah I mean I just caught the tail end of uh Mary's amazing uh talk on Apache Pulsar which is super right up my alley I'm I'm gonna like watch the whole thing from the the beginning because it sounded like there's some really good information there and I'm I'm just sad I didn't wake up early enough to see the beginning you know foreign [Laughter] so even though Josh hey um yes so even though Josh doesn't need an introduction let me try and uh do that part um Josh is one of the most kind and humble leader that I know and he is one of the reasons why spring is an extremely popular framework author of six Books open sorry I pronounced it incorrectly I can go on and on and Josh apart from folks so I'll just stop here because I I can take a complete session to introduce you um so Josh I couldn't want to ask you a question apart from folks asking you about kubernetes how often do you receive a question on when will Spring boot 3 be available ah yeah so that's due this year 2022 I'm not sure when it is it's there's it's a public uh GitHub uh you know we announced a schedule and all this stuff but I think it's November or December right so yeah it's it's soon right uh late late November early December I would expect to see uh several things spring framework six spring boot three and of course uh uh completely separate from this no equip it's just a coincidence spring one our big old uh annual 10-pole conference that happened to be at that same time so you know random great that is good to know so now I will add your screen to the stream and let you move with your session good luck and have fun thank you do I have about an hour is that the idea or how long do we have uh yes you have uh 57 minutes oh boy oh very good thank you so much okay we'll use the last five to ten minutes for the Q a but if uh you want to take that longer then we'll cut short on the questions okay I'll try and be as quick as I can be thank you so much Molly it's great to see you again as usual um oh okay good morning good afternoon good evening to everybody else I appreciate you joining us today here I can't believe we're at the end of day one I am just I'm really bummed I missed uh uh Mary's amazing talk I'm you know we have a new uh Apache Pulsar uh integration for Springs called Spring for Apache pulse or whatever and so I've been playing around with it it is phenomenal like really I didn't I didn't know uh about it before but then the um the uh the scientists on the spring R D team they they put this project together and it's like oh this looks amazing I mean it Mary talked about some of those uh points which is uh you know it's multi-model right like you can you can use it in terms of kafka's API or webmqs API and uh and of course it has its own thing it supports lots of different like patterns it's just really really a phenomenal piece of technology and again you know you come to a show like this to hear great stuff like that and that's why I'm glad I'm a part of it um my session won't be nearly so um educational but you're here so I appreciate that let's get started my friends my name is Joshua I work on the spring team asthmologist well explained I'm a spring developer Advocate at kotlin Google developer expert and of course a Java Champion um and most importantly most presently most saliently I'm at your service so should you have questions comments feedback whatever uh please find me on the internet there I am Twitter LinkedIn email if anybody's using uh email I have several blogs I have a book called reactive spring another one called Cloud native Java uh from O'Reilly and um and I have a YouTube series called the Spring tips and I do that every Wednesday or ish you know sometimes I uh take time off to produce new stuff and work on other stuff in the background but a lot of times it's on Wednesday uh I have a podcast every Thursday uh uh in the Americas but you know Friday for most everybody else in the world um and then of course the twitch Channel which is you know that's I don't use it enough to recommend it but it's there uh and so basically I'm just aching the online I'm here for you if you have questions comments feedback whatever so yes I'm gonna do my level head best to finish our presentation uh with enough time to spare so that we can have some questions at the end of this but should we um exhaust that time don't fret friends our time is uh finite here at the show but our conversation can continue well beyond the uh the duration of this little talk okay so find me on the internet let's let's be friends I'm happy to help uh and today my friends we're going to talk about building applications destined for production we're going to do so in terms of the latest and greatest in the spring boot ecosystem so uh spring boot three and spring framework six at the aforementioned spring boot three in Spring framework six uh and we're gonna do this because um you know I want to build applications uh that that do the right thing in production work today is often as not production is kubernetes right and so there's a lot of concerns there's no like one single kind of group of features I can point you at and say oh this will do everything you needed to do in kubernetes it's really about building software design for production so that it does the right thing anybody can build an HTTP endpoint that serves up some data that's hardly the hard part right uh it's everything after and before that it's all about the uh the non-function requirements it's all about uh you know availability reliability all the itties you know observability all these things that you need to care for so that your application does the right thing at scale uh when you're trying to catch a few Z's uh and get through the ninth without any issues you want your software to do the right thing and so that looks like a lot of different things so today we're going to talk about a smorgasbord of just different things you can look at that will make your application uh you know uh do the right thing do a better job in production and as you get you can gather by now we're going to do so in terms of spring boot uh three which is due again later this year so if you have questions Now's the Time to to get the bits and kick the tires and try things out um it's a big new release okay friends this is not just a point release this is a new generation a new uh a new base you know for spring boot right we've got spring boot one that lasted for several years spring boot two which lasted for most of the spring framework five generation uh and now spring boot three which lasts you know which will I imagine keep uh uh track of spring framework sixth generation remember we released spring framework five in September of 2017. so here we are September of 2022 five years hence uh and um you know we're releasing a new generation of spring framework but we don't release new generations of spring framework all that often right where we release Point releases like 5.1 5.2 Etc um but base Generations that's a new opportunity for us a new Baseline a new revision a new generation uh and you know looking forward extrapolating extrapolating out a little bit what do you imagine we're going to be doing in five years right uh five years time five years hence uh will be what five you know spring framework seven right I don't know who nobody can see the future of course but uh just imagine a time where it's now spring framework uh seven and here we are in 2027 right uh Java Java we'll be using Java 29 given the Breakneck speed at which they release new versions of java every six months uh we're in Java 19 right now Java uh 29 will be the the the the jvm version of the day so by that metric by that sort of uh prism you can kind of see that the new Baseline revision the new Baseline for spring framework six is Java 17 uh and that may seem a little aggressive again in today's terms uh but it's really not if you consider what that implies that means that in five years time or whatever if we're all still using spring framework six and we haven't moved on to Spring framework seven or whatever then you can still be writing code using Java 17 while the rest of the world is using Java 29 right and that's an incredible long tail of compatibility and that's one of those things that again we did the same thing for spring framework five which supported uh as a baseline Java eight okay so uh you can still write code using spring framework five and Java 8 today why would you no reason there's no reason at all ever uh to do that but you could if my point you shouldn't but you could um and so I just want you to keep that in mind this is a new Baseline requirement Java 17 which is fine Java 17 is awesome um and also there's a new uh a new thing uh which is that we are supporting the new Jakarta ee apis so where spring uses uh and supports Jakarta ee apis uh those those offer a you know there's a new opportunity here the um the eclipse Foundation open source Jakarta ee projects are tantalizing to be sure but bear in mind there's going to be a a road bump a small Road bump but a robot nonetheless which is that due to politics not technology okay keep in mind the technologist didn't do this to us um uh but due to politics and you know other unsavory things um the new Jakarta EE apis are separate from and different from the Java ee apis that they uh replace in that uh in order for anybody to make a change to any of the existing types in the javox namespace so jabax dot persistence javox.validation javox.jms Etc all the stuff that used to be in Java ee under the javox namespace in order for them to get away with open sourcing these projects they had to move the package uh and basically there's a requirement that if they make a change to any type or add a method or delete a method or do anything to the types or you know do anything like that it has to be in a new package and that basically meant that it was just easier to move everything to a new package so everything that's ever been written in the last 20 plus odd years of uh g2ee Java Eno Jakarta ee uh will break basically when you upgrade to the latest and greatest version of spring boot that's it it's just a package there's you know find and replace is your friend uh and from their framework integration perspective uh you know we've done everything we can do so that if you're just superficially consuming this stuff for example via spring MVC you're using the servlet abstraction but you actually don't depend on anything specific to the server abstraction then you won't notice this it doesn't matter to you it's just hidden behind the scenes because you're just writing controllers and using uh the bit of interaction that that provides you um so it's not going to be that big a deal like I said I would be very surprised if there aren't tools that make this just an absolute Breeze uh to um to uh to migrate to so that's that's the the long and short of some of the big new Baseline revisions Chief among these new features the real the real reason I'm excited is for this new aot support and we'll get to that in just a second but uh let's get into it let's actually start writing some code so we're going to do is I love to do we're gonna go over here to start spring.io and we're going to build a new application again using spring framework 6 and spring boot 3. we use 3.0 M5 I'm going to call this customers okay and this is going to be a application I'll just bring in the web support I'll bring in uh H2 which is an in-memory embedded SQL database I'll bring in jdbc I'll bring in the spring boot actuator um and there we go there's my my new project now bear in mind I'm also using Java 17 that's the Baseline I could use Java 19 that's acceptable as well um for spring framework uh you know for spring framework six as we mentioned uh you can't use Java 8 and Java 11. now uh this is for good reason they are terrible versions to use in production today there are there is no situation which using Java 8 or 11 even ironically is appropriate right uh Java 17 is technically Superior to Java 8 in every conceivable way it's faster more memory efficient more observable more secure more robust more syntax which more performant more operations uh friendly Etc it's just a better piece of software in every conceivable way it's also morally Superior to Java 8 in every conceivable way you won't like the look of Despair and Chagrin in your children's faces when they find out that you're using Java in production don't do it be the change you want to see in the world do the right thing Java 20 Java 17 is the right thing to do in 2022. my friends uh I don't think I need to go on about why Java 17 uh is an appropriate choice or Java 17 or later is an appropriate choice but just if nothing else just judging by the version number alone I think it's fair to say that Java 17 is more than twice as good as Java 8 and for this reason you know I hope we're all on the same page now we're going to hit generate we're going to generate a new project I'm using Java 17 because growl VM happens to have a major release uh for the native image compiler that works uh with Java 17 as well so this is a great place to park our code these days RM vs store little file system droppings okay good so I'm going to open this up in the IDE IntelliJ of course um uh that's not just uh pandering to the to the uh to my wonderful hosts and and so on I just love uh IntelliJ there's other choices out there but I tend to use this one for a lot of reasons um okay so let's enter presentation mode uh here we are at a little larger 30 points out very good okay so we have a brand new application it's going to be a simple application that uh that that uh just manages data so I'll just create a record here look look Mom no lumbuck isn't that nice so integer ID string name uh and uh you know I want to have a primary key uh that uh you know oops I forgot to bring in Spring data uh ABC didn't I so JBC and voila you can always just copy and paste the build back into your uh file and re-import and life is great life is golden or you could have clicked on that little Maven icon up there okay take two so there we go there's my basic entity I want somebody to persist this so I'll create a customer repository that extends the crud repository uh for entities of type customer whose primary keys of type integer I'm gonna be talking to a SQL database an embedded SQL database but a SQL database nonetheless so I need some schema so I'll just create a little file here called schema.sql uh let me see uh okay good uh and then it'll be ID serial primary key name VAR car 255 and not null very good um and uh there we go there's my basic schema from my entity and I want to actually just initialize some records I want to have some data in the database so I'll create it's spring Bean I'm going to register a bean that listens for an event in this case the application ready event and I'll just call this the data listener and it's just a simple callback interface in which I can write any custom initialization logic right so um uh you know to initialize my schema in this case and to make it work I need to provide a pointer a reference to the repository uh and so then I can just say list thought of and we'll just list some people that are awesome on the spring team and also I'll be in the list that's separate from the uh they're you know seven people that are awesome and then also separate from that me so uh Jurgen of course the uh Mr spring Madura amazing uh Stefan uh amazing uh Violetta legendary I mean I don't know have I don't have enough nice adjectives for all these wonderful people uh the good Dr sire one of my all-time favorite doctors along with doctors who strange subramaniam and uh Zeus uh we've got um uh who else do we have who else do we have uh uh uh yushin here we go uh we've got um uh I don't know do I have everybody here uh Olga here we go what about and then finally me okay there's some names um and now for each one uh for each name I'm gonna turn it into a new customer so no for that and then I'm gonna turn that into a list I'm just going to save the whole list of uh of people so customers okay and I'll say uh repository Dot save all customers uh and then I can just print out the results as you know I can print out each record from The Returned collection that I get there this can be a Lambda thank you IntelliJ for helping me with that uh this can be cleaned up like about so much nicer if you just let the ID do some of the walking for you um okay good so now I've got some I've got this basic initialization logic here not a big deal but it's just nice to have um I want an HTTP endpoint so I'll say add controller class customer HTTP controller and private final customer repository I'm going to create a Constructor there and customers collection of customer yet and what do I I'm gonna just say find all I guess and it's oh it's an iterable isn't it I knew it it's never it's never the one I always remember okay good so there's this uh it's going to be an HTTP endpoint not a not an MVC uh endpoint solve just create a user response body uh okay so good there we go there's my basic application let's just kick this off and see what we get it'll probably be fine but let's just try Okay so localhost customers voila there we are there's our data uh uh it worked of course it worked this is a demo it was always going to work uh and that's not even really a question what I want to talk to you about is this this is the spring boot ASCII artwork it works as you'd expect but here too my friends I have some uh not sad news but yeah I don't know it's a little sad it's a little Bittersweet um we have a feature in springboot 2.x that allows you to put a a replacement Banner so you can put like a file called banner.txt in your Source main resource directory and that'll replace what's here you can also put banner.jpg or GIF or PNG and it'll turn those into text for you and then render that if you have an animated gif then it'll actually render a curses animation of the image right now right unfortunately that last feature that ability to take an image and turn it into text is going to disappear in Spring boot 3 because there are a few people were using it and it's obviously obscenely inefficient but you know at least you can still replace it with a banner.txt so if you really want something to show up there uh you there's any there's a number of different online converters okay good so we have an application we have some data in the database it worked of course it worked that wasn't the hard part right what we care about is uh getting this thing into production So to that end there are some things that we need to care about like for example observability this is Made Easy thanks to the spring boot actuator which is a set of managed HTTP endpoints so I'm going to say I want to enable uh all the different actuator endpoints I'm going to show all the details here I'll say always I'll show uh the the I'm going to use the kubernetes probes as well we'll talk about that and so that's just for demo obviously you shouldn't do this in production because I'm making everything you know straight up open and available which is not what you want in a public uh setting uh so localhost actuator and there's our different beans and points and the caches and and health and info info is an empty tabularizo you can put whatever you want in there there's lots of ways to configure it you can even have a build plug-in capture the state of your git repository and then create a file called get up properties in your target directory that then gets swept up and automatically included in this and that can you that can show things like the get branch and the git commit and ID and the the author and all that stuff um so this is interesting for identifying the service you've got reports that show you what conditions were evaluated uh at um at startup time to to result in the congealed fully formed running application that you see before you you've got your configuration props you've got your environment variables and system properties and all that you've got loggers that you can use to uh to update and you know change the log levels for different loggers in the system keep dumps and thread dumps that you can use to profile the application and of course metrics which gives you statistics key value pairs that tell you information about the application and its health and its state and that is in turn powered by a project called micrometer which is great for dimensional metrics okay and this has Integrations with all manner of different time series databases including but not limited to uh Prometheus which is super common in a kubernetes ecosystem uh we've got uh you know just everything Prometheus Netflix Atlas then Chase Google Cloud stack Trace uh sorry stackdriver uh you know just anything you want right any any good uh vendor here now this is really important to bear in mind because later today we're going to talk about um we're going to talk about uh uh aot ground VM native images right and in that world Java agents don't really work that well right I I've seen some like prototypes where people got something working but just generally assume that's not going to work the way you think it does or at all uh and so you can't just transparently put a Java agent in your JRE and just have it automatically uh you know capture the state of your application um but that's okay spring it makes it trivial to integrate that in your application Level code so now you've got observability in terms of metrics and in terms of tracing provided in the spring ecosystem as part of your binary this has been true for years not just in Spring boot three what's different in Spring boot three is that we're creating a new abstraction called the observation API and basically that observation API is a unified uh abstraction that lets you think about both tracing and metrics with one API you don't have spring Cloud sleuth over here for tracing and micrometer over here for metrics now there's one single concrete thing that you can use to capture information about the activities and durations of things in your application uh consistently right and so that's new in Spring framework six and then spring boot three and again the the hope is as it has always been that for a lot of your use cases you just add that to the library to the class path and it just automatically captures that information and broadcasts it to in to either just in memory as it is doing right now or to one of these time series databases this thus circumventing one of the limitations of gravian native images Okay so we've got a lot of different uh actuator endpoints here uh one that's very useful of course is the health endpoint which gives me information about the different subsystems in my application that may or may not fail and should they fail uh I need to know about it right so I have a status here a five 500 sorry a 200 status code and some Json that says up if anything should go wrong um then you know it'll say down and I'll get a 500 status code I can of course contribute my own custom Health indicator pretty easily just by defining a bean of type Health indicator I can also change the state of the endpoint let me make this a little bit smaller so that I can kind of get into the code a little bit here we go uh so I can create a health uh HTTP controller right and I'll create a um endpoint here called down and void down and what I'm going to do is I'm going to turn I'm gonna I'm gonna change the status of that indicator to reflect the fact that the service is down when somebody calls this endpoint in order to do that I need a pointer to the spring framework application context which I'll inject into the Constructor here and I'm going to publish an event spring has an uh an event bus from one component to another so I can publish an event anywhere and any other component can listen for that event so for example if some other component wanted to listen to uh an event regarding the state changes you know the availability of the application they could just say is my app uh available for example right like this or instead of defining the class without class here of the I you know the class little of the event I can actually just inject the pointer to the event either way you'll now get a dispatch here um when there's an event of interest to you and there's actually two subclasses of this and you'll get both of those because of you know polymorphism right so availability change event and then what else I guess it's not where is the other one then oh uh payload oh well one of those two I guess anyway you'll get that event if this if the event has subclasses you'll get both of those as well um and so it's easy enough to use that event bus to communicate asynchronously from one component to another and that's what's happening here behind the scenes we're publishing a availability change event uh using the event bus the we're injecting the application event publisher and just sending the message uh that says that the service is broken something is wrong so let's restart that um Josh if it's okay for me to interrupt you the phone size if the IDE is a bit small so if you could increase the font size for the edited that's perfect thank you so much okay cheers um okay so uh let's see control I'm sorry curl uh HTTP localhost 8080 forward slash um actuator for Slash Health right so it says uh well it says up right clearly uh if I wanted to then um knock it down I could say make that request and then get back down and you can see now everything the liveness state says down the aggregate status reflects down and actually if I did minus V you'll see that the um status code is now 503. so now my application is sick and if you've plugged in this liveness and Readiness probe uh if you plugged in these con this this health check into kubernetes by specifying aliveness and Readiness probe uh then kubernetes will see that the service is down and it'll respond by killing off the Pod now it's important to keep in mind what a liveness and Readiness probe are a Readiness probe is an HTTP endpoint uh that spring boot automatically supports for you for example here uh Health forward slash Readiness right there it is and liveness Readiness kills uh kubernetes whether the service is ready to be put in production liveness tells kubernetes whether the service should stay in production whether it's still able to continue serving traffic if any of these components if any of these subsystems should reflect down then of course uh the aggregate status reflects that and then kubernetes is obligated to do something about it right and it'll it'll send a Sig term like this a Sig term which is a suggestion asking a process using you know to posix posix uh uh standard it's a suggestion that tells the process hey we're gonna We want you to gracefully shut down please um when spring boot receives this it'll shut down the application context it'll shut down all the beans and shut down the application context and shut down and shut down in an orderly fashion as best as it can uh if the process is still not being if it's still not done and for some reason it's you know the runaway processor it's just not responding the container orchestrator will send Sig kill now there is a bit of a gap usually there's no Gap at all really between when this state term happens and when sick kill happens but you might want some time maybe 30 seconds for example right if you want that built up you can configure graceful shutdown okay so uh uh down equals graceful and what this will do is it'll um stop accepting new requests and allow you to taper off any existing in-flight transactions uh and and do so in the times that you specify here 30 seconds you need to also specify that uh that time span in your kubernetes pod or deployment descriptor right um but basically if they're lined up then you'll have 30 seconds from Sig term to Sig kill so great now you now your applications gracefully cleaning up after itself it's not going to just continue running uh and then have all your state get dropped on the floor for no reason this is very useful again for building resilient systems I've got observability we looked at the probes we looked at the actuator we looked at graceful shutdown some of these things are kubernetes specific and so it might be useful to draw a configuration uh from the places where typically you you're accustomed to storing configuration in a kubernetes context that is to say in a config map or in a secret a config map and a secret are basically Maps or dictionaries keys and values uh that you can make available to your application in one of two ways you can either take the config map which is an entity in kubernetes with keys and values you can make that a uh a um an environment right you can mount it as an environment in which case you get environment variables so the keys in the config map uh become the names of the environment variables in your process uh and then the um the values in the config map become the values of those environment variables again and in your process that's one way that works out of the box so spring boot automatically normalizes things like server underscore Port uh if you have you know an environment variable server underscore port and that's in the process when you're running that'll automatically override server.port you know uh look you know in the property file right so the environment variable from without the process will override the config in the process um or you can take that config map and create a directory where the files in the directory are the are named for the keys in the config map and the contents of the files are named for the are are the values of the keys in the config map so I can actually import that directory structure that unique kubernetes config map secret directory structure we call that a config tree I can import that here like this I can say Okay I want to bring in this desktop config directory into the application so let's now go to the terminal and we'll go to the Shell here okay make their config CD config Echo you know 80 90 server.port okay so there we are there's our directory cat server.port there's the application let's see what happens if we restart this application even though the default is server Port equals 8080 we should see that it is spun up on Port 80 90 because it's drawing that external configuration so this is great in a kubernetes world where you want to have additional configuration that you get from one of these two Vehicles either environment variables or config trees okay great so now we've got an application we've got configuration uh you know I think we're ready to start talking about going to production and here the question of packaging comes up right uh obviously the the first thing people wonder about is uh how do I get a Docker container out of this right so here we have a technology called build packs it's a cloud native Computing Foundation um standard and it's a technology that makes it easy to get a container out of an application artifact be it a ruby and rails app or dot jar.war.net application a python application a whatever right any uh nginx and static site kind of use case like for view or whatever any kind of application you might have there is probably a recipe that reflects the best practices for deploying and for containerizing and deploying that application and so those build packs are extensible they're open source you can Fork them of course you can create your own and plug them in for your organization and so in this way you get a consistent image every single time and each build pack reflects the wealth of the community experience managing and containerizing and deploying a particular style of workload this the Java build pack for example uh is something that the spring team has contributed to uh heavily in the last you know decade or so of of its existence right in some form or another um and so that wealth that expertise in managing Java applications Enterprise job applications is reflected in the wisdom in that build pack so it's stuff you don't have to reinvent uh and spring boot you know you can you can use the pack CLI out of the box that works just fine but spring boot has support uh out of the box as well so I go to downloads uh CD customers and I can do Maven spring boot build hyphen image and then span back we'll get a cup of coffee or something this takes you know a minute it's not pleasant but it does work um and uh and then you then you have a container you can Docker tag Docker push and then specify in your deployment or pod descriptor for kubernetes but before we can even do that we have yet another opportunity here which is I want to make my application a little bit more efficient a little bit more uh performant Java has an amazing track record right Java has been an amazing Boon to all of us it has been a a a gift uh that sun and now Oracle uh and the community have given all of us to use and it is something for which I am endlessly grateful because I I cannot even imagine how much it costs uh to build all of it all these 27 odd years or whatever of of its history how much that cost in terms of uh human power and uh you know r d and initiatives and marketing my goodness I mean just all this stuff adds up to just extraordinary staggering amounts of money so I am grateful for it um one of the things that's amazing about Java is it's uh it's speed right uh Java whenever it has been given the choice between being small or being fasted is always always and twice on Sunday chosen to be fast right uh and sometimes to the detriment of being small so what do I mean that by that well you know it's it's it's a common commonly understood that Java's garbage collector can be very fast right obviously uh this is a unique thing if you look at the state of more systems level kind of programming languages 30 years ago garbage collectors weren't a common thing they're not even that common even still today um and so the fact that we have this amazing garbage collector that sits off to the side collecting our garbage you know uh while we're do while we're making garbage almost as fast as we make the garbage sometimes faster uh that's just amazing and it means that our code is free from these tedious uh malloch and free books where you Malik some memory and then do something with it and then free it at the end of the at the end of the scope right that that constant hiccup of memory those bursts of memory that you see uh when you see C programs and so on that can slow things down with Java you know you can use the memory and it gets added to a Italian and something comes along and cleans it off later it doesn't stop what you're doing right in the meantime so that's one thing that's really nice the other thing that's amazing is that Java has a just-in-time compiler this just-in-time compiler is you know like it's one of the secret sauces of Java's amazing success it's one of the reasons why Java has been uh so useful at scale in large organizations is because if you have a frequently accessed code path something that gets accessed many times in succession Java will come along and turn that code path into architecture and operating system specific binary code so you'll get a Linux native kind of binary or or Linux native code uh or Windows or Mac or whatever in the running Java application it's crazy it really is an amazing piece of technology and it's one of those things that makes Java so so scalable it's one of the reasons why large organizations like Google and Alibaba have used Java for decades at scale and indeed large organizations like that even know about this Dynamic so a lot of times they'll warm up the application before they deploy it so they'll you know and try to exhaust uh or to uh to uh invoke or to exercise a particular code path so as to trigger this just in time compilation and before they actually turn on live traffic so if this is such a useful thing then why not proactively take advantage of it why not do that instead because after all it cannot be very efficient to have a basically like a clang or you know a GCC sometimes that I can't imagine it's very efficient to have a whole native compiler in the same process space the same memory as the code the 10 on your online online web requests right that's going to take space and time in the background somewhere um and yet we do we do it all the time we ship a basically a full compiler tool chain or you know a half of one uh to turn our code into native code at runtime adaptively so why not be done with that why not just get rid of that why not remove as much of the runtime as possible why not uh make all of our code native well the reason we can't do that with 100 accuracy and impunity is because to make it native to truly get the benefits of going native it's youthful to trim away as much excess uh stuff from your application as possible and so grav vm's native image compiler does an analysis for reachability it looks at your main code uh and it looks at all the types that that uses then it looks at the types that that in turn uses and then looks at the types that those intern use and then it looks at the types that those in turn use and so on until it's gone all the way to the end of the of the tree and found all the types that are being explicitly used that you have uh uh you know explicitly nude up or referenced in some way uh in your code and it keeps those in one bin and it preserves those for the uh for the runtime of the application everything else it throws out good bad or otherwise it throws it out so whether it's coming from your code from the class path from the JRE itself all that stuff gets thrown away if it's not in that explicit uh analysis of reachability this is good and bad it's good because yeah your application is much much smaller as a result it's bad because Java even though I just likened it to systems programming languages of 30 years ago it actually has a lot more in common with the likes of uh JavaScript and Lua and Ruby and Python and and uh you know Pearl and uh lisp and and all these other very Dynamic sort of runtime you know uh scripting languages right that we so-called scripting languages it has a lot more in common with those languages it's possible in Java to take a Java Lang string like just an actual string literal with Java source code in it at runtime take that string compile it again after your application is started up create a DOT class file load it into the file system load it into the class loader create a new instance of it with class.4 name and user and use reflection to create a a message and so on um all this to say that uh you know it does this every it you can do all this without actually having explicit type you can do all that in terms of java Lang objects and the reflection methods and this is stuff that your compiler your gavian reachability analysis can't see it happens at runtime it's not deterministic right um so you need to tell gravim about this you need to tell about tell it about the fact that you plan on reflectively creating an instance of this type uh and so that it doesn't throw away that type information that class file Etc um and so that it still works at runtime likewise if you plan on doing other more Dynamic things like reflection like serialization like jni like loading a resource file like a class file or whatever from a a jar like a you know class dot get input stream if you try and do any of that you need to tell gravim about it because it's not going to work otherwise and so this is the problem you want to make these applications work in gravia but something needs to account for all these little Dynamic things that grauvam just hates very very much right remember ground VM wants your code to be like C code right it doesn't want you to think you have a reflective capability because you don't right once once you're in a native image context there's no like the runtime is not nearly as as powerful as it is on the JRE so in spring in the spring ecosystem for spring framework five users that is to say the current generation uh we built a skunksworks r d project several years ago called Spring native in which we incubated several different approaches uh for automatically turning a spring boot application into a gravam native image application a lot of that has work has then uh it's gone as far as it's going to go there's not going to be a ga release of that but you can use it if you like all that was done in terms of you know working on top of spring boot as opposed to Below in Spring framework at the component model level the deepest low levels of the component model in Spring framework six the new generation coming again in just a few months uh we have a deep integration of spring native into the spring framework code base and that is called the aot support in Spring boot uh three so you'll see that we have an incredible component model for aot for our purposes though just to get this trivial application working all I need to do is to package it as a native image so native and I'll skip the test to spare it some time uh and they'll take oh I don't know a minute or so and again behind the scenes it's going to do an analysis um and it's going to create some you know it's going to uh the compiler is going to capture information about what types it should it should include uh but during compilation our Maven plug-in is also analyzing the code right we're actually capturing information about all the different spring beans that you have configured in the application context and that's being used to dynamically generate to create a a rarefied version of all the state that would have otherwise been kept at runtime so for example all those Siege lib subclasses those Dynamic cdlip proxies that gets turned into actual cost files that you know this is not for you to consume directly this is for the benefit of the Native image right here uh and ditto we do a lot of work to analyze the state of the application context as it would appear at runtime and then to then capture that so that you can recreate it so that when you start up the native image you can skip all the reflection and all the scanning and all that stuff that happens in the beginning of a regular Jerry based spring application and just go straight to the fully formed running state of the application so you can see that reflected here in these generated classes um and you can see down here beam definitions uh send it this one you can see we have a method here where given a bean factory the thing that contains all of your spring objects uh we have programmatically registered definitions for all those objects right so here's every Bean in your application whether it's contributed by you by your own code or by the by the pro machinations of some of the auto configure duration here's every Bean and the you know the factory methods that are required to create that beam so that it looks and works and does exactly what your code was doing before so now I've got this native image I can run the application customers and there's our application uh up and running you know no problem at all um you can see it's fast right I'm using embedded Tomcat I'm using embedded H2 I'm using uh you know all that stuff and it's still a tenth of a second uh to start up everything um obviously if you're using reactive code that's even faster much faster and much more interestingly at least from my perspective is the memory footprint right so customers is here's the PID for that uh binary okay and I have a little script here bin rss5 kid and what this does is it you know it was PF minus o uh and gives you the RSS for the actual binary and I can just run that I can say PID okay voila there it is 110 Megs of ram to run this application as opposed to the average you know what whatever gig of RAM that most uh applications seem to take uh for one reason or another on the jvm these days uh so this is a good deal right especially if we've done our job well enough and all the community uh Integrations that are out there have also been updated to take advantage or to contribute metadata to support this gravvium compilation process then you should be able to take huge swaths of spring applications upgrade them to Spring framework six and spring boot three and then take advantage of native image support um so now we have a native binary that's running it's fast it's efficient let's create one more uh client here very quickly just to look at a few more things here okay so let's say we wanted to create a a new service I'm going to call this The Edge service it's going to use spring for graphql and we'll use spring Cloud Gateway uh and we're just going to hit generate okay so uao Edge one and the idea here is that we have an API but I want to be able to forward requests to that API from the outside world and here it makes sense to have like a door a doorstop a gatekeeper something that um that stops incoming requests and makes sure that they're secure and handles routing and handles uh canonicalization of the request payload and all that kind of stuff and this is a natural use case for things like an API Gateway which you can support with spring Cloud Gateway so I'll inject a route locator Builder here and I'll uh just say return rlb.routs.build and I'm going to build a route spec and the the spec here is the predicate that matches incoming requests coming in to this Java process on my port 99.99 let me make the font bigger here there's that there's that okay so it's a request coming into that uh that hosting Port that I want to then forward on after adapting it or transforming it in some way to the back back at back end service for customers now this is saying take the request match the path uh as being proxy and then send it to this one well of course this is the path on the downstream service this is the path on the incoming request I need to swap one for the other and I can use filters for that so I say set path equals customers and so now the path is being changed but these filters that's where the real power is here right I can do things like adding a response header it should be headers dot uh HTTP headers.accesscontrol.orgen now I'm supporting chords I can support retries I can do uh you know rate limiting I can do all sorts of things I can do authentication all with spring Cloud Gateway this is one very natural way uh to you know integrate external systems to your code um route locator Builder uh route locator Builder what did I do wrong and not bring in Spring Cloud Gateway I got spring Cloud Gateway oh I don't have the web server friends don't be like me let's see spring boot starter webflex using the reactive apis reload okay take three this time with more gusto nope didn't it didn't work um route locator Builder what is going on there is this a new thing then I stumble onto let me see oh you know I don't know I'm just gonna fix it right here and see if that fixes it for me control space copy okay take three this time you know maybe it'll work who knows let's see come on hooray okay good so uh there we go there's our uh Gateway application go to localhost 99.99 forward slash uh proxy right and there you go you get the the actual data from the downstream service on localhost 99.99 for such proxy there's this unseemly uh Neti error if you get that and you're running on an M1 and apple M1 as I am uh then just add midi all and you'll feel better uh for it and that'll just stifle that ridiculous error um okay good good good so there's our Gateway another thing you might want to do is to adapt your apis in some way uh to canonicalize them right so one opportunity for this is um graphql right so API Dot spacex.land and see if this still works okay there you go this is a API for graphql this is a graphql API from SpaceX which is a space variant company uh and you can ask for all the different information that you want about their data right and so you can say hey I want I want just the mission uh I want the launches and I want the mission name and the date and all that stuff and if you do that then you get back only the data that you specify and no more so I it doesn't matter uh where the service that provides this data lives I just have one coherent view of the data here uh and I can work with as much or as little of the data as I want and also I have schema so I can look for for example the CEO I can look for uh you know information like that so now I can have one endpoint that I can use to drive all sorts of different views both the details and the overview page for example of a of search results could be driven by the same endpoint here just by bringing in more or less uh uh you know attributes so I like graphql because it makes it easy to pull together different data sources to have different um to have a you know uh payload that match the expectations of the client and so on so that's very quickly here create a simple graphql schema so you can see what I mean so graphql and I'll create a new file here called what you know um idea whatever it doesn't have to be called anything in particular just graphqls or dqls uh and then here it's conventional to have a root type called query uh off of which you hang all the queries you know all the questions that clients can ask and this implies that I'll have a type called customer so I will um but I also might want to have a new type that we haven't seen before maybe I have another microservice called it profile so I'll Define that here as well and I'm saying that this has an ID um and that's it okay so I'm going to return customers this could be from another microservice but it conceptually it makes sense that this profile information would hang off of the the customer type and you can actually make you know you can describe things as being not null like that or nullable like this and so and I it's very easy to describe that now let's just Implement that using the spring for graphql API so I'll say CRM graphql controller and we're going to make short work of this quickly here so customer and again the Java type the representation of the data doesn't have have to match the graphql representation of the data so I'm going to create a new type here with just an ID and you know the customer data you can imagine how we get that we can just uh private final web client HTTP we'll just use the reactive non-blocking HTTP client here I'll say that I want to map for the type called query I want to map this field called customers to this endpoint that then resolves that data okay so uh customers return uh this dot http.get.uri HTTP localhost 8084 customers dot retrieve body to flux customer class voila okay now there's our uh client for that I also want to get the profile data right so if somebody asks for the profile data then they'll use this schema Handler and the type will be for customer the field will be for profile and again I don't actually have another microservice but let's just imagine let's just imagine I'm making a network call in order to make that Network call for the profile for a given customer well obviously I need a pointer to the root aggregate the customer itself so let's just say you know just imagine that instead of me doing this inline thing I'm actually making a call to another network service now this query type is as I say conventional it's going to be there for all of your applications basically so there's actually a meta annotation that you can use to make short work of this particular combination uh query mapping is equivalent to scheme mapping where the type name is defaulted to query and by the way you don't need to specify the field if you just trust the name of the method to do the job right so in this case I could drop it here but obviously in this case I do need to specify the type since spring has no way of knowing otherwise so okay there's our graphql code I want to inspect all this and make it visible so I'm going to use graphical true let's restart this okay oh which part did I put this on um I was kind of 99 I need a reactive non-blocking HTTP client I'll vend that instance here okay very good return Builder dot build restart okay good localhost 8080 graph sorry 99.99 graphical okay and this is a console that you can use to query the data from your your API endpoint and you can see it's the same thing I get schema I get the data back but if I want the profile data look if I don't include that it gives me an error saying hey you need to specify the subfields if I do that then I get back the data this profile method doesn't get called unless I ask for that data so if I don't want to pay the network cost of calling that of the service I don't need to if I just need the ID in the name great so graphql is a great way to build integrated services to pull together different apis in production which makes it easier for you to support distributed computing via the likes of microservices which in turn makes it easier to get to production faster my friends I think we're kind of at a useful stopping point we've just covered a number of different things that I think you should probably take a look at if you like uh going to production more effectively um today we talked about using spring boot 3 we looked at basic uh you know how to build a credit app who cares about that we looked at Observer ability we looked at containerization we looked at Ground VM and aot support we looked at Edge services and patterns like API gateways and graphql we looked at observability I mean we looked at a number of different things my friends so I want to thank you so very much for all of your time and I hope you have a wonderful day a great weekend and uh and uh and thanks again thank you so much Josh I was wondering whether we are talking about uh amazingly fast drink boot or blistering fast demo by Josh long because there was so many uh compliments um in in the channel I I want to show some attention to this is excellent demo and thank you and then we had uh this is amazing and this is my favorite presentation um wow thank you there's another person who said he has no idea what you're doing but he's he wants to be like you so that's something you can do better trust me so that's that's another uh compliment and thank you love the usage of Records job is awesome and this is yes I'll quickly uh show one of the questions that we had uh would you like to take up this question uh sure uh okay is it a known bug when using spring boot 3 image compilation resulting in this error you can read the error on your screen that's a long one so uh that I don't know and if you send me a direct message on Twitter at Starbucks man uh and send me like a stack Overflow or something where you reproduce it I can't it looks like there's something with the uh the bug with the the build plug-in there but I can't really tell I have no idea okay if Swagger so we don't directly support it but there's at least two different projects out there that do uh support Swagger so yeah you can use that I I don't I I bristle at having to use uh like Swagger gives me metadata about my types a lot of different approaches give me metadata about my types it seems like to build Services we need metadata about our site our type so to the extent that that's the only way to get it um [Music] um you know for for rest then use it obviously but things like graphql just have it for free ditto for uh our socket different ditto from you know grpc and a number of other things that already have types built in I think it's worth having that you know up front so when we have more comments coming up they are talking about how amazing the session is so amazing coding we have so much info uh we have another comment which says amazing energy and great job on the presentation question oh that was amazing you you know uh you have the proof now uh it's a quick quick question because we are running out of time what distribution of kubernetes do you recommend oh I quit Shameless plug obviously I work for a cloud computing company the tons of division of VMware we have a kubernetes distribution but they're all good I mean you know kubernetes is like Java it's just really really good open source software that you know the hardest part is managing it it's not about running it right and just getting somebody to keep wear the pager that's what you're paying all these different vendors for is to have that problem be their problem instead of yours but it's all the same wonderful code fundamentally and you can it's it's like spring you can add to it and uh it becomes more in your capable hands okay so the uh the last question that I'm going to take is how should we handle observability in the world of coral VM native images okay so that again speaking to what I talked about earlier the aot support yeah in ground VM Java agents don't work so look at things like the actuator that we talked about uh and so on yeah and also uh okay yes uh okay so thank you so much Josh that was a power bagged session probably the most loved session for the day today thank you so much for your time and always a pleasure to learn from you pleasure is mine thanks everybody have a wonderful day or night thanks [Music]
Info
Channel: IntelliJ IDEA by JetBrains
Views: 4,193
Rating: undefined out of 5
Keywords: kubernetes, spring boot, intellij idea, intellij idea conf, jetbrains ide, jetbrains
Id: -P2sLy7NaFc
Channel Id: undefined
Length: 60min 13sec (3613 seconds)
Published: Thu Oct 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.