Learn About Feign Clients Using Java Spring in 15 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to my channel techtutor today i'm going to talk to you about fang clients so when you work with software and you're doing it for different uh industry jobs you're usually going to end up interacting with another software service and you're going to want to make api calls to another service so that way you can integrate with things outside your organization so the best way to do this in my opinion is with vein clients and i'll give you a quick starter project to help you test your fan client as well maybe we'll even use that for future projects so let's go ahead and jump into the code now all right so to get started i created this sample project i'm kind of thinking that i'll end up using it for more than just this video for future videos if i need a server to interact with to showcase functionality that we're using this will be great for that a lot of these dependencies are pretty standard i'm pretty sure i've covered all these in other videos and i'll try to link to those videos in the description but if you have any questions just let me know so there's a docker compose file this is what i'm using to spin up the postgres database for this sample project in this application yaml configuring the database properties as well as i'm configuring http security so they'll be basic auth on some apis to help showcase how fane can be used to interact with apis that have security on them in my sample controller here i have four different types of api so i have a get a delete a put and a post and these are all interacting with our repository class using spring data so that way we can retrieve records delete records add records or update them additionally i have my application configuration class for the security again you have to use this enable web security annotation so that way it'll actually apply and then basically all i'm doing is just applying that basic security that i configured in this application email to again add a basic auth with user and password as the username and password in here this is where i was using flyway to add some records to my database again i covered this in another video already as well this is using spring data to set up your your repository layer to interact with your database and then also you end up needing to have an entity class so that way you can add and retrieve records from your database as well so now that we've covered pretty much everything in this sample project let's go ahead and jump over to the fame project and i'll start explaining everything there how you get set up with a basic fan client how it it's really easy to use it's a good abstraction to you know simplify basic http calls and it's even more robust than that you really can do a lot of things with that but this video is just going to get you started and then i'll also show you where you can look at the documentation to make additional configurations as needed all right so now over here in my fane project if you look at the dependencies you'll notice that i'm using spring boot version 2.5.2 and then the spring cloud version 2020.0.3 the reason why i'm pointing this out is because in a moment i will show you where you can figure out which spring cloud versions are compatible with which spring brute versions this is important if you pick a version that's not compatible you will probably have issues additionally i have some of the standard dependencies spring boot starter spring restarter web the new one here that i'm using specifically for fade is springs cloud starter open fane i like to use lombok in almost all my projects it's just easier for getters setters all that abstraction and then you do need something for dependency management when you add spring cloud at least unless you're really wanting to control your dependencies to a finite level otherwise you can just include a spring cloud version and it will automatically set the version of your cloud dependencies and then this is just some additional standard setup here for when i want to build my project taking a look at the application yaml you'll see some basic configurations so i'm putting all my apis to have slash api in front and then i like to disable histrix i also do prefer to use okay http as my client for my fan client uh i'll show you this in the documentation just a moment and then also i am setting this uh the client url the other product is 9090. the reason why i did that in the sample project which i forgot to mention earlier is so that way i can run this project on 8080 so 99 will just be the you uh the localhost and port number that i'm running on that project at all times just to make it easier because most of the time unless you configure a different port it will default to a80 so now let's go ahead and look at the documentation i was talking about really quickly so here and the spring cloud project documentation here you will see how they have the release train compatibility so this is exactly what i was talking about earlier you can see how this is the version that is compatible with the 2.4 and 2.5 versions of spring boot otherwise if you had older versions of spring boot you'd want to use hoxton and so forth i will put a link to this in the description so that way you can take a look yourself in case you decide to use other versions of spring boot over here here's some documentation about the fane project portion of spring and so in here there's lots of good information again i'm kind of going to give you a very general project to get you started but there is a lot of abstraction available via this same project and configurations if you need something very simple my project will probably be fine but otherwise as you can see here there's so many different settings so many options you can configure automatic retries uh you can also have specific error handling uh here you see that there's a part about using a basic os request interceptor and i'm actually going to show you how i configured my fan client to handle the basic off you can also configure the timeouts the log level i mean this thing is robust so i really prefer to use the fan client when i'm interacting with other projects and i need to make http request so you know i hope you also find this to be very useful and if you have any questions uh and you need to configure your project a certain way just let me know but i hopefully my project is enough to at least get you up and unstarted and if you decide you need more then hey you could take a look at this documentation and dig in and keep customizing your fan client to your needs all right so let's go back to the project all right so next over here in my spring boot application class you will want to enable fan clients you could do this here you could do it in a configuration class it doesn't really matter but you do need to do this if you do not add this annotation your fan clients will not work and when you try to wire them into other classes it's just not going to find them because they're not enabled so here is my sample fan client right here so this is what we're going to be using this is basically how you set up a fan client uh as you can see use the bang client annotation uh you can call it whatever you want here i called my sample client and then this part's important so the url sample client.url i come back over here you're seeing where that's where i was setting up the uh post and port number of my sample project that i'm hitting so you do want to have a way to match that up this allows you to add it in dynamically you could have put it here hard-coded but i would not suggest that and then you also can optionally add a configuration class which i'll cover in just a minute i did that because of some settings that i wanted to add and showcase to you that i think you'll find useful and then as far as when you want to add the api calls that it's going to make to the other project it's very similar to how you set up a controller really uh you know you set up the api endpoint as well as which type it is whether it's get delete put or post the parameters that go to the url so i mean if you've worked with spring controllers it should look really familiar it's almost intuitive because it it's so similar and here you know everything's abstracted away you don't have to put the full url uh the parting the starting part of your host name and port again is configured in that application yaml and you're just really putting the end part of the api uh for each location that you're interacting with as well as the payload again this is all very similar to your normal controller setup so continuing over to that configuration i was talking about the reason why i added it is because i wanted to be able to handle basic auth so as you saw in my sample project i ended up adding my basic auth where the username is just user and the password password i would not suggest this but obviously you can see that what this is doing is for every request that's going over to the other project this is going to intercept that request and add your user credentials so this makes it really easy you don't have to go add your credentials into every single call into your fan client which you can do but i find this to be easier because then you add in one spot and it's covered for all of your api calls now if you need to customize it because let's say that you don't actually have the same type of authentication on every api or whatever your other customizations may be you can obviously change it out and do things differently but if you have the same type of authentication on all apis this is great it makes it really easy then i also added a controller for this project so that way i can send requests to this project which will then get forwarded to the other project i'm going to showcase this in postman in just a bit so i have some really basic apis i want to get the data from the other project to send a delete request from this project over there update the data or save new data which again forwards to the other server that's running and then saves it to that postgres database all right and then also i utilize this sample request class i made this class just to help set up my payload that's going to go over to uh the other server and i actually had set up sample response but i forgot to use this i don't really need to use it because if you look in the sample controller i actually just simplified it and returned a response entity and started populating that but you could always instead of doing what i did here have actually used the sample response and then just returned that back in your controller it's up to you there's lots of different ways to do this but this is the way i ended up doing i just forgot that i created this class i never even ended up using it so what i'm going to do now is i'm going to go ahead and run both projects and then it will showcase how to basically use them and show that they're actually working via postman all right so let's go ahead and click run on this project or if you haven't run it before you always right click and then run from here all right so that one is up and running now let's go ahead and run the other project all right and it's really important that you actually uh run the docker compose if you don't obviously this project won't work because it's the one interacting with the database uh again if you have any questions let me know i'm going to link to those other videos but obviously if your database is not running this will not work so it's very possible you'll get some sort of error where it's saying your database is not running so because i skipped those steps you have questions let me know but otherwise stay with my other video if you want to know more about using docker to run a postgres database all right so now let's go ahead and use postman to showcase what the project does so if you hit the get api in the fame project with the first three records you'll see you have some data other data and more data and what this is pulling from is a sample project i have used flyaway to set up the first record some data other data and more data so as you can see working next we will go ahead and add a record as well so let's just go ahead and add some record with let's just say new data and the id of that one is 13. so to prove that that got added let's go ahead and fetch 13. hey you see new data and it was created today now let's go ahead and update a record so we could update uh record three i think is what i had here so let's go ahead grab records two three again and you'll see some data other data more data and now we'll update this one and we'll set the value to updated data and so now when i fetch record three it should be updated data there it is and lastly if you try and delete records i believe i had actually set this up ahead of time to showcase that i have some error checking so that way when you search for something to delete it or fetch it it actually returns a 404 this is in my project that way i'm intentionally covering whether a record is not found or it is found this is something you customize on your end how you handle certain results coming back from your fang client so that way you know your front end code could properly display to the user what's happening so obviously there's no record 999 so it's just not going to work and so then if you go ahead and let's delete record let's delete record three the one that we just updated so returns 200 okay because it found it and deleted it and then after that 404 because it's no longer there and then when we try to search for records one two and three as you can see three is no longer there so the delete was successful so this is a very quick introduction into saying clients and again how they make http calls very easy how they abstract out even you know adding a security layer to your fan client to do basic authorization or again there's tons of configurations you could set up for automatic retries or other features that bain has added out there for you so if you have any questions just let me know in the comments if you want to see anything more about thane tell me about what you want to know or if there's any other topics you'd like to cover in these future videos let me know i will find out if i don't already i'll research them and i'll put some videos out for you don't forget to look in the description where i will link both of my github repos for the sample project as well as the vein project and please take time to like and subscribe for future content thank you for watching
Info
Channel: Tech Tutor
Views: 1,131
Rating: undefined out of 5
Keywords: docker, software, tutorial, java spring, feign, http
Id: LkmhwaxlduQ
Channel Id: undefined
Length: 16min 2sec (962 seconds)
Published: Sun Jul 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.