ORM Configuration - Methods - 图1tip

Below is an introduction to the configuration of database underlying management. If you are interested in the underlying configuration management of databases, you can continue reading the following sections.

Data Structure

The internal configuration management data structure of the gdb database management module is as follows:

ConfigNode is used to store database node information; ConfigGroup manages configuration groups consisting of multiple database nodes (typically, a group corresponds to a business database cluster); Config manages multiple ConfigGroup configuration groups.

Configuration Management Features:

  1. Supports management of multi-node database clusters;
  2. Each node can individually configure connection properties;
  3. Uses a singleton pattern to manage database instance objects;
  4. Supports database cluster group management and access to instantiated database operation objects according to group names;
  5. Supports management of various relational databases, configurable through the ConfigNode.Type attribute;
  6. Supports Master-Slave read-write separation, configurable through the ConfigNode.Role attribute;
  7. Supports client load balancing management, configurable through the ConfigNode.Weight attribute, where a higher value indicates a higher priority;
  1. type Config map[string]ConfigGroup // Database configuration object
  2. type ConfigGroup []ConfigNode // Database group configuration
  3. // Database configuration item (one configuration item corresponds to multiple configuration items)
  4. type ConfigNode struct {
  5. Host string // Address
  6. Port string // Port
  7. User string // User
  8. Pass string // Password
  9. Name string // Database name
  10. Type string // Database type: mysql, sqlite, mssql, pgsql, oracle
  11. Link string // (Optional) Custom link information. When set, the above link fields (Host,Port,User,Pass,Name) will be invalid (this field is an extension feature)
  12. Extra string // (Optional) Additional feature configuration for different databases, defined by the underlying database driver
  13. Role string // (Optional, default is master) Database role, used for master-slave operation separation, at least one master is required, parameter value: master, slave
  14. Debug bool // (Optional) Enable debug mode
  15. Charset string // (Optional, default is utf8) Encoding, default is utf8
  16. Prefix string // (Optional) Table name prefix
  17. Weight int // (Optional) Weight for load balancing calculation. When there is only one node in the cluster, the weight is meaningless
  18. MaxIdleConnCount int // (Optional) Maximum number of idle connections in the connection pool
  19. MaxOpenConnCount int // (Optional) Maximum number of open connections in the connection pool
  20. MaxConnLifetime time.Duration // (Optional, in seconds) Time duration for which a connection object can be reused
  21. }

It is worth mentioning that the greatest feature of gdb configuration management is that (within the same process), all database cluster information is uniformly maintained by the same configuration management module, and different businesses use different group names for database cluster configuration and retrieval.

Configuration Method

This is a native call to the gdb module to configure database management. Developers who want to control their database configuration management independently can refer to the following methods. If not needed, this section can be ignored.

API Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/database/gdb

  1. // Add a database node to a specified group
  2. func AddConfigNode(group string, node ConfigNode)
  3. // Add a configuration group to database configuration management (overwrites if the same name exists)
  4. func AddConfigGroup(group string, nodes ConfigGroup)
  5. // Add a database node to the default group (default is 'default', can be changed)
  6. func AddDefaultConfigNode(node ConfigNode)
  7. // Add a configuration group to the database configuration management (default group is 'default', can be changed)
  8. func AddDefaultConfigGroup(nodes ConfigGroup)
  9. // Set the default group name, which will be automatically read when obtaining the default database object
  10. func SetDefaultGroup(groupName string)
  11. // Set the database configuration to defined configuration information, overriding the original configuration
  12. func SetConfig(c Config)

The default group means that if you do not specify a configuration group name when obtaining a database object, the gdb will default to reading the configuration group. For example, gdb.NewByGroup() can be used to obtain a database object of the default group. As a simple approach, we can use the SetConfig configuration management method of the gdb package to perform custom global database configuration, for example:

  1. gdb.SetConfig(gdb.Config {
  2. "default" : gdb.ConfigGroup {
  3. gdb.ConfigNode {
  4. Host : "192.168.1.100",
  5. Port : "3306",
  6. User : "root",
  7. Pass : "123456",
  8. Name : "test",
  9. Type : "mysql",
  10. Role : "master",
  11. Weight : 100,
  12. },
  13. gdb.ConfigNode {
  14. Host : "192.168.1.101",
  15. Port : "3306",
  16. User : "root",
  17. Pass : "123456",
  18. Name : "test",
  19. Type : "mysql",
  20. Role : "slave",
  21. Weight : 100,
  22. },
  23. },
  24. "user-center" : gdb.ConfigGroup {
  25. gdb.ConfigNode {
  26. Host : "192.168.1.110",
  27. Port : "3306",
  28. User : "root",
  29. Pass : "123456",
  30. Name : "test",
  31. Type : "mysql",
  32. Role : "master",
  33. Weight : 100,
  34. },
  35. },
  36. })

Subsequently, we can use gdb.NewByGroup("database group name") to obtain a database operation object. This object is used for a series of subsequent database methods/chain operations.