Connect your Spring application to Application Protection by adding a dependency and using annotation-based checkpoints for your methods. This approach is minimally invasive to your code.
Procedure
Log on to the AHAS console.
In the upper-left corner of the AHAS console, select the region where your application is deployed.
In the left-side navigation pane, choose .
In the upper-right corner of the Application Protection page, click Add Application. On the Java tab, click SDK Integration, and then click Connect a Spring Application.
Add the dependency in one of the following ways:
Method 1: Add the dependency to the Pom file.
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>ahas-sentinel-client</artifactId> <version>x.y.x</version> </dependency>On the Connect a Spring Application page, find the latest version in the Step 1: Add Pom dependency section. Replace
x.y.xwith the latest version number.
Method 2: Add the JAR file dependency.
On the Connect a Spring Application page, click Click this link to download to download the compressed package. Extract the package and place all JAR files on your application's classpath.
Add checkpoints in your application project.
Add an HTTP checkpoint.
@Configuration public class SentinelWebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SentinelWebInterceptor()); } }Add a checkpoint for a common interface.
Register SentinelResourceAspect as a Spring Bean.
@Configuration public class SentinelAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); } }Add the @SentinelResource annotation to your business method.
// Original business method @SentinelResource(value = "getUserById") public User getUserById(String id) { return new User(id); }
Configure the startup parameters for your application based on its network environment.
Category
Parameter example
Internal network
-Dproject.name=AppName -Dahas.namespace=default -Dahas.regionId=<RegionId>NoteReplace
AppNamewith a custom application name.Replace
with the ID of the region where your application is deployed. For a mapping of regions and RegionIds, see the Region and RegionId mapping table below.
Public network
-Dproject.name=AppName -Dahas.license=<license> -Dahas.namespace=default -Dahas.regionId=cn-publicNoteReplace
AppNamewith a custom application name and replacewith your actual license. If you are in a public network region, you need to view the license information in the Step 3: Configure startup parameters area. This step is not required for internal network regions. For more information, see View License.

Restart the application.
Optional: Use annotations to configure how your application handles triggered flow control, degradation, or system protection rules. For more information, see Annotation-based configurations.
NoteBy default, if you do not add these handlers, triggering a rule throws a subclass of
BlockException. Specifically, aFlowExceptionis thrown for a flow control rule, and aDegradeExceptionis thrown for a degradation rule.
Verify the result
Log on to the AHAS console. In the left-side navigation pane, choose . The integration is successful if the resource card for your application appears and shows active data reporting.

Region and RegionId mapping
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 |