preface

Several call methods of SOFA-RPC were introduced above, including one-way call, synchronous call, Future call and callback, and generalized call and filter were introduced. This article will cover the advanced features of SOFA-RPC, including parameter configuration, custom thread pools, preheating weights and automatic fault elimination.

Other articles

  • Ant Financial RPC framework SOFA- INITIAL RPC experience

  • Ant Financial SOFA-Boot Integration SOFA-RPC

  • Ant Financial SOFA-Boot Integration SOFA-RPC

  • Ant Financial SOFA-Boot Integration SOFA-RPC

The body of the

1. Set parameters

The SOFABoot RPC Starter provides convenient parameter Settings. These parameters can currently be divided into two parts. One part is configuration such as port, registry address and so on, which is in application.properties. The other part is configuration such as timeout, which is in XML.

The XML configuration

  • Call timeout

Here is how to set the timeout, in ms, and throw an exception if the call exceeds this time. This parameter can be set on both the server and client. The client timeout duration is preferred. The default client is 3000 and currently works with Bolt, REST, and Dubbo.

<sofa:binding.bolt>
    <sofa:global-attrs timeout="5000"/>
</sofa:binding.bolt>
Copy the code
  • Get address wait time

The following is the set waiting time for obtaining the address, in ms. If the service reference waits longer than this time at startup, it will not wait for the address and will continue to start. Set on the client. The default value is -1, which means that the client waits until the address is reached. Currently in effect for Bolt, REST.

<sofa:binding.bolt>
    <sofa:global-attrs address-wait-time="30000"/>
</sofa:binding.bolt>
Copy the code
  • Timeout period for establishing a connection

The timeout period for establishing a connection is set as follows, in ms. An exception is thrown if it takes longer to establish a connection. The default value is 5000. Currently in effect for Bolt, REST.

<sofa:binding.bolt>
    <sofa:global-attrs connect.timeout="30000"
</sofa:binding.bolt>
Copy the code
  • The weight

Here’s how to set the weights. When the client invokes a call, if the call algorithm is random, the call will be random based on the weight. Server Settings, default is 100. Currently in effect for Bolt.

<sofa:binding.bolt>
    <sofa:global-attrs weight="200"/>
</sofa:binding.bolt>
Copy the code
  • Lazy connection

By default, the client establishes the connection as soon as the registry pushes the address to the client, usually before the first call. If the service reference property lazy is set to true, the client does not connect to the remote address to be called until the first call. The default is false.

Set the lazy connection as follows, setting the lazy property to true. Bolt and Dubbo protocols are currently supported.

<sofa:reference id="lazyServiceReferenceBolt" interface="com.ostenant.sofa.rpc.example.lazy.LazyService">
    <sofa:binding.bolt>
        <sofa:global-attrs lazy="true"/>
    </sofa:binding.bolt>
</sofa:reference>
Copy the code
  • Check attribute

Service references do not require available addresses and connections by default when the client is started. If check is set to true, the service reference checks for the address and connection when the client starts, and throws an exception if the address and connection do not exist. The default is false.

Set the check connection as follows, and set the check property to true. Bolt and Dubbo protocols are currently supported.

<sofa:reference id="checkServiceReferenceBolt" interface="com.ostenant.sofa.rpc.example.check.CheckService">
    <sofa:binding.bolt>
        <sofa:global-attrs check="true"/>
    </sofa:binding.bolt>
</sofa:reference>
Copy the code
  • Retry count

Number of retries: The maximum number of retries after the first invocation fails. If the retries succeed, no further retries will be allowed. The default is 0. Set the number of calls as follows, using the retries attribute to specify the number of retries. Bolt and Dubbo protocols are currently supported.

<sofa:reference id="retriesServiceReferenceBolt" interface="com.ostenant.sofa.rpc.example.retries.RetriesService">
    <sofa:binding.bolt>
        <sofa:global-attrs retries="2"/>
    </sofa:binding.bolt>
</sofa:reference>
Copy the code
  • Load balancing

Select the load balancing mode as follows. Use the loadBalancer attribute to specify the load balancing policy to be used during the call. The default load balancing policy is Random.

The software supports random, localPref, roundRobin, consistentHash, and weightRoundRobin load balancing policies. For details, see SOFARPC load balancing. Bolt protocol is currently supported.

<sofa:reference id="loadBalancerServiceReference" interface="com.ostenant.sofa.rpc.example.loadBalancer.LoadBalancerService">
    <sofa:binding.bolt>
        <sofa:global-attrs loadBalancer="random"/>
    </sofa:binding.bolt>
</sofa:reference>
Copy the code
  • Method level configuration

As follows, the SOFA: Method element is a method-level configuration. The method level configuration takes precedence over the service level configuration. The name attribute specifies the name of the method. Support call timeout, call mode, callback class Settings. The method level configuration applies the same protocol as the service level configuration.

<sofa:binding.bolt>
    <sofa:method name="sayMethod" timeout="3000" type="sync" callback-ref="xxx"/>
</sofa:binding.bolt>
Copy the code

The Properties configuration

attribute describe The default value
spring.application.name The application of
logging.path Log path
logging.level.com.alipay.sofa.rpc.boot Sofa -rpc-boot-start Log level (starter logs) info
logging.level.com.alipay.sofa.rpc Sofa – RPC log level (SOFA – RPC core log is basically here) info
com.alipay.sofa.rpc.bolt.port Bolt port 22000
com.alipay.sofa.rpc.bolt.io.thread.count The number of BOLT I/O threads
com.alipay.sofa.rpc.bolt.executor.thread.count Maximum number of bolt business threads 200
com.alipay.sofa.rpc.bolt.accepts.count Maximum number of long connections bolt can support 100000
com.alipay.sofa.rpc.rest.hostname The rest of the hostname
com.alipay.sofa.rpc.rest.port Rest port 8341
com.alipay.sofa.rpc.rest.io.thread.count Number of REST I/O threads Number of CPU cores x 2
com.alipay.sofa.rpc.rest.executor.thread.count Number of REST business threads 200
com.alipay.sofa.rpc.rest.max.request.size The maximum length of rest byte requests 1024 * 1024 * 10
com.alipay.sofa.rpc.rest.telnet Whether REST supports Telnet true
com.alipay.sofa.rpc.rest.daemon Whether REST supports daemons true
com.alipay.sofa.rpc.dubbo.port Dubbo port 20880
com.alipay.sofa.rpc.dubbo.io.thread.count Number of DUbbo I/O threads Number of CPU cores + 1
com.alipay.sofa.rpc.dubbo.executor.thread.count Number of dubbo business threads 100
com.alipay.sofa.rpc.dubbo.accepts.count Maximum number of long connections supported by Dubbo 0: no limit

2. Customize the thread pool

SOFA-RPC supports custom business thread pools. A separate business thread pool can be set up for a given service, which is isolated from SOFA-RPC’s own business thread pool, and multiple services can share a separate thread pool. Bolt protocol is currently supported.

In the SOFA-Boot environment, you can set up a custom thread pool for a service as follows:

  • Declare a custom thread pool

The following statement a custom thread pool, the class must be com. Alipay. Sofa.. RPC server UserThreadPool, this is sofa xml-rpc provide classes, init – method = “init” must be declared to initialize.

<bean id="customerThreadPool" class="com.alipay.sofa.rpc.server.UserThreadPool" init-method="init">
    <property name="corePoolSize" value="10"/>
    <property name="maximumPoolSize" value="10"/>
    <property name="queueSize" value="5"/>
    <property name="threadPoolName" value="customerThreadPool_name"/>
</bean>
Copy the code
  • Set up a custom thread pool for the service

Set a custom thread pool for this service using the Thread-pool-ref attribute of sofa:global-attrs element as follows. CustomerThreadPool is the bean ID of the custom thread pool above.

<bean id="threadPoolServiceImpl" class="com.ostenant.sofa.rpc.example.threadpool.ThreadPoolServiceImpl"/>
<sofa:service ref="threadPoolServiceImpl" interface="com.alipay.sofa.rpc.samples.threadpool.ThreadPoolService">
    <sofa:binding.bolt>
        <sofa:global-attrs thread-pool-ref="customerThreadPool"/>
    </sofa:binding.bolt>
</sofa:service>
Copy the code

3. Preheat the weights

SOFA-RPC provides the function of preheating weights so that the client machine can distribute traffic according to the corresponding weights of the server. Bolt protocol is currently supported.

SOFA-Boot provides a series of parameter properties to preheat specified services. The client machine can automatically parse these parameters and distribute traffic by weight.

  • Warm-up-time: indicates the warm-up time of the service
  • Warm-up -weight: the weight of service Settings during warm-up
  • Weight: weight of service Settings after they are preheated
<sofa:reference id="sampleRestFacadeReferenceBolt" interface="com.alipay.sofa.endpoint.facade.SampleFacade">
    <sofa:binding.bolt>
         <sofa:global-attrs warm-up-time="10000" warm-up-weight="10" weight="100"/>
     </sofa:binding.bolt>
</sofa:reference>
Copy the code

In the preceding configuration, the preheating period of the service is 10 seconds. During the preheating period, the weight is 10. After the preheating period, the normal weight is 100.

If the service is published on both machines A and B. A The machine is in the preheating period, use the above configuration; B has been preheated. The normal weight is 200. Then when the client is called, the proportion of traffic distribution is 10:200. A After the machine is preheated, the proportion of traffic distribution is 100:200.

4. Automatic fault elimination

Automatic fault culling automatically monitors RPC calls, demotes the weight of the failed node, and restores the weight when the node recovers. Bolt protocol is currently supported.

In SOFA-Boot, you only need to configure automatic fault elimination parameters to application.properties. Set only the parameters that you care about, and keep the default values for other parameters. It is important to note that the RPC. Aft. Regulation. The function is the effective global switch, this function does not run on closed, the other parameters are not effective.

  • Meaning of automatic fault removal configuration parameters
attribute describe The default value
com.alipay.sofa.rpc.aft.time.window Time window size: period during which statistics are calculated. 10s
com.alipay.sofa.rpc.aft.least.window.count Minimum number of calls in the time window: Only data that reaches this minimum value in the time window is added to the calculation and regulation. Ten times
com.alipay.sofa.rpc.aft.least.window.exception.rate.multiple Degradation ratio of the outlier rate to the average outlier rate of the service in a time window: During statistics calculation, the average outlier rate of all valid calling IP addresses of the service is calculated. If the outlier rate of an IP address is greater than or equal to the minimum outlier rate, the IP address is degraded. 6 times
com.alipay.sofa.rpc.aft.weight.degrade.rate Demotion rate: The demotion rate of the address when it is demoted. 1/20
com.alipay.sofa.rpc.aft.weight.recover.rate Recovery ratio: recovery ratio of the address when weight recovery is performed. 2 times
com.alipay.sofa.rpc.aft.degrade.effective Degrade switch: If this switch is turned on, the application will degrade the address that matches the degrade. Otherwise, only logs will be printed. False (closed)
com.alipay.sofa.rpc.aft.degrade.least.weight Demoted minimum weight: If the address weight is smaller than the minimum weight, the minimum weight will be used as the demoted value. 0
com.alipay.sofa.rpc.aft.degrade.max.ip.count Maximum number of DEGRADED IP addresses: The maximum number of degraded IP addresses of a service cannot exceed this value. 2
com.alipay.sofa.rpc.aft.regulation.effective Global switch: If this switch is turned on, the entire single point of failure auto-cull is enabled. Otherwise, the logic of this feature is not enabled at all. False (closed)
  • Configuration of the sample
com.alipay.sofa.rpc.aft.time.window=20 com.alipay.sofa.rpc.aft.least.window.count=30 Com. Alipay. Sofa. RPC. Aft. Further. Window. Exception. Rate. The multiple = 6 com. Alipay. Sofa. RPC. The aft. Weight. Degrade. Rate = 0.5 Com. Alipay. Sofa. RPC. Aft. Weight. Recover. Rate = 1.2 com. Alipay. Sofa. RPC. The aft. Degrade. Effective = true com.alipay.sofa.rpc.aft.degrade.least.weight=1 com.alipay.sofa.rpc.aft.degrade.max.ip.count=2 com.alipay.sofa.rpc.aft.regulation.effective=trueCopy the code

In the preceding configuration, the fault elimination function and degrade switch are enabled by default. When a node fails, it will be weighted down, and when it recovers, it will be weighted back up.

The node health status is measured every 20 seconds. Nodes that are invoked for more than 30 times within 20 seconds are calculated as data.

If the outlier rate of a single node exceeds 6 times of the average outlier rate of all nodes, the node will be demoted with a weight ratio of 0.5. The weight is reduced to at least 1. If the outlier rate of a single node is less than 6 times of the average outlier rate, the weight of the node is restored with a recovery ratio of 1.2. A maximum of two IP addresses can be degraded for a single service.

summary

This paper introduces the advanced functions of SOFA-RPC, including parameter configuration, custom thread pool, service preheating, automatic degradation and weight recovery, etc. The basic functions provided by SOFA-RPC, as well as the integration of SOFA-Boot configuration and usage are introduced. This has a preliminary understanding, is conducive to the follow-up in-depth implementation of the principle and analysis of the source code.


Welcome to pay attention to the technical public number: Zero one Technology Stack

This account will continue to share learning materials and articles on back-end technologies, including virtual machine basics, multithreaded programming, high-performance frameworks, asynchronous, caching and messaging middleware, distributed and microservices, architecture learning and progression.