1
Introduction to Pixiu is a cloud-native, high-performance, and scalable microservice API gateway based on Dubbogo. As a gateway product, Pixiu helps users easily create, publish, maintain, monitor and protect APIs of any scale, accept and process thousands of concurrent API calls, including traffic management, CORS support, authorization and access control, restrictions, Monitoring, and API version management.

What is Pixiu

Before answering what Pixiu is, let's briefly explain what Dubbo is. Dubbo is an open source high-performance RPC framework with rich service governance capabilities and excellent scalability. Dubbo has expanded to Dubbo-go ​, which provides users with Golang's Dubbo solution, which bridges the gap between the two languages ​​and makes Dubbo closer to cloud native.

皮球.png
As a Golang service, Dubbo-go implements mutual calls with Dubbo services. However, in daily use scenarios, users often have the need to expose Dubbo services in a RESTful style while also taking into account internal Dubbo calls. To solve this scenario, Pixiu ​ (Chinese: 貔貅, formerly known as dubbo-go-proxy) as a Dubbo API gateway came into being. The name Pixiu is adopted because the image of Zuul, a similar Java product, is a Western monster. As a domestic product, Pixiu uses a similar sacred animal Pixiu from China as the project name. It also expressed the Dubbo community's determination to expand a complete cloud-native ecological chain.

In the current Dubbo multi-language ecology, Java is naturally the best developed, followed by Golang. Other languages ​​are not satisfactory. The dubbo-go-pixiu project is a project developed based on dubbo-go. The current interface protocol layer supports seven layers of HTTP request calls. It is planned to support gRPC request calls in the future version 0.5. Its other mission is to serve as a A new dubbo multilingual solution.

Why use Pixiu

Pixiu is a cloud-native, high-performance, and scalable microservice API gateway based on Dubbogo. As a gateway product, Pixiu helps users easily create, publish, maintain, monitor and protect APIs of any scale, accept and process thousands of concurrent API calls, including traffic management, CORS support, authorization and access control, restrictions, Monitoring, and API version management. In addition, as a derivative product of Dubbo, Pixiu can help Dubbo users perform protocol conversion and realize cross-system and cross-protocol service capability interoperability.

The overall design of Pixiu follows the following principles:

  1. High performance: High throughput and millisecond latency.
  2. Extensible: Through go-plugin, users can extend Pixiu's functions according to their needs.
  3. Easy to use: Users can go online with a small amount of configuration.

Features and core functions of Pixiu

  • Provide support for RESTful API and Dubbo API

Non-RESTful APIs and Dubbo protocol services often need to be modified before they can be opened to the outside world in the RESTful API style. Pixiu provides protocol conversion function. Through Pixiu, developers can configure their own HTTP API or Dubbo API to open to the outside world in a RESTful API style. The v0.2.1 version already supports protocol conversion from HTTP to Dubbo based on generalized calls and HTTP protocol forwarding. In subsequent versions, the community will add support for gRPC and http2 protocols.

  • User-oriented configuration

The configuration of general gateways is often cumbersome and complicated. Pixiu aims to be an easy-to-use gateway product, with three levels of configuration in design, the global configuration of the Gateway layer, the configuration of the API resource layer and the configuration of the HTTP verbs method. Through three different levels of configuration, both deep customization can be achieved, and a unified default configuration is also supported; at the same time, it supports local configuration files and can also use a unified configuration server. In addition, a console module is also provided, through which the configuration hot update is supported. Pixiu's supporting console interface is also being developed simultaneously.

  • Integration of common functions

General functions such as retry, fuse, flow control, and access control no longer need to be implemented repeatedly on each back-end service. Using Pixiu, by configuring filters, developers can perform global control, and they can also configure their own rules according to the API. So developers can focus on business logic and services instead of spending time on maintaining infrastructure.

  • Scalable

Different usage scenarios have their own unique needs. To meet the customized needs of different users, Pixiu uses a plug-in model. Developers can write go plugins to embed their own unique business logic into the Pixiu gateway in the form of filters to realize functions such as enterprise login authentication.
截屏2021-07-15 下午6.23.32.png

Figure 1: List of Pixiu's core functions

Pixiu's architecture design

**image.png
Figure 2: Pixiu architecture

: dubbo-go-pixiu, composed of four main modules: Listener, Router, Filters and Clients;
Dubbo Cluster: The cluster where the Dubbo service is located, including one or more Dubbo Services;
Other Cluster: The cluster where services other than Dubbo are located, now supports HTTP services, and will expand to support other services such as gRPC in the future;
Registry Center: Registry Center, which maintains the calling address information of each business service;
Metadata Center: Metadata Center, which maintains the configuration information of each business service and stores the configuration information of Pixiu itself.

As an API gateway derived from Dubbo, Pixiu uses Golang to build, mainly because: 1. Golang's GMP, net poller and other features make Golang very suitable for building IO-intensive applications; 2. Using Golang can directly introduce some components in Dubbo-go To simplify development.

The entire Pixiu can be roughly divided into four main modules: Listener, Router, Filters and Client .

1、Listener

In Pixiu, Listener represents the way the outside can access Pixiu. Expose the Gateway by configuring the specified protocol type, address, port and other properties. The HTTP protocol is temporarily supported at this stage, and gRPC will be added in the future.

listeners: 
  - name: "net/http" 
    address: 
      socket_address: 
        protocol_type: "HTTP" 
        address: "0.0.0.0" 
        port: 8888 
    config: 
      idle_timeout: 5s 
      read_timeout: 5s 
      write_timeout: 5s

2、Router

Router is the routing component of Pixiu. According to the configuration file, Pixiu stores the exposed URLs in the memory as a tree. When the router component is requested, it will find the corresponding back-end service and its API configuration according to the URL and HTTP method, and encapsulate the information in In the request, provide enough content for subsequent filter and client calls.

At this stage, Router provides the following functions:

  • Support request one-to-one forwarding routing configuration or wildcard routing configuration.
  • Supports forwarding of HTTP requests to the back-end HTTP service.
  • Supports the conversion of HTTP requests into dubbo generalized call requests.

3、Filters

Filter is the main component of Pixiu to achieve additional functions and scalability. Its implementation is similar to the filter in Dubbo-go. According to the designation of the filter in the configuration, a call chain is generated, so that before calling the back-end service, the logic in each filter is run again to achieve functions such as throttling and logging.

If the user needs a customized filter, it can be realized by writing go-plugin. In the configuration, it can be achieved by loading the .so file, and specifying the plugin group and plugin name in the API config through a configuration similar to the following.


pluginFilePath: "" 
pluginsGroup: 
  - groupName: "group1" 
    plugins: 
      - name: "rate limit" 
        version: "0.0.1" 
        priority: 1000 
        externalLookupName: "ExternalPluginRateLimit" 
      - name: "access" 
        version: "0.0.1" 
        priority: 1000 
        externalLookupName: "ExternalPluginAccess" 
  - groupName: "group2" 
    plugins: 
      - name: "blacklist" 
        version: "0.0.1" 
        priority: 1000 
        externalLookupName: "ExternalPluginBlackList"

4、Client

Client is responsible for calling specific services. At this stage, Pixiu supports HTTP and Dubbo back-end services. The community will gradually add other clients such as gRPC to meet different protocols.

The implementation of the HTTP client is relatively simple. According to the back-end service information obtained in the Router, requests are generated and invoked through the official Golang package net/http.

The implementation of Dubbo client is slightly more complicated than HTTP client, and its basis is the generalized call of Dubbo service. The generalized calling technology is a very basic function provided by Dubbo. You only need to know the called method name, parameter type and return value type to initiate a service call. The client's generalized call to the server can either discover the service through the registry, or directly connect to the server to implement dynamic calls to the service.

As shown in the code below, Pixiu dynamically configures the referenceConfig, and then generates Dubbo's Generic Client (generalized call client) through GetRPCService for the next call.

 referenceConfig := dg.NewReferenceConfig(irequest.Interface, context.TODO())
  referenceConfig.InterfaceName = irequest.Interface
  referenceConfig.Cluster = constant.DEFAULT_CLUSTER
  var registers []string
  for k := range dgCfg.Registries {
    registers = append(registers, k)
  }
  referenceConfig.Registry = strings.Join(registers, ",")

  if len(irequest.DubboBackendConfig.Protocol) == 0 {
    referenceConfig.Protocol = dubbo.DUBBO
  } else {
    referenceConfig.Protocol = irequest.DubboBackendConfig.Protocol
  }

  referenceConfig.Version = irequest.DubboBackendConfig.Version
  referenceConfig.Group = irequest.Group
  referenceConfig.Generic = true
  if len(irequest.DubboBackendConfig.Retries) == 0 {
    referenceConfig.Retries = "3"
  } else {
    referenceConfig.Retries = irequest.DubboBackendConfig.Retries
  }
  dc.lock.Lock()
  defer dc.lock.Unlock()
  referenceConfig.GenericLoad(key)
  clientService := referenceConfig.GetRPCService().(*dg.GenericService)

In fact, in the client of the generalization call, the key step to actually execute the generalization call is the generic\_filter in Dubbo-go (code snippet below). When calling Invoke of generic\_filter, it is agreed that the first invocation parameter list is the method name, the second is the parameter type list, and the third is the parameter value list. generic\_filter converts the parameter value list requested by the user into a unified format map (struct2MapAll in the code), and turns the positive and negative serialization operations of the class (struct in golang) into the positive and negative serialization operations of the map. This eliminates the need for POJO descriptions to inject the hessain library through hard coding, thereby completing the generalized call of Dubbo services.

func (ef *GenericFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
  if invocation.MethodName() == constant.GENERIC && len(invocation.Arguments()) == 3 {
    oldArguments := invocation.Arguments()
    if oldParams, ok := oldArguments[2].([]interface{}); ok {
      newParams := make([]hessian.Object, 0, len(oldParams))
      for i := range oldParams {
        newParams = append(newParams, hessian.Object(struct2MapAll(oldParams[i])))
      }
      newArguments := []interface{}{
        oldArguments[0],
        oldArguments[1],
        newParams,
      }
      newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments())
      newInvocation.SetReply(invocation.Reply())
      return invoker.Invoke(ctx, newInvocation)
    }
  }
  return invoker.Invoke(ctx, invocation)
}

Summarize

Through the above four modules and the simple introduction of the registration center, it is not difficult to find that when the request is received by Pixiu through the listener, the request is passed to the router. According to the configuration of the interface, the router finds the target back-end service from the original request and sends it to the filter component along with the related API configuration. The filter component executes sequentially according to the original request, API configuration and other information, and finally the request reaches the client, and the back-end service is called through the client.

Pixiu's future

1.png
Figure 3: Pixiu iteration milestone

In addition to Pixiu as a gateway product, its derivative projects will also be in our future plans, the main purpose is to provide better usability. For example, due to the lack of native annotations in the Golang language, Dubbo-go needs to generate service metadata and write it into the registry through configuration files. Classmates from the education company wrote a scan code tool\_ https://github.com/jack15083/dubbo-go-proxy-tool\_ , and add corresponding comments before each RPC service method, In this way, metadata is generated by scanning annotations before the service starts. Pixiu also plans to provide a package in a future version to allow the service to use annotations to use \_ https://github.com/MarcGrol/golangAnnotations \_ to generate API configuration and register to Pixiu.

Pixiu is currently positioned as a seven-layer protocol gateway, and its initial version is defined as a Dubbo service gateway. As a product in the cloud era, Pixiu's development direction is bound to be cloud-native. The current version is 0.2.1, which has realized the basic Dubbo/Http service proxy and some common gateway functions. The 0.4 and subsequent versions currently under development support gRPC and Spring Cloud service calls, and MQ service support will be provided in the future. In addition, the community will continue to optimize the configuration method, reduce the difficulty of users, and continue to optimize the official filter, so that Pixiu can implement more general gateway functions at the official level.

image.png

In the next year, the community plans to support the xDS API and evolve Pixiu into a sidecar of Dubbo mesh. Its ultimate goal is to evolve the form of Proxy Service Mesh from the existing dubbo mesh form. Based on this form, script language programs such as Js, Python, PHP, Ruby and Perl will not only reap the original technical dividends of dubbo mesh, but also have a high probability of reaping performance improvements.

Pixiu's ultimate goal in Dubbo Mesh is to gradually unify Pixiu's east-west and north-south data plane traffic, while allowing it to gradually have the capability of Application Runtime as a key solution for Dubbo's multilingual ecology.

The dubbogo community [Dingding Group No. 23331795] is with dubbogo.

Zhenyu, Apache Dubbo Committer, currently manages the entire team of the IT department of a consumer goods company in Hong Kong. In the summer of 2020, I accidentally saw an article introducing dubbogo and joined the dubbogo community. Currently, I am leading the development of Pixiu 0.4.0.
\_

Click https://developer.aliyun.com/community/cloudnative to learn more about cloud native content!

Copyright Notice: content of this article is contributed spontaneously by Alibaba Cloud real-name registered users, and the copyright belongs to the original author. The Alibaba Cloud Developer Community does not own its copyright, and does not assume corresponding legal responsibilities. For specific rules, please refer to the "Alibaba Cloud Developer Community User Service Agreement" and the "Alibaba Cloud Developer Community Intellectual Property Protection Guidelines". If you find suspected plagiarism in this community, fill in the infringement complaint form to report it. Once verified, the community will immediately delete the suspected infringing content.

阿里云开发者
3.2k 声望6.3k 粉丝

阿里巴巴官方技术号,关于阿里巴巴经济体的技术创新、实战经验、技术人的成长心得均呈现于此。