فهرست منبع

连接MongoDB

zhou.xiaoning 5 سال پیش
والد
کامیت
6046686ec8
7فایلهای تغییر یافته به همراه145 افزوده شده و 5 حذف شده
  1. 77 5
      config/config.go
  2. 8 0
      config/config.xml
  3. 41 0
      db/mongodb.go
  4. 1 0
      go.mod
  5. 2 0
      go.sum
  6. 8 0
      main.go
  7. 8 0
      models/quote.go

+ 77 - 5
config/config.go

@@ -43,12 +43,21 @@ type MqConfig struct {
 	Exchange string
 }
 
+type MongoDBConfig struct {
+	Hostname string
+	Port     int
+	DBName   string
+	Username string
+	Password string
+}
+
 type ServiceConfig struct {
-	WebCfg   WebConfig
-	LogCfg   LogConfig
-	DbCfg    DbConfig
-	RedisCfg RedisConfig
-	MqCfg    MqConfig
+	WebCfg     WebConfig
+	LogCfg     LogConfig
+	DbCfg      DbConfig
+	RedisCfg   RedisConfig
+	MqCfg      MqConfig
+	MongoDBCfg MongoDBConfig
 }
 
 func (c *ServiceConfig) Init(path string) error {
@@ -235,6 +244,49 @@ func (c *ServiceConfig) Init(path string) error {
 		SerCfg.MqCfg.Exchange = exchange.SelectAttrValue("value", "entry")
 	}
 
+	// MongoDB配置
+	mongodbsettings := root.SelectElements("MongoDBSetting")
+	for _, setting := range mongodbsettings {
+		// 主机地址
+		hostname := setting.SelectElement("Hostname")
+		if hostname == nil {
+			return errors.New("read mongondb hostname failed")
+		}
+		SerCfg.MongoDBCfg.Hostname = hostname.SelectAttrValue("value", "")
+
+		// 端口
+		port := setting.SelectElement("Port")
+		if port == nil {
+			return errors.New("read mongondb port failed")
+		}
+		ret, err := strconv.ParseUint(port.SelectAttrValue("value", "5025"), 10, 32)
+		if err != nil {
+			return errors.New("read mongondb port failed")
+		}
+		SerCfg.MongoDBCfg.Port = int(ret)
+
+		// 数据库名
+		dbname := setting.SelectElement("DBName")
+		if dbname == nil {
+			return errors.New("read mongondb dbname failed")
+		}
+		SerCfg.MongoDBCfg.DBName = dbname.SelectAttrValue("value", "")
+
+		// 用户名
+		username := setting.SelectElement("Username")
+		if username == nil {
+			return errors.New("read mongondb username failed")
+		}
+		SerCfg.MongoDBCfg.Username = username.SelectAttrValue("value", "")
+
+		// 密码
+		password := setting.SelectElement("Password")
+		if password == nil {
+			return errors.New("read mongondb password failed")
+		}
+		SerCfg.MongoDBCfg.Password = password.SelectAttrValue("value", "")
+	}
+
 	return nil
 }
 
@@ -288,3 +340,23 @@ func (c *ServiceConfig) GetRedisConnNum() int {
 func (c *ServiceConfig) GetRedisTimeout() int {
 	return SerCfg.RedisCfg.TimeOut
 }
+
+func (c *ServiceConfig) GetMongoDBHostname() string {
+	return SerCfg.MongoDBCfg.Hostname
+}
+
+func (c *ServiceConfig) GetMongoDBPort() int {
+	return SerCfg.MongoDBCfg.Port
+}
+
+func (c *ServiceConfig) GetMongoDBDBName() string {
+	return SerCfg.MongoDBCfg.DBName
+}
+
+func (c *ServiceConfig) GetMongoDBUsername() string {
+	return SerCfg.MongoDBCfg.Username
+}
+
+func (c *ServiceConfig) GetMongoDBPassword() string {
+	return SerCfg.MongoDBCfg.Password
+}

+ 8 - 0
config/config.xml

@@ -33,4 +33,12 @@
         <Url       value="amqp://guest:guest@192.168.31.114:5020/test"/>
         <Exchange  value="entry"/>
     </MqSetting>
+
+    <MongoDBSetting>
+        <Hostname   value="192.168.30.182"/>
+        <Port       value="5025"/>
+        <DBName     value="HistoryQuote"/>
+        <Username   value="quote_test01"/>
+        <Password   value="123456"/>
+    </MongoDBSetting>
 </Configuration>

+ 41 - 0
db/mongodb.go

@@ -0,0 +1,41 @@
+package db
+
+import (
+	"fmt"
+	"mtp2_if/config"
+
+	"gopkg.in/mgo.v2"
+)
+
+var session *mgo.Session
+var mongodb *mgo.Database
+
+// InitMongoDB 初始化连接MongoDB
+func InitMongoDB() error {
+	// 创建链接
+	var err error
+	session, err = mgo.Dial(fmt.Sprintf("%s:%d", config.SerCfg.GetMongoDBHostname(), config.SerCfg.GetMongoDBPort()))
+	if err != nil {
+		return err
+	}
+
+	// 选择DB
+	mongodb = session.DB(config.SerCfg.GetMongoDBDBName())
+
+	// 登陆
+	if err := mongodb.Login(config.SerCfg.GetMongoDBUsername(), config.SerCfg.GetMongoDBPassword()); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// GetMongoDB 获取MongoDB Database
+func GetMongoDB() *mgo.Database {
+	return mongodb
+}
+
+// CloseMongoDB 关闭MongoDB连接
+func CloseMongoDB() {
+	session.Close()
+}

+ 1 - 0
go.mod

@@ -46,5 +46,6 @@ require (
 	golang.org/x/text v0.3.3 // indirect
 	golang.org/x/tools v0.0.0-20200928201943-a0ef9b62deab // indirect
 	gopkg.in/flosch/pongo2.v3 v3.0.0-20141028000813-5e81b817a0c4 // indirect
+	gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
 	gopkg.in/yaml.v2 v2.3.0 // indirect
 )

+ 2 - 0
go.sum

@@ -430,6 +430,8 @@ gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/R
 gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
 gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
 gopkg.in/ini.v1 v1.47.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
+gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
+gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 8 - 0
main.go

@@ -52,6 +52,14 @@ func main() {
 	}
 	defer db.Close()
 
+	// 初始化MongoDB引擎
+	err = db.InitMongoDB()
+	if err != nil {
+		logger.GetLogger().Errorf("init mongodb engine failed:", err.Error())
+		return
+	}
+	defer db.CloseMongoDB()
+
 	// 初始化Redis客户端
 	err = rediscli.InitRedisCli()
 	if err != nil {

+ 8 - 0
models/quote.go

@@ -0,0 +1,8 @@
+package models
+
+const (
+	CycleType_Min = iota
+	CycleType_5Min
+)
+
+func GetHistoryCycle()