Schlagwortarchiv für: Application


In this article, we will learn to create a basic Hello World spring boot application.

To create a spring boot application, there are several tools such as:

  • Spring Initializer
  • Spring Tools Suits
  • Oscurecimiento
  • IntelliJ IDEA

All these tools are used by the developers to create the spring applications.

Since the Spring Initializer is a mostly used tool, we will start with this first and then we will discuss the other tools in our next articles.

We will use the Oscurecimiento for the development/coding. Oscurecimiento is an IDE that is primarily used to create the java-based applications.

If you are not sabido with Oscurecimiento, don’t worry because it’s a simple code editor. We will help you understand it in this article. To get it, visit the official site https://www.eclipse.org/downloads/ and install it to your tópico system.

Prerequisites:

Before starting further, make sure that you have the following tools installed in your tópico system:

  • Java must be installed in your system. Java 11 LTS or Java 17 LTS is recommended.
  • Oscurecimiento must be installed before starting as this is the main IDE/Editor.

Now, let’s get started in this step-by-step learning.

Step1: Open the Spring Initializer Tool

To open this tool, visit the https://start.spring.io/ URL. This is the official web-based tool provided by Spring itself to create the spring boot application.

At first glance, it looks like the following image:

It asks for a couple of options before creating the project. For example:

The first is project type which refers to what type of project you want to create. It gives two choices: either Maven or Gradle, which are basically the project dependency management tools.

We selected Maven und Java as the main programming language. After that, we selected the spring boot version. It shows all the available versions. We selected the version 2.7.1 which is one of the stable versions to start with.

Project Metadata comprises several steps:

Group: It is a name that should be unique and is used to group all the files. It follows the reverse domain name and the java package name concept.

Artifact: It is a sub-package name under the group name and used as a part of name of the JAR file while building the project.

Name: It is the name of the application that you wish to have.

Description: It is a short description of your project.

Packaging: It specifies the type of package for the application while building. JAR is used for creating libraries and plugins while WAR is used for building the Web Applications.

In our case, we selected JAR as we are not creating a web application.

Java Version: It specifies the Java version that will be used for creating the application. In our case, we used the Java 11.

Dependency: This specifies the dependencies that we want to include in our application such as Web, JDBC, Thymeleaf, etc.

Since we are just creating a simple application, it does not require any dependency for now. We will learn this more in our next articles while creating more functional applications.

Now, click on the GENERATE button to download this project.

Step 2: Extract the Application

After downloading the project, extract it first since the project is a zip file.

Step 3: Import Project to the Oscurecimiento

After extracting the zip project, open the desvanecimiento IDE and import this project. For this, just click on the “File” menu and select the import option like the following:

File -> Import -> Existing Maven Project -> Next -> Browse -> Select the project -> Finish

This opens a sub-window like in the following image. Choose the existing Maven project and click next.

Locate the location in the application folder and select it.

If all is good, it should look like the following image. Click on the finish button.

After importing, it takes a couple of minutes to set up the project and download all the project dependencies. If your computer network speed is good, it should load faster. Otherwise, have patience and wait for a while.

Step 4: Project the Explorer

After downloading all the dependencies, set up the project. It looks like the following:

This project contains some default files such as:

  • SpringbootappApplication.java
  • SpringbootappApplicationTests.java
  • Application.properties
  • Pom.xml

Let’s understand the uses of these files one by one.

// SpringbootApplication.java

This is the main application file that contains the main() method. This method calls the run() method to run the application.

This class uses the @SpringBootApplication which specifies this application as a spring boot application. This annotation is a combination of @EnableAutoConfiguration, @Configuration, and @ComponentScan annotations. We will learn more about these annotations in our next detailed articles.

package com.linuxhint.springbootapp;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringbootappApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootappApplication.class, args);

}

}

// pom.xml

This is another important file that is basically an XML file containing the entries of all the project dependencies and configurations.

Since we did not add any dependency while creating the project, it contains the default configurations and dependencies.

<?xml version=«1.0» encoding=«UTF-8»?>

<project xmlns=«http://maven.apache.org/POM/4.0.0» xmlns:xsi=«http://www.w3.org/2001/XMLSchema-instance»

xsi:schemaLocation=«http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd»>

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>springbootstarterparent</artifactId>

<version>2.7.1</version>

<relativePath/> <!– lookup parent from repository –>

</parent>

<groupId>com.linuxhint</groupId>

<artifactId>springbootapp</artifactId>

<version>0.0.1SNAPSHOT</version>

<name>springbootapp</name>

<description>this is a hello world application </description>

<properties>

<java.version>11</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>springbootstarter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>springbootstartertest</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>springbootmavenplugin</artifactId>

</plugin>

</plugins>

</build>

</project>

// application.poperties

This file is used to set the total properties for the application such as database connection, server port number, view files path, etc. Initially, this file is empty.

// SpringbootappApplicationTests.java

This file is used to create the test cases for testing the application by using the Junit library. Initially, it does not have any test case.

package com.linuxhint.springbootapp;

import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

class SpringbootappApplicationTests {

@Test

void contextLoads() {

}

}

Step 5: Run the Application

Now, let’s run the application by selecting the main class (SpringbootappApplication.java). Right click, choose “run as” and then click the java application like this:

Right-click -> Run as -> Java application

It starts executing the code and you can see the output in the console.

You can see the status of the running application in the console tab which means that the application is started.

We have successfully created and run our first spring boot application.

Step 6: Display the Hello World Message

By default, the spring boot application does not display any message to the console.

If you wish to display a Hello World message to the console, simply update the main class by adding this print statement:

System.out.println(«Hello World!»);

The new updated file looks like this:

package com.linuxhint.springbootapp;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringbootappApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootappApplication.class, args);

System.out.println(«Hello World!»);

}

}

Run the application again and see the output console.

This time, it shows the Hello World message to the console.

Conclusion

This article comprises the steps for creating a simple spring boot application. We used the Spring Initializer tool and Oscurecimiento for editing and running the application. Spring boot uses its own in-built server to run the application, although we can set up an external server as well. In the following articles, we will learn to add more dependencies to the project to create a more functional and web-based applications.



Source link


“A load balancer receives requests and then transfers them to targets defined in a target group. We can create an Application Load balancer either using the AWS management console or AWS CLI. There are several routing options with AWS Application Load Balancer, e.g., Host-Based routing.

In Host-based routing, incoming traffic is routed on the basis of the domain name or host name given in the Host Header. In this tutorial, we are going to create an Application Load balancer with Host-Based routing.”

Overview of This Lab

In this lab, we will set up Application Load Balancer on AWS using the host-based routing approach. To perform this lab, we require the following components: First, we need two availability zones with a minimum of one EC2 instance in each of them. Next is a VPC with at least one public subnet in each of these availability zones. We will configure the load balancer using these public subnets. Finally, configure a web server for the above EC2 instances and use the security group to open the port 80 for http requests on these instances.

Configuring the EC2 Instances

The first EC2 instance is an Ubuntu 20.04 Linux machine, and the web server (apache) displays the message: “This is an xyz host”. It has a hostname: www.xyz.tecofers.com

Similarly, the second instance is also an Ubuntu 20.04 Linux, with the web server displaying the message: “This is an abc host”. It has a hostname: www.abc.tecofers.com

The web servers contain the “abc” and “xyz” directories inside their root directories, i.e. (/var/www/).

Setting up the Target Group

Step 1. In this step, we will create target groups(“xyz” and “abc”) for routing the incoming requests. On the EC2 dashboard, select the Target Groups option on the left pane under the Load Balancing title. Now select the “Create target group” option:


Step 2. The “Specify group details” page has several options to be filled.

i) We start with the “Basic configuration” heading:

  1. For the “Choose a target type” option, click the “Instances” radiodifusión button here.

  1. For the “Target group name”, enter an appropriate target group name(“xyz” in my case).
  1. For the “Protocol” and “Port” options, select “HTTP” and “80”, respectively.
  1. For the “VPC” option, choose the VPC containing your instances.
  1. Finally, for the “Protocol version,” stick to the default option(HTTP1).

ii) The “Health checks” Configuration has the following fields:

  1. For the “Health check protocol” drop-down menu, choose HTTP.
  1. For the “Health check path”, specify a custom path or use the default one(recommended).
  2. We recommend keeping the “Advanced health check settings” as it is. Adding tags is an optional step. Enter “Next” to continue.

Register the Instances

In this step, we need to register the instances with the Target Groups.

Step 1. On this page, choose an instance and then select the button “Include as pending below”. This will make the instance appear in the “Review targets” heading. Proceed by hitting the “Create target group” button.

For another target group, “abc”, repeat the same steps of creating a target group and then attach another instance here. On the Target groups, the two target groups will appear as:


So far, we have not attached any Load balancer to our target groups. Let’s go and create a load balancer.

Create the Application Load Balancer

Note: Creating a load balancer service requires you to pay AWS.

Step 1. On the EC2 dashboard, select the Load Balancers title and hit the “Create Load Balancer” button, and then choose “Application Load Balancer”:


Step 2. Enter a name for the load balancer that satisfies the naming constraints. Under the Scheme, keep the default option (“Internet-facing”) selected. For “IP address type,” choose IPv4.


Step 3. On moving ahead, we have “Network mapping”; here, choose the VPC we are working with. Also, choose two availability zones that hold the targets. The incoming traffic will be routed to these targets by the load balancer.


Step 4. For the Security Groups, select or create a new security group for the load balancer that will allow communication with the target port (port 80)


Step 5. Now come the Listeners and routing segment; what we have to do here is to configure a listener by choosing a protocol(HTTP here) and a port. For the Default action, choose your target corresponding to the “forward to” field.


Step 6. Adding tags and Add-on services are optional steps and can be skipped. After reviewing the configuration summary and click the “Create load balancer” button:


The load balancer is now ready.

Add the Host-based Forwarding Rules

After the load balancer is created and its status becomes active, we are required to add traffic forward rules.

Step 1. From the “Load Balancers” page and select the load balancer and then further move to the Listeners tab:


Step 2. Navigate to the Listeners tab and under the “Rules” column, click on the “View/Edit rules” link. A new page appears here first; click on the “+” icon, then click on the “Insert Rule” link.


Step 3. For the IF(all match) column, do as follows:

i) click on the “+ Add condition”  and set “Host header” as the type of Rule.

ii) enter the host or domain name inside the field corresponding to the label “is”.

Step 4. For the “Then” column, do as follows:

I) click on “+Add action” and set “Forward to” as the action.

ii) choose the target group.

Save the rule and for another target group, insert the rule in the same way and save it.


In case the above rules are not satisfied, the default rule will be used.

Register the Domain

To make the host-based routing work over the internet, we have to register the hostnames with Route 53 and map them with the load balancer DNS.

Step 1. From the Route 53 dashboard, choose the “Create hosted zone” option:


Step 2. Next, enter your domain name to where traffic will be routed to. For the “type” field, set “Public hosted zone”. Hit the button “Create hosted zone”.


Step 3. In the “Records” section, choose “Create record”.


Step 4. On  the “Choose routing policy’ page, select the link “Switch to wizard” and then choose the “Simple Routing” box and click “Next”:

Step 5. Now choose “Define simple record” and then enter the record details:

Domain: domain or subdomain name to route the traffic to.

Record type: DNS type of the record.

Value/Route traffic to:

  1. Choose “Apelativo to Application and Classic Load Balancer”
  2. Choose the region where the load balancer is deployed
  3. Choose the load balancer address and hit “Define simple record”.

On the next page, click on the “Create records” button.

Do the same steps for another domain name and verify the above record on the “Configure Records” page.

Test the Setup

To verify the above procedure, on a web browser, enter the DNS name of the load balancer along with the hostnames:

Conclusion

We have finally made the way to host-based routing on the AWS application load balancer. Try to do this lab yourself and also try to configure the Application Load balancer with path-based routing.

References: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancer-getting-started.html



Source link