Quick Start

更新时间:
复制 MD 格式

This topic describes how to use the SOFABoot framework to create a web project, run it locally or in the cloud, and add middleware services.

The main steps are as follows:

Note

A Core project is similar to a Web project. For more information, see Create a project.

Create a web project

Prerequisites

Before you begin, set up the SOFABoot runtime environment. For more information, see Set up the environment.

Procedure

Creating a SOFABoot web project involves two main steps:

  1. Create a local project.

  2. Configure key properties.

Follow these steps:

Create a local project

Important

These instructions apply to projects created with SOFABoot 3.3.0 or later. To upgrade, see SOFABoot Upgrade Guide.

  1. Open the command line interface and run the following command:

    mvn archetype:generate -DarchetypeGroupId=com.alipay.sofa -DarchetypeArtifactId=sofaboot-web-archetype -DarchetypeVersion=1.0-SNAPSHOT -DarchetypeCatalog=internal
  2. When prompted, enter the following Maven coordinate information:

    • groupId: A unique identifier in the Maven repository. This is typically the reversed domain name of your company or organization. For this example, enter com.alipay.sofa.

    • artifactId: The name of the project or application. For this example, enter web-app.

    • version: The version number. The default is 1.0-SNAPSHOT. SNAPSHOT indicates that the project is an unstable, in-development version. For this example, keep the default value.

    • package: The application package name. By default, it is the same as the groupId.

      # Example
      Define value for property 'groupId': com.alipay.sofa
      Define value for property 'artifactId': web-app
      Define value for property 'version': 1.0-SNAPSHOT
      Define value for property 'package': com.alipay.sofa
  3. Confirm the Maven coordinate information.

    • If the information is correct, enter Y to confirm and continue creating the project.

    • To change the information, enter N to redefine the project properties.

      # Example
      Confirm properties configuration:
      groupId: com.alipay.sofa
      artifactId: web-app
      version: 1.0-SNAPSHOT
      package: com.alipay.sofa
      Y::
  4. After you confirm, the project is created, and the command line interface displays the following message. The project files are located in the ./web folder in the current path.

    [INFO] Project created from Archetype in dir: {current_dir}/web
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 28.298 s
    [INFO] Finished at: 2018-01-10T23:36:19+08:00
    [INFO] Final Memory: 15M/163M
    [INFO] ------------------------------------------------------------------------

  5. Go to the Version Guide to find the latest SOFABoot version. Then, open the main pom.xml file in the project's root directory and update the version number in the <parent> tag. The following image shows an example:

    版本号

Configure key properties

Before you deploy to the cloud, you must configure the application.properties file. For more information, see Add SOFA middleware. Follow these steps:

  1. Go to the SOFAStack console > R&D Efficiency > Scaffolding > Step 2. The following image shows an example:

    Obtain the following information:

    • Instance Identity: The unique identity of the application instance in the workspace. The corresponding key in the application is com.alipay.instanceid.

    • AntVIP: The application uses AntVIP to retrieve the server-side addresses of various components. A unique address is provided for each region. The corresponding key in the application is com.antcloud.antvip.endpoint. The AntVIP address values for different environments are listed below.

      • Hangzhou Gold zone: cn-hangzhou-fin-middleware-acvip-prod.cloud.alipaycs.net

      • Shanghai non-Gold zone: cn-shanghai-middleware-acvip-prod.cloud.alipaycs.net

      • Shanghai Gold zone: cn-shanghai-fin-sofastack-middleware-acvip-prod.cloud.alipaycs.net

      • Hangzhou non-Gold zone: cn-hangzhou-middleware-acvip-prod.cloud.alipaycs.net

  2. In the Scaffolding console, click the user icon in the upper-right corner and select AccessKey Management. On the page that appears, obtain the property values for Resource Access Management (RAM). The corresponding keys in the application are:

    • Access Key ID: com.antcloud.mw.access

    • Access Secret: com.antcloud.mw.secret

      Note

      To create an AccessKey, click Get AK. For more information, see Create an AccessKey.

  3. Determine the run mode and environment. The following code shows an example:

    run.mode=NORMAL
    com.alipay.env=shared
  4. Configure the keys and values for these properties in the application.properties file.

For more information about the SOFABoot framework configuration, project dependencies, and middleware services, see the following topics:

Import the web project into an IDE

A SOFABoot project is a standard Maven project. You can use a common Java integrated development environment (IDE), such as IntelliJ IDEA or Eclipse, for development. Follow these steps to import the SOFABoot project into your IDE:

IntelliJ IDEA

  1. Open IntelliJ IDEA.

  2. Import the project folder that you created.

    • If you are using the IDE for the first time or if you closed all projects, a welcome screen appears. On this screen, click Open, select the project folder that you created, and then click OK.

    • If IntelliJ IDEA automatically loads the last open project, click File > Open, select the project folder that you created, and then click OK.

  3. Configure Maven:

    1. Click File > Settings to open the Settings window. Navigate to the Maven configuration page in one of the following ways:

      • Click Build, Execution, Deployment > Build tools > Maven.

      • Search for the keyword Maven.

    2. Set Maven home directory to your local Maven installation directory.

    3. Set User Settings file to the path of your local Maven settings.xml file.

    4. Click OK to close the Settings window.

After the import is complete, you can view the project structure and content in the project folder.

Eclipse

  1. Open Eclipse.

  2. Configure Maven:

    1. Click Window > Preferences to open the Preferences window. Search for the keyword Maven to find the Maven configuration page.

    2. Under Maven, select Installations and configure the Maven directory.

      • If your local Maven installation directory is already in the Installations list, select its checkbox.

      • If the directory is not in the list, click Add. In the new window, set Installation home to your local Maven installation directory. Click Finish to return, and then select the checkbox for the new directory.

    3. Click Apply to save the configuration.

    4. Under Maven, select User Settings and set User Settings to the path of your local Maven settings.xml file.

    5. Click Apply and Close to save the configuration and close the Preferences window.

  3. Click File > Import > Maven > Existing Maven Projects > Next. Select the project folder that you created, and then click Finish.

Compile and run locally

Follow these steps:

  1. Start the local project in one of the following ways:

    • Start from the command line:

      • Use the cd command to navigate to the project's root directory.

      • Run the mvn clean install command.

    • Start from the IDE: Run the main function in the Web module.

  2. After the project starts successfully, open http://localhost:8080/index.html in a browser. You will see the following static page content:

     the static page in SOFABoot based on spring-boot
    Note

    A SOFABoot Core project does not have a user interface, so you cannot preview it locally. However, you can perform a health check by accessing http://localhost:8080/actuator/readiness. If the status is UP, the project is running correctly.

    For more ways to compile and run a SOFABoot project locally, see Run locally.

Run in the cloud

  • For an overview of the application publishing flow, see Technology Stack and Application Publishing Flow in the Technology Stack User Guide.

  • For detailed steps on how to publish an application, see the Quick Start for Classic Application Service.

Add middleware services

After you create a SOFABoot project, you can add starters to the main pom.xml file to use middleware services.

  • For a list of middleware service types and their starter version information, see Add SOFA middleware.

  • For sample demos of each middleware service, see the Quick Start for the corresponding product.