Connect a MyBatis application

更新时间:
复制 MD 格式

After you connect a MyBatis application to Application Protection, you can configure throttling, degradation, and system protection rules to ensure application stability. This topic describes how to connect a MyBatis application to Application Protection using the SDK.

Procedure

  1. Log in to the AHAS console.

  2. In the upper-left corner of the AHAS console, select the region where you want to connect your application.

  3. In the left-side navigation pane, choose Traffic Protection > Application Protection.

  4. On the Application Protection page, click Add Application in the upper-left corner.

  5. On the Java Language tab, click SDK, and then click Connect Spring Boot Application.

  6. Add the dependency to your application by using one of the following methods.

    • Method 1: Add the dependency to your pom.xml file.

      <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>spring-boot-starter-ahas-sentinel-client</artifactId>
        <!-- You can specify a version. For the latest version, see the application connection page in the AHAS console. -->
        <version>x.y.z</version>
      </dependency>

      On the Connect Spring Boot Application page, find the latest version number in the Step 1: Add the pom.xml dependency section. Replace x.y.z with this version number.

      pom版本号.png

    • Method 2: Add the JAR dependency.

      1. Download the ahas-sentinel dependency package.

      2. Extract the dependency package and place all JAR files from the package in your classpath.

  7. Add the interceptor.

    • If you use MyBatis Spring Boot Starter, the integration completes automatically after you add the AHAS dependency. This requires version 1.5.1 or later of spring-boot-starter-ahas-sentinel-client.

    • If you do not use MyBatis Spring Boot Starter, you must add the SentinelMyBatisMapperInterceptor interceptor to your MyBatis XML configuration file:

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration
              PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-config.dtd">
      <configuration>
          <plugins>
            <!-- Add the AHAS Sentinel interceptor -->
              <plugin interceptor="com.alibaba.csp.sentinel.adapter.mybatis.SentinelMyBatisMapperInterceptor"/>
          </plugins>
      </configuration>
  8. Configure the startup parameters for your application in one of the following ways.

    • Add JVM -D parameters.

      Type

      Example

      Internal network

      -Dproject.name=AppName -Dahas.namespace=default -Dahas.regionId=<RegionId>
      Note
      • Replace AppName with your custom application name.

      • Replace <RegionId> with the ID of the region where your application is deployed. For a list of region IDs, see the "Region and RegionId Mappings" section.

      Public network

      -Dproject.name=AppName -Dahas.license=<license> -Dahas.namespace=default -Dahas.regionId=cn-public
      Note

      Replace AppName with your custom application name and replace <license> with your license key.

    • In the application.properties file, add the following content:

      Type

      Example

      Internal network

      • # Specify the target AHAS environment.

        ahas.namespace=default
      • # Specify a custom name for your application.

        project.name=AppName
      • # Specify the region where the application is deployed.

        ahas.regionId=<RegionId>
        Note

        Replace <RegionId> with the ID of the region where your application is deployed. For a list of region IDs, see the "Region and RegionId Mappings" section.

      Public network

      • # Specify the target AHAS environment.

        ahas.namespace=default
      • # Specify a custom name for your application.

        project.name=AppName
      • # Configure the license information.

        ahas.license=<license>
      • ahas.regionId=cn-public

A license is required only for applications that connect over the public network. You can find the license on the Connect Spring Boot Application page. For more information, see View the license.

Spring boot license2.png

Verify the integration

Log in to the AHAS console. In the left-side navigation pane, choose Traffic Protection > Application Protection. If a resource card for your application appears on the Application Protection page and displays data, the integration is successful.

应用防护.png

Region and RegionId mappings

Region

RegionId

Public network

cn-public

China (Hangzhou)

cn-hangzhou

China (Shanghai)

cn-shanghai

China (Beijing)

cn-beijing

China (Zhangjiakou)

cn-zhangjiakou

China (Shenzhen)

cn-shenzhen

Singapore

ap-southeast-1

Germany (Frankfurt)

eu-central-1

Next steps

To configure throttling and degradation rules for your application, see the following topics:

When a configured throttling, degradation, or system protection rule is triggered in your MyBatis application, a MyBatisSentinelBlockException exception is thrown. You can catch this exception and handle it based on your business requirements.