Loading repository data…
Loading repository data…
seanmayer / repository
2022 - Java Spring Boot REST API, JPA, Native SQL Queries, MySQL, Apache Tomcat, AWS EC2, AWS Beanstalk, JWT, STS
Quick refresher on Java Spring Framework with REST Webservices.
Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.
xcode-select --install -> brew install maven./mvnw spring-boot:run or VSCode UImvn install command.jar file to a production serverjava -jar command/mobile-app-ws directroy folder./mvnw install this will compile, build the project and unit tests./mvnw spring-boot:run this will run the RESTful webservices application in a apache tomcat server containerRun ./mvnw install` this will compile, build the project and unit tests (create jar in target)cd /mobile-app-ws/targetcp mobile-app-ws-0.0.1-SNAPSHOT.jar /Users/{user-name}/Desktopjava -jar mobile-app-ws-0.0.1-SNAPSHOT.jarpom.xml -><packaging>war</packaging>
./mvnw clean clears out target folder./mvnw install creates the war package in /targetcd /Desktop/apache-tomcat-9.0.65/bin/chmod a+x *.sh./startup.sh./shutdown.shcd /Desktop/apach-tomcat-9.0.65/conftomcat-users.xml<role rolename="manager-gui"/>
<user username="admin" password="admin" roles="manager-gui"/>
/mobile-app-ws/target/mobile-app-ws-0.0.1-SNAPSHOT.war -> mobile-app-ws.warsudo su (if required)chmod 400 myprivatekey.cer (if required)ssh -i myprivatekey.cer ec2-user@{DNS-name}sudo yum updatesudo java -versionsudo yum list javasudo yum install java-1.8.0sudo /usr/sbin/alternatives --config javasudo /usr/sbin/alternatives --config javactar.gz file typesudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gzlssudo tar xvf apache-tomcat-9.0.65.tar.gz -C /usr/sharels -lrt /usr/sharesudo ln -s /usr/share/apache-tomcat-9.0.65 /usr/share/tomcat9ls -lrt /usr/share is now: tomcat9 -> /usr/share/apache-tomcat-9.0.65 :)sudo groupadd --system tomcatsudo useradd -d /usr/share/tomcat9 -r -s /bin/false -g tomcat tomcatsudo chown -R tomcat:tomcat /usr/share/apache-tomcat-9.0.65 (set tomcat folder permissions for this new user)sudo vi /etc/systemd/system/tomcat9.service[Unit]
Description=Tomcat Server
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment='JAVA_OPTS=-Djava.awt.headless=true'
Environment=CATALINA_HOME=/usr/share/tomcat9
Environment=CATALINA_BASE=/usr/share/tomcat9
Environment=CATALINA_PID=/usr/share/tomcat9/temp/tomcat.pid
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M'
ExecStart=/usr/share/tomcat9/bin/catalina.sh start
ExecStop=/usr/share/tomcat9/bin/catalina.sh stop
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reloadsudo systemctl enable tomcat9sudo systemctl start tomcat9 (to stop sudo systemctl stop tomcat9)sudo vi /usr/share/tomcat9/webapps/manager/META-INF/context.xml <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
:wq to save changessudo vi /usr/share/tomcat9/conf/tomcat-users.xml<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
:wq to save changessudo systemctl restart tomcat9sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpmsudo amazon-linux-extras install epel -ysudo yum install mysql-community-server/etc/yum.repos.d/mysql-community.repo and disable gpg if you trust reposgpgcheck=0
sudo systemctl enable --now mysqldsystemctl status mysqldsudo grep 'temporary password' /var/log/mysqld.logsudo mysql_secure_installationmysql -u root -pmysql -u root -pcreate database photo_app;show databases;create user '{my username here}'@'localhost' identified by '{my password here}';grant all privileges on photo_app.* to '{my username here}'@'localhost';flush privileges;exitmysql -u {my username here} -p;show databases;cd mobile-app-ws./mvnw installcd mobile-app-ws/target/mobile-app-ws-0.0.1-SNAPSHOT.war.war file to "War file to deployOne of the fastest and simplest ways of deploying spring boot applications. You simply upload your application and your application will automatically handle your capacity provisioning, load balancing, auto-scaling and application health monitoring.
So you could use your application in a production environment within just a few minutes, without any infrastrucutre or resource configuration work yourself.
Elastic beanstalk is:
As depicted below, you will get an EC2 instance started and configured auotmatically, additionally to that you wll get an autoscaling, so if need your EC2 instance, this can scale up and handle more traffic.
To load balance the incoming traffic, Elastic Load Balancer is created and configured for you automatically, the incoming HTTP traffic will be equally balanced between the running EC2 instances.
Amazon RDS can be used here to, Amazon RDS is a relational database and this supports many different types of relational databases, for this demo I will be using MySQL again. Also by using Amazon RDS, you will also get autoscaling included here too, you do not need to aquire specialist or develop additional skills here, Amazon RDS will do it for you! :)
So as our spring boot application scales up and down it will work with MySQL server that will use Amazon RDS service. By deploying your application with Beanstalk and running your database in Amazon RDS, you access capabilities of production ready environment in just a few minutes! No need to manually configure infrastructure, and no need for installing and maintaining database software, you get this all provided by Amazon.
photoappuserapiphotoappusers_dbapplicaiton.properties in the main maven project - update appropriately:server.error.whitelabel.enabled=false
spring.datasource.username={username}
spring.datasource.password={password}
spring.datasource.url=jdbc:mysql://{rds-endpoint-address.amazonaws.com}:3306/{database name configuration}
spring.jpa.hibernate.ddl-auto=update
tokenSecret=tokenSecret
server.error.include-stacktrace=never
server.servlet.context-path=/mobile-app-ws
./mvnw package (Take the compiled code and package it in its distributable format, such as a JAR or WAR)PhotoAppUsersApiTomcatmobile-app-ws-0.0.1-SNAPSHOT.warhttp://{{aws-beanstalk-instance-name}}/usersHeaders:
application/jsonapplication/jsonBody:
{
"firstName":"Joe",
"lastName":"Bloggs",
"email":"joe@blogg