Spring Cloud AWS example using SQS and ElastiCache for Redis | AWS Primer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
few days ago i posted a poll on what video you want me to make next on aws and most of you suggested spring cloud aws with sqs and elastic cache in this video we are going to look at spring cloud aws using sqs and elastic cache with redis as the caching implementation let's get started [Music] press the bell icon on the youtube app and never miss any update from tech primers initially we are going to look at the architecture diagram on what we are going to deploy and how we are going to integrate sqs and elastic cache within our architecture after that we will directly jump into the hands-on and we will try them out in the aws environment i'm going to use aws ec2 instance to deploy our spring cloud aws application which is based on spring boot i'll be connecting this particular service to an sql service to send a message to the sql service within amazon sks if you're not aware is a simple queue service which you can leverage for point-to-point communication as the logo denotes you can publish a message and you can consume a message as a receiver so we are going to consume the same message from the sql topic and then just log it initially so this is the first part of the video i'll be doing this separately and then we'll deploy in the aws environment and then we'll see how we're getting the message once this is completed we're going to integrate the caching layer so we're going to store the message into the elastic cache instead of directly storing it into a database i'm just showing you how you can store it in an elastic cache instance if you had remember i had done a video on spring data redis we are going to leverage the same kind of implementation in this example as well because we already are using springboard all this is triggered based on a post request for a user endpoint which i'm going to expose from the amazon ec2 instance also i'm going to use a get request to pull the data from that redis cache to show that okay this cache has been updated every resource within this architecture or every service within this architecture will have a corresponding role and we need some permissions in order to these services to communicate with each other for example the ec2 instance needs a specific role to connect to the sqs topic same way we need to have a specific role or permission to connect to the elastic cache instance all these will be configured as a part of the same video as well so i'll be showing you how i'm going to use these policies to update my existing roles to get permission to update or query sqs or redis cache with that let's jump into the hands-on part like always i'm going to start with start dot spring dot io i'm going to create an application with a group id com tech primers i'm just going to call this as a spring cloud aws example one so i'm just going to remove the package name to be contact primers i'm going to use java 11. also i'm going to use the version 2.3.9 version of springboot i don't want to try the latest version yet so i'm going to use 2.3.9 the dependencies which i require are the spring web dependency because i'm going to expose an end point if you see in this architecture for exposing the endpoint i'm going to use spring web and i'm going to use some aws specific dependencies because i'm going to integrate with aws services i don't have to integrate with rds because i'm not going to leverage rds but i need the core and also i need the dependency for sqs right so i'm just going to say select squeeze as well so sqs is basically for messaging so these are the three dependencies which i'm adding and then based on our integration for example we need reduce and things like that we can add it later so i'm just going to generate the project let me generate this project and then open it in intellij so the project got opened all my dependencies are getting downloaded since i tried the whole demo with a different version of springboard it's just downloading everything so let's wait for the import to complete so the project got loaded let me open the project it's a plain vanilla project there's nothing going to be there so in order to create all our resources i'm just going to create a different packages i'm just going to say controller as one package i'll also create one for model one for service so let me create the controller so i'm going to call this particular controller as user controller my traditional user class which i used to create nothing fancy here this is not a production grade application so don't just follow blindly so i'm just going to call request mapping and i'm going to expose an endpoint called slash user and i'm going to add a post mapping because i want to create a particular user right this is going to be a part of the request body and user is the model which i want to create let me create that in a bit i'm going to create it under the model class i'm going to implement serializable because i'm going to publish it into the squares topic as quick topic needs a serializable object so i'm going to implement serializable i'm just going to create private string name i also need the id i'll also add a string parameter for salary just for the namesake i also need the uh private static final long i'll just create some version so i just created equals two string hash code all my data setters variables etc right which is the default stuff obviously i can use lombok i'm just not using it here right now so let me import okay so this is imported but because by default it's in the same project so let me create and then we can add a log statement creating user so what i need to do is i'm going to publish this so i'm just going to return true here true or false right because i want to publish the message if this particular user was sent to a queue or not so what i'm going to do is i'm going to create a sqs sender service i'm just going to create it because it doesn't exist so let me create this particular class in the service package i'm going to annotate it to this service annotation and we are done i'm going to auto wire this so as a part of the service i'm just going to send the message right i'm just going to send this particular user that's all i want to do so i'm just creating a method called send with the user as the message so let me create this so the written type i'm just going to have it as void i don't want to return anything i'm just going to say user now in order to send the message we need to create something called cue messaging template so similar to how we create rest templates redux templates inside springboot there is something called queue messaging template so i'm just going to create a q messaging template so this is the class i want to auto inject it but i want to inject the amazon sqs asynchronous class into that particular queue messaging template so q messaging template is used for any interactions with any type of queue here i'm going to use amazon sqs so for that purpose i'm just going to create and inject a amazon sq asynchronous topic so i'm just going to create a constructor and i'm just going to instead of injecting this whole template i'm just going to inject the amazon sqs async interface right so this is going to create an implementation of the amazon sqs and then inject it into the queue messaging template so i'm just going to pass that particular implementation here so instead of auto wiring it i'm just going to inject the amazon sqs async so this way this template will be now plugged on to the amazon sqs topic by default so i'm going to send the message into this so i'm just saying uh queue messaging template dot there is something called convert and send i'm just going to use that and i need to send it to a particular sql topic right i mean i'm just going to create a new sqs topic which i'm going to call as user cache details because i'm going to use this particular queue to interact and then update my cache right so i'm just calling it as user hyphen cache hyphen details and i'm just going to say what message i want to push that's it so i'm just going to convert this user json message or user model into a message which sqs understand and that's why i'm doing convert and send i'm not going to convert into a format which amazon understands so i'm just going to leverage queue messaging template to do it as a part of convert and send and this implementation and i'm going to send it to this particular topic so i have not created this topic yet let's create the topic in a bit but that's all right i mean i'm just going to add some log message here just to show that okay we're sending it that's it so i i'm going to expose an endpoint which is post as a part of it by creating a user object and then pushing it into a queue so this aligns with our architecture diagram if you remember i'm just going to create a user and then push it into sqs topic now i need to also consume it from the squeeze topic i'm not going to create a separate ec2 instance for it so what i'm going to do i'm going to plug that consuming part also in the same application just to make things simpler i'm just going to use the same application to consumers also so i'm going to create something called sqs receiver so you can definitely do it in a separate microservice just for simplicity i'm going to create it in the same microservice so i'll just create sqs receiver again i'm just going to call this as a service because i'm just going to leverage this bean for just receiving the message now the beauty of spring cloud aws comes in when you have annotation based options for listening and publishing right so in the earlier publishing use case we just leveraged the template like how we generally do within um spring and now we are going to leverage another annotation to just retrieve the message spring boot exposes something called as sqs listener using which we can listen to a particular queue so i'm just going to call this q as user cache details that's the queue name and i'm just going to give some name to it so i'm just going to say user cache listener and whatever message i'm going to receive i'm just going to enter here so from the queue we are going to consume the message automatically just by using this annotation and it's just going to get the message directly to the function i'm just going to log it saying received message for user that's it so i'll just receive the message into this particular method and then i'm just printing it nothing else so we're going to achieve this particular part here so i'm going to publish a message and then receive the message and then store it and show it here so what i'm going to do is i'm going to do the infrastructure setup for it so done with the coding for now unless we want to do the integration of cache which will take bit more time but i'm going to create all the aws resources which we require the first thing is uh we need to have an ec2 instance so i'm just going to create a ec2 instance so let me go to ec2 service and i'm just going to create i can do a launch instance i already have one instance which is running you just saw i'm not going to leverage it so i'm going to leverage a new ec2 instance so i'm going to launch a new one i'm going to leverage the uh t2 micro uh 1gb of memory is enough for me so i'm just going to say review one launch um so i don't want to modify actually i want to modify some [Music] oh what was that i wanted to configure the instance with a security group right so i'm going to create a new security group and i'm going to assign a new im role so what happens is when i'm pushing the code into the ec2 instance i'm going to push it via an s3 bucket so this is hidden in the architecture but how do i get my code into the ec2 instance i'm going to push it to s3 bucket and from there i'm going to download it in order to do that i need a role for it right so i'm just going to leverage i am role for my ec2 instance so that i can get access to the s3 bucket so i already created this role called ec2 s3 role read role let me open it up and show it for you so i'm just going to the im console so here i have a role called as ac2 s3 read role and inside that if you see i'm just giving access to amazon s3 read only ignore the elastic cache thing for now but understand that i am just giving a read-only access to s3 bucket so i'm just going to give that role here i am role for the cc2 instance and i'm also running it in a default vpc right that's it i don't want to add anything else complicated let me go to uh i think security group so in the security group i need to have a way for my machine to access this ec2 instance right so i'm just going to say uh use my ipaddress so i'm just selecting my ip address here so i'll be able to log in from my local machine my local terminal into the cc2 instance so i'm just giving that permission and done so let me login i already have the keypad with me so i'm just going to use the same so let this ec2 instance get launched meanwhile we need to create the sqs topic so i'm just going to go to sqs amazon sqs just search for sqs and then you can get it let me open up in a new tab here i already have something called user details as one queue i'm going to create a new queue which i'm going to call it as user cache details so this is my queue i don't want to have a fi for queue so i'm just creating a standard queue there is something called um visibility timeout delivery delay and received message wait time so visibility timeout is when how long can the message be um seen and how long should i retain my message like for example this message is written for four days visibility timeout is when within 30 seconds if you are not able to consume the message right i mean i can make that message available immediately if my process consumes the message and it's not able to process that message in the next 30 seconds i can make it available so amazon will make it available visible for other consumers to consume right i mean if it is consumed from the same topic there is something called delivery delay using which we can delay the message to the receiver so i'm just going to add a delay of five seconds right this is just to make sure we get the logs we look at the logs and we understand that okay this is retrieved by the same process right so just to show that delay i'm just adding a five second delay so we can look at that for the access policy i'm just going to say define who can send message to the queue so this is where i was mentioning we need to send the message to the queue all right now and we need to have a role or access policy for it so i'm just going to select this particular option where i'm going to enter specific aws accounts or i am users or roles which can have access so what i did is i created a role or what we can do is we can create a new role so let me go to roles or let me go to users right i mean i i think i already have a user called app user um yeah so i think we can leverage the same so i created this user called app user under the app user i have given multiple roles so i have given a role for sqs right you can like ignore the other stuff i can i'm just having a read-only role for ec2 i also have actually we can remove the cloud formation rules we don't need any additional roles as such so i've just detached that particular uh policy from here so these are different policies these are permissions so if you remember from the architecture diagram we added permissions these are the permissions so i'm giving permission for sqs to have full access which basically means this particular user whoever has access with this particular user they can read and write from this particular queue so i've created a specific user this particular user will have a secret key i will show that in a bit so i'm just using the same user and i'm giving some permission for that particular user on what he can access and what resources he can create and have read read-only access to so these are the things which he has access to right um so i'm done with so i want to copy this arn so i'm just selecting it i will be pasting the ar in here so only this particular user can publish the message to this particular queue so that's what we are defining here right who can send messages to the queue only this particular user who can receive the message from the queue only this particular user so this way we can control who has permissions to access this particular queue so i'm just creating a queue let me hit the create button so that's it the queue got created and if you look at it this is the url of the queue right if you want to directly access from my api coming back to the ec2 instance if you launch the ec2 instance you can see that the easy to instance is launched i'm going to connect to this particular ec2 instance let me click here and i'm just saying connect i have the ssh client so what i'm going to do is i'm just going to use my ssh command and let me go to the terminal here and i'm just going to paste it so this will ssh onto my ec2 instance and let me go there done so in this machine by default java won't be there if i say java come on not form so i'm just going to say yam install java obviously in production don't do this okay you need to root so i'll just say solar su obviously in production you cannot run things as root as well so make sure you don't do whatever i do in production these are all just for proof of concept purposes so let me go back to genius um let me go back to the project so here i'm just going to build it right right now we did not build it we need to build it and push it into a s3 bucket so that we can download it from the ec2 instance so i'm just going to say clean and install let's do a clean and install uh in the palm xml i think we saw um okay so we can see that there is aws starter dependency there is also aws messaging dependency right all these are the different dependencies which we require i'll also need to add some configurations because to connect to the sqs topic i need some credentials right so for that what i did is so i added some aws credentials so these are the credentials which are specific for this uh user which we created here so the app user whatever we created i'm just going to use this from my application and these are my access key and secret key obviously in production you can leverage sts for assuming role and then trying out but just for the demo purpose i'm just going to use a access key and secret key to connect to sqs so that's what i'm doing here so this app user has these as the access key and secret key i'm also adding some more properties for example i'm hard coding my region as us east 2 because this particular app is going to run on us east 2. so if you see here this is us east too so if you want to dynamically set that variable from outside the application you can definitely do that i'm also disabling the cloud formation stack because this application will come up and then check if the stack was created automatically so i'm just disabling it because i'm manually deploying the application i think my tests are creating some issue let me remove the test package for now don't do that in production so the build is successful and we are good what i'm going to do now is open my terminal inside intellij itself and i'm going to copy this aws s3 and i'm going to copy my application which is what is my jar name my jar will be like springboo spring cloud aws example this i'm going to copy it into my s3 bucket and the bucket name is let me go to the console and then see what is my bucket name so i created a bucket earlier i think yesterday so let's use the same bucket i don't want to create a new bucket so there is a bucket called tp code bucket so i can leverage the same bucket so i'm going to use the same bucket pp code bucket if you don't have a bucket created you can definitely do that i'm going to call this application a spring cloud aws.jar so this will copy my application from my local machine which is under target folder which is this file it will copy that into the s3 bucket so let me copy this and this is the command aws s3 cp so right now my package size is 28 mb and this is just copying the image now into the s3 bucket meanwhile let's go back to the terminal and you can see that java installation got completed let's do a java version java iphone version so i have java 11 installed which is open jdk version so i'm good with that so i'm currently inside the home ec2 users let me go to the root so i'm going to create a folder called app mkdir app so inside app is where i'm going to deploy my jar so let me so right now it's empty so let's now do aws s3 ls so we can see the bucket from here as well so you can see that there are two buckets so let's check if the upload got completed yep the upload got completed now we can download the file right so i'm just going to say s3 copy and we're going to give the reverse ep code bucket slash spring cloud aws.jar and i'm just going to copy it to my local directory so just got downloaded i'm just going to give some permission done now my jar is available for me i have done let me go back to the architecture so my application is now deployed within the ec2 instance i have code for both publishing and also for receiving it i have also assigned my role with the permissions for sql topic i also created an sql topic i think that's it right i mean we are all set let's go to the ec2 instance so i'm going to run the application so i can do java jar spring cloud.jar so this should bring up my application right now i'm using the default profile everything is default so let's create it so the application is up i need to hit the end point which is um this users right so what i did is i did not expose any load balancing for this particular ec2 instance right in order to do that again i need to create a classic elb and things like that i don't want to make things complicated for this example or the demo right so i'm going to create another um terminal window so i'm going to log into the same ec2 instance and let's so i'm logged into the ec2 instance from here as well let me do a clear so my application is running in this terminal which is connected to the same ec2 instance i'm just connected to the same one here as well i'm going to do a curl so let me do a curl um for the endpoint http colon localhost because this is inside the ec2 right so i'm going to hit the post endpoint from the ec2 instance itself so the application is running in 8080 so i'll just say localhost 8080 and my endpoint is user i'm going to use the response body which i'm going to enclose within single quotes and i'm going to pass name which i'm just gonna say ryan i'll also pass the id which i will say one for now i'll say salary for now i'll say 12 000 done i'll just enclose this with the single brace so that it's done so this is the message which i'm going to push right this is the user i'm going to create and i want to send the header the header defines what kind of message type we're sending right so i'm going to say content type as application slash json so this is what we are sending that's it right and also i'll say what type of message it is it's a post message right so i'll just say iphone x uh to denote what type of message we're sending this is a post so i'll just say post that's it so let me hit the enter and notice that in the top we already got a message saying creating user sending user to sqs and you can see that after few seconds we received the message also saying it is ryan actually i should have printed the whole user object but at least we got the message right so looks like we send the message from the application to the queue and from the queue we received it because if you look at the code we had separate service for receiver which came via this for example here we added this message here and we also had a separate code for sending the message which is sending user to sks here so looks like our code worked so with spring cloud aws the advantage what we are getting here is we did not do much we just added annotations to connect to the sqs listeners and we configured the credentials using the cloud database credentials using the access key right obviously you can leverage sts for assuming roles and then doing it without any access key kind of a use case you can have temporary tokens and using the sts and then you can connect it that's the best way but i'm just showing a demo with the access key here so that's done um now the next step is to integrate the cache right so in order to do that uh obviously we need to add some more code in order to integrate redness into the code we need to add some dependencies right so i'm going to add some dependencies so i've already pre-loaded my radius dependency so i'm adding spring data radius for the same version i'm excluding the i o lattice jar and i'm adding the jettis library so j this is one library using which you can connect to the radius from java i'm also adding the elastic cache jar so that we can leverage the elastic cache api right so that's how it's going to integrate and then call elastic cache so those are the java dependencies which we require there are some properties which we need to add for example i'm just going to say aws service config these are the three different properties which i need to add the cache type is there it is i need to add the host name here in order to add the host name i need to create a redis cluster so let's create the redis cluster because redis cluster creation takes a bit longer so i'll just say elastic cache let me go to elastic cache i will show you so inside elastic cache there are two implementations of elastic cache one is the memcached one is the radius so i'm going to use the redis here as an example you can also leverage memcached if you want so let me create a new cache so i'm going to call this particular cache as user cache right because this is where i'm storing the user i'm going to call this as user cache user cache is the name of the cache which i'm going to use i'm just going to leave everything as default you can change the port number you can change the engine version default readers version you can also change the instance type right right now i'm using a 13 gb machine for my instance so let it be there i'm okay and i'm also having two replicas right so that will be three instances now one master and two replicas i will also configure it as a multi-easy that's it right and i don't want to have any backup i am going to disable it so that's that there is something called security group if you see here by default it's added a security group right it's using the default security group why security group because in order to access the redis cache you need to have some permission again right so i need to configure my ec2 instance to have access to this particular security group so i'll show that in a bit but this is the security group it is using that's it i don't want anything else i'll just say create so let this create and once the cache gets created it will give us some primary endpoint and that is the end point which we'll be adding as a host here so let's let's not add that here right now but let's go and add some code so now the change which we need to do the application is when we receive the message we need to purchase the message into the cache right so here we have the data here but i need to cache this message now how do i cache this message right so that's where i am going to integrate into the redis repository so i'm going to create a redis repository using the usual spring repository implementation or the way of creating it right so i'm going to inject a repository called user repository so right now it doesn't exist so i'm just going to create it all right so this is an interface so i'm just going to create a interface i'll just create a new package called repository i'll also annotate it with and it's an interface so i don't want to annotate it with anything so i'll also say user opposite interface i already pre-loaded my code with all those examples so i'm just going to that's it so we have multiple methods say find all find by update delete these are the general methods which we have the thread operations which we can do in the database usually we use the repository to connect to a database here we are going to use it to connect to the redis cache so this is the interface which i have created now i need to implement this interface right so i'm just going to create a implementation of this particular interface and i'm going to implement all the methods obviously i have um loaded all these into my intellij already so i'm just going to call this as implementation so i've already written the code for all the um methods which we just saw here so let me remove all the unnecessary package my redis uh dependencies are not getting detected because i did not import so i just imported my maven um settings so this is just now fine let me minimize this and minimize this also and let's go in the full view more so we can understand this file better so if you see here this is the same example which i had used in my spring data radius video nothing fancy here i just created an implementation of the user repository but there are two things we are doing here one is called the hash operations using which we can cache our data into the redis cache which integrates it with the redis template so reduce template is the alternative for our rest template or the queue messaging template which we saw earlier like few minutes back right so redis template is what is going to insert the data and i'm injecting that reduce template into the user repository implementation and that's what we are doing here as a part of save i'm just putting the data into the hash operations using the cache name so the cache name here denotes user cache which is what we created uh here this is the one so it's going to enter data into the user cache with the id so id is going to be my unique key because in the user i am just using id as my unique value so it could be the employee id right so that's how i'm putting the data and pushing the whole user data into it so as a part of save method we'll be putting the data into the cache find all will retrieve all the method data from the cache find by id using which we can retrieve a specific id update will save the same right i mean obviously it's going to call the same it's going to update the same delete is going to remove the cache for that particular id and that's what we have done here so this is what is the core for integrating with the redis instance so done right i mean let's go back to the receiver so in the receiver i'm just going to auto wire this one once i have water wired i'm just going to leverage it here so i'll just say user repository dot save because i'm going to save this message right so i'll just say save the message in cash that's it so i've just saved the message whatever we received into the cache using the user repository and i'm just having a message that's it so we can purchase the message directly however in order to configure this whole thing right i mean the whole redis package integration we have to create some configurations so i'm going to create some spring auto configuration by default spring can do it but i'm going to plug in my elastic instance right i'm going to plug in my redis uh instance so i'm just going to add some configurations but you can try without the configuration i did not try without the configuration but you can definitely try without it as well so i'll just call it as redisconfig so i've already loaded that into my um template that's why it got auto generated and i'm just importing the right class that's it here so what i have done here is i've added an annotation for configuration which will be the auto configuration for spring boot i've enabled the elastic cache with the cluster configuration for the cache name as user cache i've also got the values for the redis host and the port number which we have here all these will be injected as values here those two will be used to create a reduced standalone configuration using jedis factory right this factory will now be injected into the redis template right and that's what i'm doing here and the same reduce template will be injected into the and the same factory will be injected into my cache manager if i want to retrieve it via cache manager i'm not going to use the cache manager directly here but i just added as a configuration you can just in fact ignore the whole file and then use the property to load it but i i don't know for some reason it did not work for me initially so i did not try that but do let me know without the configuration if it works or not i don't want to just do it without the configuration and then fail in the video so i'm just adding it as a configuration by default but spring does that all these under the hood anyway so let's come back to the receiver part so that's it right we have saved the message now we need to figure out a way to show this to the user right so that we can retrieve the message right this is the one so we need to create a new endpoint called get using which we can retrieve uh the data from the cache so let's go to the here we have post mappings right let's add some get so let me add a get mapping i'm going to call this as uh i need to send the user message by id so i'll add it as a path parameter so the part parameter will be called id so when you pass the id you can get the message so i'll just say integer id so when you pass the id i'm going to query the repository by the id so i don't want to query the repository directly so i'm going to create a service i'm going to create a user or redis service in fact even in the previous service i should have directly done it but since i can directly call a repository from a service i didn't do it but let's create a service so that's it so in the reddit service i'm going to call get by id i'll pass the id and i need a user to be returned so i'm going to create a method by default it's going to add all these things here i'm going to inject my user repository so using the user repository dot find by id i'm going to query the id and i'm just going to return it that's it so we have done the method for get by id if somebody wants by all we can do that as well let's do all also right i mean let's go to here i want to hear anything we need to return it right so let's return this i want to say all so if somebody says all user slash all we should return everything so let's use the redis service for it now again let me create the method and this should now return user repository.find so this should now return everything so the find all returns a different okay type map so let's return this back so let's do the same that's it so i'm doing a get and get all uh that's it right i mean we can directly use the disk service uh in fact let's go to the sqs receiver and instead of auto wiring the repository here let's do the other way around reduce service i'm doing the same thing let's move this to the done now we can get rid of the repository and we can move everything to the server so that's it i moved all the repository injection to this particular user in uh repository service right which is based on the credits so i'm having a redis service using which i'm interacting with the repository at least it looks decent right now uh that's it so what else do i need i have added okay no we need the host name right in order to connect to the cache so the cache got created uh the status of the cache is available so let's go to the user cache and there is this end point here this is the end point which we need to connect to and if i go to the cache you can see two different replicas if you remember we created a replica count as two right and these are the two different replicas so the data will be like present in the one and then it will be replicated to two and three right um so there are three instances running right now so let's go here paste the hostname we don't need the port done so this is the host with which we are going to connect to from the ec2 instance so let's package this application obviously uh we need to have the role uh with the necessary permissions if you remember we created the app user role and that app user role has elastic cache full access right also if you remember i was mentioning about the security group right the security group which [Music] the elastic cache uses right let's go to the ec2 instance security group and if you go to the elastic cache we had a specific if you go to settings i think we should be able to see you know in the description let's check yep in the description there is this security group called sgc 04 right let's go to the same if you see this particular security group i have configured it to have connections to be accessible from another security group this security group is nothing but the ec2 instance security group so ec2 instance would have got created with a security group and from our ec2 instance we need to connect to our elastic cache right and how do we do it uh obviously you can configure the security group to have access to right that's the um that's another way of accessing or giving authentication to the elastic cache instance so i've done that so that is why we should be able to access it right i mean initially you will have to configure it um i did it as a part of my previous demo so i am not repeating it again or will just be too much so i'm just making a note so you can just do it yourself uh that's it so my package is done all my configurations are done i think what else is pending that's it i guess so let's go to the terminal and push the artifact let me push the artifact to s3 bucket uh meanwhile let me go to the terminal window i think my ssh got timed out so let's reconnect to both the terminals from my local let me go to user slash app so if the upload is completed yes now if you see the jar size is like huge 33.9 mb right that's because we added this as a part of our new package right so let's go here and see if our application is still running the application went down it is a good thing now i need to copy my uh s3 content into here right let's do here yeah so i'm copying from the s3 bucket so the command is again aws s3 copy into the current directory so the file got pushed here i'm just i think i can do it but let me do a siege mod seven double five and spring that's it so i'm gonna run this uh application so now the changes are with elastic cache as well so this should now connect to our elastic cache instance uh also i'll show you by stopping the instance restarting so that we know the data is coming from the cache and not within the application we are not faking it right so let's do that from here i'm going to do a curl right let's do the curl so i'm going to hit the same endpoint we had which is the post endpoint using which i'm going to create a user called ryan with the ids 1 salary is 12000 right let's hit that so the message is sent to the excuse topic it should get received in uh five seconds i think it should be now yep uh we got the message uh looks like there is a socket timeout if you notice that right uh i think this could be because my ec2 instance doesn't have the connection to the security group i think now i get it let's go to the ec2 console let's look at what is a security group i think maybe my ec2 instance had a different security group that is why we are not able to connect two so let's look at which is my instance okay this is launch time today this is the ec2 instance let's look at the security group of my instance security okay so the instant security group is different launch is the two i think this is the different security group compared to the access which we have given here let's go to the settings sorry description i think this is a separate group right let's open another instance let me go to security group okay there are two security groups now right i mean that's the ring the launch wizard two is my ec2 instance security group so i need to add my ec2 instance into this like i was mentioning earlier i should have added it but uh yeah let's let's add it i'm just saying uh get all the traffic for now and i need to install so i need to give this launch user two here that's it so this should add my ec2 instance to have access to the elastic cache instance let's uh restart the application or let's retry pushing the message i don't know if that will work i've never tried it i heard security should work i think now it's worked right it says saved message in the cache now how do i verify it uh we need to do a curl uh that's when we know it's going to be stored right did i add any message okay i think i do not add any log statement but i think we can uh check using id equal to one hyphen x get so let's do that uh okay the path user slash id equal to one doesn't work all right i mean what does it say we got a null pointer because did we inject did we not inject let's go to the controller oh looks like i did not auto wire my service that's the problem okay so that's a good thing now we are going to redeploy the application and i'm not going to create the user again but let's see if the user is retrieved from the cache or not because if i reset my application obviously if the data was within the application it got deleted now at least we know that it's going to be coming from the cache so it was just a blind mistake i did not add a lot of wired here so let's quickly deploy the application so i've restarted my application after downloading the new package from the s3 bucket hopefully this should now solve the issue which we got it was an all pointer exception a classic null pointer so let's do a get now uh at least we should get yeah i think we got the message see this right uh we got the ryan merced let me do a clear so see that we got the ryan id right let me post one more message right just to have a clear idea let's let's update the salary of ryan to 10 000 right i'll query uh still it is showing 12 000 still it is 2012 now you see that it got updated to 10 000. so my cache is getting updated right after it is pushed now let me add one more user right just to have some more users so we can do a find all and then we can get two different uh users right i'll just to create two i'll just say chris so chris has one lakhs okay so message got sent it should get received in the other queue yep and it got saved in the cache now let's do a all right i mean if you look at it i had uh user slash all in point which should return all the yup i can see that for the id2 this is my data for id1 this is my data so looks like my cache works fine uh let me try stopping this application and then restarting right that way we know the cache works right let's uh stop this particular application hopefully this got killed if not i will forcefully kill it okay it did not get killed by default so i've killed the application and let me restart the application just to make sure uh let's hit the end point again just to show it failed connection refused connection refuse application is still not up i'm just waiting now you can see that the data is coming from the cache so that's how we know it connects to the elastic cache instance and then it's retrieving the data so i hope you guys understood how we did it as a part of spring cloud with the spring cloud aws dependencies right i'll just summarize what we did uh initially we saw the architecture diagram we created a ec2 instance we created a spring boot application with spring cloud aws as the dependency we created an endpoint called user controller as a part of it we created a user message using the post mapping we created a sqs sender using which we were sending the message we created a queue messaging template using which we converted and send the message to the queue we also created a receiver endpoint receiver service using which we annotated the sqs listener and then we retrieved it from the same queue and then we logged it later on we integrated that with the redis repository which we implemented here using the redis template and hash operations here we created multiple methods so that we could query the elastic cache integrated that with the receiver to save the message and from the user controller we exposed multiple endpoints so that we could retrieve the data we created also a redis service using which we connected to the repository we also added some configuration details so that we could create the uh factories and the beans which spring boot can use to connect to the redis instance we added those properties in the application.properties as always this particular code will be available in the github for you for reference i will remove the access keys i will disable them once the video goes live i hope this particular video was helpful as always if you like the video go ahead and like it if you haven't subscribed to the channel go ahead and subscribe to it meet you again in the next video thank you very much
Info
Channel: Tech Primers
Views: 4,613
Rating: 4.9682541 out of 5
Keywords: techprimers, tech primers, spring cloud aws, spring aws, spring boot with aws, spring coud aws with sqs, amazon sqs with spring, elasticache with spring, redis with spring, redis with aws, spring cloud aws redis, spring cloud aws sqs, amazon sqs with spring cloud example, spring cloud aws for beginners, spring cloud aws explained, spring cloud handson, aws community builder, spring cloud redis, spring elasticache for redis, redis from spring
Id: H3--8038so8
Channel Id: undefined
Length: 52min 38sec (3158 seconds)
Published: Thu Feb 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.