AOP and Extension Mechanism

AOP and Extension Mechanism

1. Extension Module and Init Method

1.1 Interface and Implementation

In Golang, an interface is often accompanied by multiple implementation classes. Dubbo-go provides a pluggable and extensible mechanism for interface implementation classes, reducing coupling between modules and facilitating developers in introducing and customizing components.

1.2 Init Method in Golang

The init method, as a special method in Golang, executes first on program startup when a user imports a set of modules, performing loading logic. This method is an important way for dubbogo to register extension components.

1.3 Extension Module

In the framework source code, there is a special module: common/extension, which is responsible for caching all the implementations of the extensible components.

Taking the load balancing module as an example: common/extension/loadbalance.go

  1. package extension
  2. import (
  3. "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
  4. )
  5. var loadbalances = make(map[string]func() loadbalance.LoadBalance)
  6. // SetLoadbalance sets the loadbalance extension with @name
  7. // For example: random/round_robin/consistent_hash/least_active/...
  8. func SetLoadbalance(name string, fcn func() loadbalance.LoadBalance) {
  9. loadbalances[name] = fcn
  10. }
  11. // GetLoadbalance finds the loadbalance extension with @name
  12. func GetLoadbalance(name string) loadbalance.LoadBalance {
  13. if loadbalances[name] == nil {
  14. panic("loadbalance for " + name + " is not existing, make sure you have import the package.")
  15. }
  16. return loadbalances[name]()
  17. }

This module contains Get and Set methods. Get returns the instantiated LoadBalance interface, while Set is used to register factory functions, with a map used for caching these functions.

When the user imports _ “dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/random”, the corresponding module’s init function will be loaded, calling the Set method to register the unique key and factory function into the above map.

cluster/loadbalance/random/loadbalance.go

  1. package random
  2. import (
  3. "math/rand"
  4. )
  5. import (
  6. "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
  7. "dubbo.apache.org/dubbo-go/v3/common/constant"
  8. "dubbo.apache.org/dubbo-go/v3/common/extension"
  9. "dubbo.apache.org/dubbo-go/v3/protocol"
  10. )
  11. func init() {
  12. extension.SetLoadbalance(constant.LoadBalanceKeyRandom, NewRandomLoadBalance)
  13. }

Thus, once all init methods have been executed, the instantiated object can be obtained through the extension module’s Get method.

1.4 Imports Module

Dubbogo places all built-in modules in imports/imports.go; users need to import this module to use the basic capabilities provided by the framework.

  1. import (
  2. _ "dubbo.apache.org/dubbo-go/v3/imports"
  3. )

2. Component Loading Process

  1. The user imports _ “dubbo.apache.org/dubbo-go/v3/imports”

  2. The program starts, executing the init functions sequentially, registering factory functions/instantiated objects to the extension module.

  3. The framework starts, loading configuration, retrieving module keys from the configuration and obtaining instantiated objects based on these keys.

  4. Users can also manually call the extension’s Get method to obtain instantiated objects and use them directly.

3. Custom Components

Based on the above introduction, developers can emulate built-in modules and write custom extension components.

4. Aspect-Oriented Programming Design (AOP)

In the Dubbo-go service framework, many interfaces are designed based on AOP concepts. For example, Invoker, Filter, LoadBalance, and Router.

These multiple implementations often form a chain of calls, with a single implementation class only handling its relevant logic.

Related Reading: 【AOP wikipedia】

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)