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
Log in to the AHAS console.
In the upper-left corner of the AHAS console, select the region where you want to connect your application.
In the left-side navigation pane, choose .
On the Application Protection page, click Add Application in the upper-left corner.
On the Java Language tab, click SDK, and then click Connect Spring Boot Application.
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.zwith this version number.
Method 2: Add the JAR dependency.
Extract the dependency package and place all JAR files from the package in your classpath.
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
SentinelMyBatisMapperInterceptorinterceptor 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>
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>NoteReplace 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-publicNoteReplace 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>NoteReplace <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.

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

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:
Configure circuit breaking rules
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.