You can integrate your application with AHAS Application Protection using custom Java SDK instrumentation, which allows you to protect any code block. This topic shows you how to perform this integration.
Procedure
Log on to the AHAS console.
In the upper-left corner of the AHAS console, select your application's region.
In the left-side navigation pane, select .
On the Application Protection page, click New application in the upper-right corner. On the JAVA tab, click SDK, and then click Custom.
Add the Application Protection dependency to your application using one of the following methods:
Add the following dependency to your application's pom.xml file:
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>ahas-sentinel-client</artifactId> <!-- You can specify a version. For the latest version, see the New application page for Application Protection in the AHAS console. --> <version>x.y.z</version> </dependency>On the Custom page, find the latest version number in the Step 1: Add the Pom dependency section and replace
x.y.zwith it.
Add the dependency by using JAR files.
On the Custom page, click Download to download the compressed package, and place all JAR files from the package into the classpath directory.
To add instrumentation, wrap your business logic in the following code:
Entry entry = null; try { // "HelloWorld" is the resource name. This name is used for monitoring and is displayed in the call chain in the console. // Rules are configured for specific resource names. // EntryType.IN identifies the resource as an inbound resource. System rules apply only to inbound resources. entry = SphU.entry("HelloWorld", EntryType.IN); // Protected business logic. System.out.println("Hello Sentinel!"); } catch (BlockException e) { // This block is executed when a throttling or degradation event is triggered. Implement your handling logic here, such as a fallback or logging. } finally { // Ensure that the exit() method is called here and is paired with SphU.entry(). if (entry != null) { entry.exit(); } }NoteUse
EntryTypeto distinguish between inbound and outbound traffic.EntryType.INrepresents inbound traffic, andEntryType.OUTrepresents outbound traffic. System rules apply only to inbound resources.The total application traffic shown in the console counts only
EntryType.INtraffic.For more information about the SphU API, see Common classes and their methods.
Resource names for custom instrumentation cannot exceed 1,024 characters.
Configure the startup parameters for your application.
Type
Parameter example
private network
-Dproject.name=AppName -Dahas.namespace=default -Dahas.regionId=<RegionId>NoteReplace AppName with your application name.
Replace <RegionId> with the ID of the region where your application is located. For a list of regions and their corresponding RegionIds, see the Region and RegionId Mapping Table.
public network
-Dproject.name=AppName -Dahas.license=<license> -Dahas.namespace=default -Dahas.regionId=cn-publicNoteReplace AppName with a custom application name and <license> with your actual license key.
A license is required for applications in a public network region but not for those in private network regions. You can find the license key in the Step 3: Configure startup parameters section. For more information, see View a license.

Restart your application.
Optional: Use annotations to configure the handling logic for when your application triggers throttling, degradation, or system protection rules. For more information, see Annotation-based configuration.
NoteBy default, the application throws a subclass of
BlockExceptionwhen a rule triggers. AFlowExceptionis thrown when a flow control rule triggers, and aDegradeExceptionis thrown when a degradation rule triggers.
Verify the results
Log on to the AHAS console. In the left-side navigation pane, select . The integration is successful if a resource card for your application appears on the Application Protection page and reports data.

Region and Region ID mapping
Region | Region ID |
Public | 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 |