SOFARPC Documentation

repository·master·Indexed 26 days ago

https://github.com/sofastack/sofa-rpc

A production-grade Java RPC framework designed for high-performance, extensible, and stable point-to-point remote service invocation in microservices architectures. It supports multiple protocols (Bolt, Rest, Dubbo), various invocation modes (synchronous, one-way, callback, generalized), and integrates with service registries. The framework provides features for routing, load balancing, cluster fault tolerance, and service warming, requiring JDK 8 and Maven 3.2.5 or above for build and runtime.

Tokens
2.1K
Snippets
4
Records
13
Agent score
86%

What's inside SOFARPC

  1. Overview of SOFARPC

    master
    SOFARPC is a high-performance, high-extensibility, production-level Java RPC framework designed to simplify remote service invocation between applications. It provides stable, efficient point-to-point communication with minimal code intrusion. The framework is highly extensible through model abstractions and interfaces for filters, routing, load balancing, and more. It also supports a rich MicroService governance ecosystem.
  2. Design overview of SOFA-RPC HTTP protocol

    master

    The SOFA-RPC HTTP protocol supports both HTTP/1.1 and HTTP/2 designs.

    • HTTP/1.1: Provides server-side support only.
    • HTTP/2: Supports both server and client, but does not currently support streaming calls.

    Default Settings:

    • Server Port: 12300 (default)
    • Keep-Alive: true (default for both server and client)
  3. Key Features of SOFARPC

    master

    SOFARPC supports the following capabilities:

    • Transparent & High-Performance Calls: Simplified remote service invocation.
    • Routing & Load Balancing: Supports multiple service routing and load balancing strategies.
    • Registry Integration: Supports integration with various service registries.
    • Multiple Protocols: Supports Bolt, Rest, Dubbo, and other protocols.
    • Diverse Invocation Modes: Supports synchronous, one-way, callback, and generalized (泛化) calling methods.
    • Fault Tolerance & Stability: Supports cluster fault tolerance, service warming, and automatic fault isolation.
    • Extensibility: Powerful extension mechanism to add functional components on demand.
  4. Construct URL and URI for HTTP calls

    master

    The URL/URI follows this format: ${protocol}://${ip}:${port}/${interfaceName}[:${uniqueId}]/methodName

    • Recommended Method: Use POST for standard calls.
    • Simple Functions: Can use GET with query parameters.

    Examples:

    POST (Standard): POST h2c://127.0.0.1:12300/com.alipay.sofa.rpc.test.HelloService/sayHello

    GET (Simple): GET http://127.0.0.1:12300/com.alipay.sofa.rpc.test.HelloService:1.0:groupA/sayHello?name=zhang&age=123

    POST h2c://127.0.0.1:12300/com.alipay.sofa.rpc.test.HelloService/sayHello
    GET http://127.0.0.1:12300/com.alipay.sofa.rpc.test.HelloService:1.0:groupA/sayHello?name=zhang&age=123
  5. Handle HTTP Responses and Errors

    master

    Responses are categorized into three states:

    1. Successful Response

    • HTTP Status Code: 200
    • Headers: sofa_head_serialize_type (Required) and content-type (Required, defaults to application/json).
    • Body: The serialized return value (e.g., a JSON string: "Hello world").

    2. Business Exception (Application Error)

    • HTTP Status Code: 200
    • Headers: sofa_head_resp_error is set to true.
    • Body: A UTF-8 encoded error description string (e.g., java.lang.RuntimeException: 业务异常).

    3. RPC Framework Exception

    • HTTP Status Code: Not 200 (e.g., 404 for service not found, 400 for bad requests).
    • Body: A UTF-8 encoded error description string (e.g., RPC-02411: 未找到业务服务...).
  6. Reference Response Headers

    master

    The following headers are used in the HTTP response:

    HEADMeaningRequiredNotes
    sofa_head_serialize_typeSerialization typeYesIf not set, content-type is used
    content-typeSerializationYesDefaults to application/json
    sofa_head_resp_errorIndicates if the response is an exceptionNo
    sofa_head_resp_baggageResponse baggage (pass-through)No
    | HEAD | 含义 | 必填 | 备注 |
    |:-------------------------|:----------|:----|:-----------------------------------|
    | sofa_head_serialize_type | 序列化类型 | 是 | 如果没有设置这个值将读取 content-type |
    | content-type             | 序列化     | 是 | 默认 application/json              |
    | sofa_head_resp_error     | 返回是否异常 | 否  |                                   |
    | sofa_head_resp_baggage   | 响应透传值 | 否  |                                   |
  7. Configure Request Headers for HTTP calls

    master

    When making an RPC call via HTTP, use the following headers to control serialization and metadata:

    HEADMeaningRequiredNotes
    sofa_head_serialize_typeSerialization typeYesIf not set, content-type is used
    content-typeSerializationNo
    sofa_head_target_appTarget applicationNo
    sofa_head_req_propsRequest additional propertiesNo
    sofa_head_req_baggageRequest baggage (pass-through)No

    Supported content-type values:

    • hessian2: x-application/hessian
    • json: application/json
    • protobuf: application/x-protobuf
    | HEAD | 含义 | 必填 | 备注 |
    |:-------------------------|:----------|:----|:-----------------------------------|
    | sofa_head_serialize_type | 序列化类型 | 是 | 如果没有设置这个值将读取 content-type |
    | content-type             | 序列化     | 否  |                                    |
    | sofa_head_target_app     | 目标应用   | 否  |                                    |
    | sofa_head_req_props      | 请求附加值 | 否  |                                    |
    | sofa_head_req_baggage    | 请求透传值 | 否  |                                    |