31 lines
915 B
Go
31 lines
915 B
Go
package mongo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/qiniu/qmgo"
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
goproto "google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
var url = "mongodb://root:Wishpal2024@dds-bp1da1ddd62bede41.mongodb.rds.aliyuncs.com:3717,dds-bp1da1ddd62bede42.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-77304659"
|
|
|
|
func NewMongoClient() (*qmgo.Client, error) {
|
|
clientCfg := &qmgo.Config{
|
|
Uri: "mongodb://admin:Wishpal%402023@172.31.37.71:27017",
|
|
//Uri: url,
|
|
ConnectTimeoutMS: goproto.Int64(30000),
|
|
MaxPoolSize: goproto.Uint64(16),
|
|
MinPoolSize: goproto.Uint64(0),
|
|
SocketTimeoutMS: goproto.Int64(5000),
|
|
ReadPreference: &qmgo.ReadPref{Mode: readpref.PrimaryMode},
|
|
}
|
|
cli, err := qmgo.NewClient(context.Background(), clientCfg)
|
|
if err != nil {
|
|
fmt.Printf("NewMongoClient fail, cfg: %v, err: %v", clientCfg, err)
|
|
return nil, err
|
|
}
|
|
return cli, nil
|
|
}
|