Quick Start

更新时间:
复制 MD 格式

This topic describes the complete procedure for service throttling, from local project development to cloud application deployment and configuration.

Procedure

  1. Develop the project locally.

    For more information, see Implement an SOFARPC service locally.

  2. Import dependencies and configure them locally.

    • Import dependencies

      In the pom.xml file of the endpoint module in your SOFABoot web project, import the DRM and guardian dependencies as follows:

      <dependency>
           <groupId>com.alipay.sofa</groupId>
           <artifactId>ddcs-enterprise-sofa-boot-starter</artifactId>
      </dependency>
      
      <dependency>
           <groupId>com.alipay.sofa</groupId>
           <artifactId>guardian-sofa-boot-starter</artifactId>
      </dependency>

      Using rest-enterprise-sofa-boot-starter and rpc-enterprise-sofa-boot-starter

      When importing rpc-enterprise-sofa-boot-starter, also import the following dependencies to enable the service throttling feature. Otherwise, service throttling will not work.

      <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-autoconfigure</artifactId>
      </dependency>
      <dependency>
           <groupId>com.alipay.sofa</groupId>
           <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
      </dependency>

      When you define an interface, also define a custom exception and an exception handler. This lets you throw an exception when a request is throttled and return the result to the frontend.

    • Local configuration

      After you import the guardian-sofa-boot-starter dependency, the application can throttle SOFARPC interfaces and Spring MVC requests. To also throttle methods defined in internal Spring Beans, add an AOP interceptor configuration to the Spring Bean configuration file as follows:

      <!-- Import the bean defined in guardian. -->
      <import resource="classpath:META-INF/spring/guardian-sofalite.xml"/>
      <!-- Configure the AOP interceptor. -->
      <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <property name="interceptorNames">
              <list>
                   <value>guardianExtendInterceptor</value>
              </list>
          </property>
          <property name="beanNames">
              <list>
                   <!-- Configure the beans to be intercepted. -->
                   <value>*DAO</value>
              </list>
          </property>
          <!-- To use a CGLIB proxy, uncomment the following line. -->
          <!-- <property name="optimize" value="true" /> -->
      </bean>
  3. Publishing to the cloud.

    For deployment instructions, see the following topics:

  4. Configure service throttling rules in the SOFAStack console.

    For more information, see Configure service throttling rules in the console.