Deploying the backend

If you are going to use your own server, the easiest way to deploy the Spring Boot application is to use an executable JAR file. If you use Maven, an executable JAR file is generated by typing the mvn clean install command in the command line. That command creates the JAR file in the build folder. In this case, you don't have to install a separate application server, because it is embedded in your JAR file. Then, you just have to run the JAR file using the java command, java -jar your_appfile.jar. The embedded Tomcat version can be defined in the pom.xml file, with the following lines:

<properties>
<tomcat.version>8.0.52</tomcat.version>
</properties>

If you are using a separate application server, you have to create a WAR package. This is slightly more complicated, and you have to make some modifications to your application. The following are the steps that have to be observed in order to create the WAR file:

  1. Modify an application's main class by extending SpringBootServletIntializer and overriding the configure method:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure
(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
  1. Change the packaging from JAR to WAR in the pom.xml file:
<packaging>war</packaging>
  1. Add the following dependency to the pom.xml file. Then, the Tomcat application is no longer embedded:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

Now, when you build your application, the WAR file is generated. It can be deployed to the existing Tomcat by copying the file to Tomcat's /webapps folder.

Nowadays, cloud servers are the principal means of providing your application to end users. Next, we are going to deploy our backend to the Heroku cloud server (https://www.heroku.com/). Heroku offers a free account that you can use to deploy your own applications. With the free account, your applications go to sleep after 30 minutes of inactivity, and it takes a little bit more time to restart the application. However, the free account is sufficient for testing and hobby purposes.

For deployment, you can use Heroku's web-based user interface. The following steps will take you through the deployment process:

  1. After you have created an account with Heroku, log in to the Heroku website. Navigate to the dashboard that shows a list of your applications. There is a button called New that opens a menu. Select Create new app from the menu:
  1. Name your app, select a region, and press the Create app button:
  1. Select a deployment method. There are several options; we are using the GitHub option. In this method, you first have to push your application to GitHub, and then link your GitHub repository to Heroku:
  1. Search for a repository you want to deploy to, and then press the Connect button:
  1. Choose between an automatic and manual deployment. The automatic option deploys your app automatically when you push a new version to connect to the GitHub repository. You also have to select a branch you want to deploy. We will use the manual option that deploys the app when you press the Deploy branch button:
  1. Deployment starts, and you can see a build log. You should see a message that says Your app was successfully deployed:

Now, your application is deployed to the Heroku cloud server. If you are using the H2 in-memory database, this will be enough, and your application should work. We are using MariaDB. Therefore, we have to install the database.

In Heroku, we can use JawsDB, which is available in Heroku as an add-on. JawsDB is a Database as a Service (DBaaS) provider that offers the MariaDB database, which can be used in Heroku. The following steps describe how to start using the database:

  1. Open a Resources tab in your Heroku app page and type JawsDB into the Add-ons search field:
  1. Select JawsDB Maria from the drop-down list. Click on JawsDB, and you will see the connection information of your database:
  1. Change the database connection definition in the application.properties file with the values from the JawsDB Connection Info page. In this example, we use a plain password, but it is recommended that you encrypt a password using, for example, the Java Simplified Encryption (JASYPT) library:
spring.datasource.url=jdbc:mariadb://n7qmaptgs6baip9z.chr7pe7iynqr.eu-west-1.rds.amazonaws.com:3306/ebp6gq2544v5gcpc
spring.datasource.username=bdcpogfxxxxxxx
spring.datasource.password=ke68n28xxxxxxx
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
  1. With the free account, we can have a maximum of 10 concurrent connections to our database; therefore, we also have to add the following line to the application.properties file:
spring.datasource.max-active=10
  1. Push your changes to GitHub and deploy your app in Heroku. Now, your application is ready, and you can test that with Postman. The URL of the app is https://carbackend.herokuapp.com/, but you can also use your own domain. If we send the POST request to the /login endpoint with the credential, we can get the token in the response header. So, everything seems to work properly:

We can also connect to the JawsDB database with HeidiSQL, and we can see that our car database has been created:

We can watch application logs by selecting View logs from the More menu:

The application log view looks like the following:

Now, we are ready to deploy our frontend.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.6.77