更新时间:2024-11-28 GMT+08:00
分享

原生DB

创建go版本下database/sql包sql.DB数据源,进行mysql操作

  1. 在go.mod文件中引入依赖,即引入MAS-GO-SDK。

    github.com/huaweicloud/devcloud-go

  2. 结合配置项说明,添加配置文件或者配置信息。

    • 配置文件示例config_mysql.yaml。
      props:
        version: v1
        appId: xxx
        monitorId: xxxx
        databaseName: xxxx
      etcd:
        address: xxx.xxx.xxx.xxx:xxxx
        apiVersion: v3
        username: xxxx
        password: xxxx
        httpsEnable: false
      # require
      datasource:
        ds0:
          url: tcp(xxx.xxx.xxx.xxx:xxxx)/ds0
          username: xxxx
          password: xxxx
        ds0-slave0:
          url: tcp(xxx.xxx.xxx.xxx:xxxx)/ds0-slave0
          username: xxxx
          password: xxxx
        ds0-slave1:
          url: tcp(xxx.xxx.xxx.xxx:xxxx)/ds0-slave1
          username: xxxx
          password: xxxx
        ds1:
          url: tcp(xxx.xxx.xxx.xxx:xxxx)/ds1
          username: xxxx
          password: xxxx
        ds1-slave0:
          url: tcp(xxx.xxx.xxx.xxx:xxxx)/ds1-slave0
          username: xxxx
          password: xxxx
        ds1-slave1:
          url: tcp(xxx.xxx.xxx.xxx:xxxx)/ds1-slave1
          username: xxxx
          password: xxxx
      # require
      router:
        active: c0
        routeAlgorithm: single-read-write
        retry:
          times: 10
          delay: 50# ms
        nodes:
          c0:
            master: ds0
            loadBalance: RANDOM
            slaves:
              - ds0-slave0
              - ds0-slave1
          c1:
            master: ds1
            loadBalance: ROUND_ROBIN
            slaves:
              - ds1-slave0
              - ds1-slave1
    • 配置信息。
      func mysqlConfiguration() *config.ClusterConfiguration {
          return &config.ClusterConfiguration{
              Props: &mas.PropertiesConfiguration{
                  Version: "v1",
                  AppID: "xxx",
                  MonitorID: "xxxx",
                  DatabaseName: "xxx",
              },
              EtcdConfig: &etcd.EtcdConfiguration{
                  Address: "xxx.xxx.xxx.xxx:xxxx",
                  APIVersion: "v3",
                  Username: "xxxx",
                  Password: "xxxx",
                  HTTPSEnable: false,
              },
              RouterConfig: &config.RouterConfiguration{
                  Nodes: map[string]*config.NodeConfiguration{
                    "dc0": {
                          Master: "ds0",
                          LoadBalance: "RANDOM",
                          Slaves: []string{"ds0-slave0","ds0-slave1"},
                      },
                  "dc1": {
                          Master: "ds1",
                          LoadBalance: "ROUND_ROBIN",
                          Slaves: []string{"ds1-slave0","ds1-slave1"},
                      },
                  },
                  Active: "dc0",
                  Retry: &config.RetryConfiguration{
                      Times: "10",
                      Delay: "50",
                  },
                  RouteAlgorithm: "single-read-write",
              },
              DataSource: map[string]*config.DataSourceConfiguration{
                  "ds0": {
                      URL:      "tcp(xxx.xxx.xxx.xxx:xxxx)/ds0",
                      Username: "xxxx",
                      Password: "xxxx",
                  },
                  "ds0-slave0": {
                      URL:      "tcp(xxx.xxx.xxx.xxx:xxxx)/ds0-slave0",
                      Username: "xxxx",
                      Password: "xxxx",
                  },
                  "ds0-slave1": {
                      URL:      "tcp(xxx.xxx.xxx.xxx:xxxx)/ds0-slave1",
                      Username: "xxxx",
                      Password: "xxxx",
                  },
                  "ds1": {
                      URL:      "tcp(xxx.xxx.xxx.xxx:xxxx)/ds1",
                      Username: "xxxx",
                      Password: "xxxx",
                  },
                  "ds1-slave0": {
                      URL:      "tcp(xxx.xxx.xxx.xxx:xxxx)/ds1-slave0",
                      Username: "xxxx",
                      Password: "xxxx",
                  },
                  "ds1-slave1": {
                      URL:      "tcp(xxx.xxx.xxx.xxx:xxxx)/ds1-slave1",
                      Username: "xxxx",
                      Password: "xxxx",
                  },
              },
          }
      }

  3. 初始化创建sql.DB数据源DevSporeDb。

    • 配置文件方式创建
      import (
          "database/sql"
          "log"
      
          _ "github.com/huaweicloud/devcloud-go/sql-driver/mysql"
      )
      var (
          DevSporeDb *sql.DB
          err error
      )
      func init() {
          DevSporeDb, err = sql.Open("devspore_mysql", "./conf/resources/config_mysql.yaml")
          if err != nil {
              log.Fatalln(err)
          }
      }
    • 配置信息方式创建。
      import (
          "database/sql"
          "log"
      
          devspore "github.com/huaweicloud/devcloud-go/sql-driver/mysql"
      )
      var (
          DevSporeDb *sql.DB
          err error
      )
      func init() {
          devspore.SetClusterConfiguration(mysqlConfiguration())
          DevSporeDb, err = sql.Open("devspore_mysql", "")
      
          if err != nil {
              log.Fatalln(err)
          }
      }

  4. 在需要执行Mysql操作的地方使用DevSporeDb执行Mysql操作,具体执行因业务各异,执行对应的sql.DB命令。

相关文档