Connect a Spring application

更新时间:
复制 MD 格式

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

  1. Log on to the AHAS console.

  2. In the upper-left corner of the AHAS console, select the region where your application is deployed.

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

  4. 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.

  5. 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.x with the latest version number.

      pom版本号2.png

    • 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.

  6. 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.

      1. Register SentinelResourceAspect as a Spring Bean.

        @Configuration
        public class SentinelAspectConfiguration {
          @Bean
          public SentinelResourceAspect sentinelResourceAspect() {
            return new SentinelResourceAspect();
          }
        }
      2. Add the @SentinelResource annotation to your business method.

        // Original business method
        @SentinelResource(value = "getUserById")
        public User getUserById(String id) {
          return new User(id);
        }
  7. 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>
    Note
    • Replace AppName with 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-public
    Note

    Replace AppName with a custom application name and replace with 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.

    Spring license2.png

  8. Restart the application.

  9. Optional: Use annotations to configure how your application handles triggered flow control, degradation, or system protection rules. For more information, see Annotation-based configurations.

    Note

    By default, if you do not add these handlers, triggering a rule throws a subclass of BlockException. Specifically, a FlowException is thrown for a flow control rule, and a DegradeException is thrown for a degradation rule.

Verify the result

Log on to the AHAS console. In the left-side navigation pane, choose Traffic Protection > Application Protection. The integration is successful if the resource card for your application appears and shows active data reporting.

应用防护.png

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