mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-17 09:54:45 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13642d7402 | ||
|
|
18d620118e | ||
|
|
f02592d126 | ||
|
|
0f890887f7 | ||
|
|
c5c05b6ae9 | ||
|
|
68b891df51 | ||
|
|
63199bf862 | ||
|
|
4d5d45d555 | ||
|
|
ca48c443cd | ||
|
|
7d40a48a1b |
@@ -87,6 +87,9 @@ service.json如下:
|
|||||||
---------------
|
---------------
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
|
"Global": {
|
||||||
|
"AreaId": 1
|
||||||
|
},
|
||||||
"Service":{
|
"Service":{
|
||||||
"HttpService":{
|
"HttpService":{
|
||||||
"ListenAddr":"0.0.0.0:9402",
|
"ListenAddr":"0.0.0.0:9402",
|
||||||
@@ -157,7 +160,7 @@ service.json如下:
|
|||||||
```
|
```
|
||||||
|
|
||||||
---------------
|
---------------
|
||||||
以上配置分为两个部分:Service与NodeService,NodeService中配置的对应结点中服务的配置,如果启动程序中根据nodeid查找该域的对应的服务,如果找不到时,从Service公共部分查找。
|
以上配置分为两个部分:Global,Service与NodeService。Global是全局配置,在任何服务中都可以通过cluster.GetCluster().GetGlobalCfg()获取,NodeService中配置的对应结点中服务的配置,如果启动程序中根据nodeid查找该域的对应的服务,如果找不到时,从Service公共部分查找。
|
||||||
|
|
||||||
**HttpService配置**
|
**HttpService配置**
|
||||||
* ListenAddr:Http监听地址
|
* ListenAddr:Http监听地址
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ var cluster Cluster
|
|||||||
type Cluster struct {
|
type Cluster struct {
|
||||||
localNodeInfo NodeInfo //本结点配置信息
|
localNodeInfo NodeInfo //本结点配置信息
|
||||||
masterDiscoveryNodeList []NodeInfo //配置发现Master结点
|
masterDiscoveryNodeList []NodeInfo //配置发现Master结点
|
||||||
|
globalCfg interface{} //全局配置
|
||||||
|
|
||||||
localServiceCfg map[string]interface{} //map[serviceName]配置数据*
|
localServiceCfg map[string]interface{} //map[serviceName]配置数据*
|
||||||
mapRpc map[int]NodeRpcInfo //nodeId
|
mapRpc map[int]NodeRpcInfo //nodeId
|
||||||
@@ -418,3 +419,7 @@ func HasService(nodeId int, serviceName string) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cls *Cluster) GetGlobalCfg() interface{} {
|
||||||
|
return cls.globalCfg
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
type NodeInfoList struct {
|
type NodeInfoList struct {
|
||||||
MasterDiscoveryNode []NodeInfo //用于服务发现Node
|
MasterDiscoveryNode []NodeInfo //用于服务发现Node
|
||||||
NodeList []NodeInfo
|
NodeList []NodeInfo
|
||||||
@@ -29,18 +30,19 @@ func (cls *Cluster) ReadClusterConfig(filepath string) (*NodeInfoList,error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cls *Cluster) readServiceConfig(filepath string) (map[string]interface{},map[int]map[string]interface{},error) {
|
func (cls *Cluster) readServiceConfig(filepath string) (interface{}, map[string]interface{}, map[int]map[string]interface{}, error) {
|
||||||
c := map[string]interface{}{}
|
c := map[string]interface{}{}
|
||||||
//读取配置
|
//读取配置
|
||||||
d, err := ioutil.ReadFile(filepath)
|
d, err := ioutil.ReadFile(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(d, &c)
|
err = json.Unmarshal(d, &c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GlobalCfg, ok := c["Global"]
|
||||||
serviceConfig := map[string]interface{}{}
|
serviceConfig := map[string]interface{}{}
|
||||||
serviceCfg, ok := c["Service"]
|
serviceCfg, ok := c["Service"]
|
||||||
if ok == true {
|
if ok == true {
|
||||||
@@ -60,7 +62,7 @@ func (cls *Cluster) readServiceConfig(filepath string) (map[string]interface{},
|
|||||||
mapNodeService[int(nodeId.(float64))] = serviceCfg
|
mapNodeService[int(nodeId.(float64))] = serviceCfg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return serviceConfig,mapNodeService,nil
|
return GlobalCfg, serviceConfig, mapNodeService, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cls *Cluster) readLocalClusterConfig(nodeId int) ([]NodeInfo, []NodeInfo, error) {
|
func (cls *Cluster) readLocalClusterConfig(nodeId int) ([]NodeInfo, []NodeInfo, error) {
|
||||||
@@ -104,7 +106,6 @@ func (cls *Cluster) readLocalClusterConfig(nodeId int) ([]NodeInfo,[]NodeInfo,er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return masterDiscoverNodeList, nodeInfoList, nil
|
return masterDiscoverNodeList, nodeInfoList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,11 +120,15 @@ func (cls *Cluster) readLocalService(localNodeId int) error {
|
|||||||
for _, f := range fileInfoList {
|
for _, f := range fileInfoList {
|
||||||
if f.IsDir() == false {
|
if f.IsDir() == false {
|
||||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
||||||
serviceConfig,mapNodeService,err := cls.readServiceConfig(filePath)
|
currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if currGlobalCfg != nil {
|
||||||
|
cls.globalCfg = currGlobalCfg
|
||||||
|
}
|
||||||
|
|
||||||
for _, s := range cls.localNodeInfo.ServiceList {
|
for _, s := range cls.localNodeInfo.ServiceList {
|
||||||
for {
|
for {
|
||||||
//取公共服务配置
|
//取公共服务配置
|
||||||
@@ -164,7 +169,6 @@ func (cls *Cluster) parseLocalCfg(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (cls *Cluster) checkDiscoveryNodeList(discoverMasterNode []NodeInfo) bool {
|
func (cls *Cluster) checkDiscoveryNodeList(discoverMasterNode []NodeInfo) bool {
|
||||||
for i := 0; i < len(discoverMasterNode)-1; i++ {
|
for i := 0; i < len(discoverMasterNode)-1; i++ {
|
||||||
for j := i + 1; j < len(discoverMasterNode); j++ {
|
for j := i + 1; j < len(discoverMasterNode); j++ {
|
||||||
|
|||||||
15
node/node.go
15
node/node.go
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/duanhf2012/origin/profiler"
|
"github.com/duanhf2012/origin/profiler"
|
||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
"github.com/duanhf2012/origin/util/timer"
|
"github.com/duanhf2012/origin/util/timer"
|
||||||
|
"github.com/duanhf2012/origin/util/buildtime"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
slog "log"
|
slog "log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -38,6 +39,7 @@ func init() {
|
|||||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10))
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10))
|
||||||
|
|
||||||
console.RegisterCommandBool("help",false,"<-help> This help.",usage)
|
console.RegisterCommandBool("help",false,"<-help> This help.",usage)
|
||||||
|
console.RegisterCommandString("name","","<-name nodeName> Node's name.",setName)
|
||||||
console.RegisterCommandString("start","","<-start nodeid=nodeid> Run originserver.",startNode)
|
console.RegisterCommandString("start","","<-start nodeid=nodeid> Run originserver.",startNode)
|
||||||
console.RegisterCommandString("stop","","<-stop nodeid=nodeid> Stop originserver process.",stopNode)
|
console.RegisterCommandString("stop","","<-stop nodeid=nodeid> Stop originserver process.",stopNode)
|
||||||
console.RegisterCommandString("config","","<-config path> Configuration file path.",setConfigPath)
|
console.RegisterCommandString("config","","<-config path> Configuration file path.",setConfigPath)
|
||||||
@@ -53,13 +55,20 @@ func usage(val interface{}) error{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, `orgin version: orgin/2.14.20201029
|
if len(buildtime.GetBuildDateTime())>0 {
|
||||||
Usage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...
|
fmt.Fprintf(os.Stderr, "Welcome to Origin(build info: %s)\nUsage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...\n",buildtime.GetBuildDateTime())
|
||||||
`)
|
}else{
|
||||||
|
fmt.Fprintf(os.Stderr, "Welcome to Origin\nUsage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...\n")
|
||||||
|
}
|
||||||
|
|
||||||
console.PrintDefaults()
|
console.PrintDefaults()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setName(val interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func setPprof(val interface{}) error {
|
func setPprof(val interface{}) error {
|
||||||
listenAddr := val.(string)
|
listenAddr := val.(string)
|
||||||
if listenAddr==""{
|
if listenAddr==""{
|
||||||
|
|||||||
113
sysmodule/mongodbmodule/mongodbmodule.go
Normal file
113
sysmodule/mongodbmodule/mongodbmodule.go
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package mongodbmodule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MongoModule struct {
|
||||||
|
client *mongo.Client
|
||||||
|
maxOperatorTimeOut time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
*mongo.Client
|
||||||
|
maxOperatorTimeOut time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mm *MongoModule) Init(uri string, maxOperatorTimeOut time.Duration) error {
|
||||||
|
var err error
|
||||||
|
mm.client, err = mongo.NewClient(options.Client().ApplyURI(uri))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
mm.maxOperatorTimeOut = maxOperatorTimeOut
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mm *MongoModule) Start() error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := mm.client.Connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctxTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := mm.client.Ping(ctxTimeout, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mm *MongoModule) TakeSession() Session {
|
||||||
|
return Session{Client: mm.client, maxOperatorTimeOut: mm.maxOperatorTimeOut}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) CountDocument(db string, collection string) (int64, error) {
|
||||||
|
ctxTimeout, cancel := s.GetDefaultContext()
|
||||||
|
defer cancel()
|
||||||
|
return s.Database(db).Collection(collection).CountDocuments(ctxTimeout, bson.D{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) NextSeq(db string, collection string, id interface{}) (int, error) {
|
||||||
|
var res struct {
|
||||||
|
Seq int
|
||||||
|
}
|
||||||
|
|
||||||
|
ctxTimeout, cancel := s.GetDefaultContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
after := options.After
|
||||||
|
updateOpts := options.FindOneAndUpdateOptions{ReturnDocument: &after}
|
||||||
|
err := s.Client.Database(db).Collection(collection).FindOneAndUpdate(ctxTimeout, bson.M{"_id": id}, bson.M{"$inc": bson.M{"Seq": 1}},&updateOpts).Decode(&res)
|
||||||
|
return res.Seq, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//indexKeys[索引][每个索引key字段]
|
||||||
|
func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error {
|
||||||
|
return s.ensureIndex(db, collection, indexKeys, bBackground, false,sparse)
|
||||||
|
}
|
||||||
|
|
||||||
|
//indexKeys[索引][每个索引key字段]
|
||||||
|
func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error {
|
||||||
|
return s.ensureIndex(db, collection, indexKeys, bBackground, true,sparse)
|
||||||
|
}
|
||||||
|
|
||||||
|
//keys[索引][每个索引key字段]
|
||||||
|
func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool,sparse bool) error {
|
||||||
|
var indexes []mongo.IndexModel
|
||||||
|
for _, keys := range indexKeys {
|
||||||
|
keysDoc := bsonx.Doc{}
|
||||||
|
for _, key := range keys {
|
||||||
|
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
options:= options.Index().SetUnique(unique).SetBackground(bBackground)
|
||||||
|
if sparse == true {
|
||||||
|
options.SetSparse(true)
|
||||||
|
}
|
||||||
|
indexes = append(indexes, mongo.IndexModel{Keys: keysDoc, Options:options })
|
||||||
|
}
|
||||||
|
|
||||||
|
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.maxOperatorTimeOut)
|
||||||
|
defer cancel()
|
||||||
|
_, err := s.Database(db).Collection(collection).Indexes().CreateMany(ctxTimeout, indexes)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) GetDefaultContext() (context.Context, context.CancelFunc) {
|
||||||
|
return context.WithTimeout(context.Background(), s.maxOperatorTimeOut)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Collection(db string, collection string) *mongo.Collection {
|
||||||
|
return s.Database(db).Collection(collection)
|
||||||
|
}
|
||||||
208
sysmodule/mongodbmodule/mongodbmodule_test.go
Normal file
208
sysmodule/mongodbmodule/mongodbmodule_test.go
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
package mongodbmodule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Student struct {
|
||||||
|
ID primitive.ObjectID `bson:"_id"`
|
||||||
|
Name string `bson: "name"`
|
||||||
|
Age int `bson: "age"`
|
||||||
|
Sid string `bson: "sid"`
|
||||||
|
Status int `bson: "status"`
|
||||||
|
MapData map[int64]int64 `bson: "maptest"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StudentName struct {
|
||||||
|
Name string `bson: "name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Example(t *testing.T) {
|
||||||
|
//0.初始化模块
|
||||||
|
var mongoModule MongoModule
|
||||||
|
err := mongoModule.Init("mongodb://admin:123456@192.168.2.15:27017/?authSource=admin&maxPoolSize=100&maxConnecting=2&connectTimeoutMS=10000&socketTimeoutMS=5000", time.Second*10)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mongoModule.Start()
|
||||||
|
|
||||||
|
//1.创建索引
|
||||||
|
session := mongoModule.TakeSession()
|
||||||
|
var IndexKeys [][]string
|
||||||
|
//分别建立number,name组合索引
|
||||||
|
var key1 []string
|
||||||
|
key1 = append(key1, "number", "name")
|
||||||
|
//keyId为索引
|
||||||
|
var key2 []string
|
||||||
|
key2 = append(key2, "KeyId")
|
||||||
|
|
||||||
|
IndexKeys = append(IndexKeys, key1, key2)
|
||||||
|
session.EnsureIndex("testdb", "test2", IndexKeys, true)
|
||||||
|
|
||||||
|
//2.插入数据
|
||||||
|
//插入单行
|
||||||
|
var s Student
|
||||||
|
s.ID = primitive.NewObjectID()
|
||||||
|
s.Age = 35
|
||||||
|
s.Name = "xxx"
|
||||||
|
|
||||||
|
ctx, cancel := session.GetDefaultContext()
|
||||||
|
ret, err := session.Collection("testdb", "test2").InsertOne(ctx, s)
|
||||||
|
cancel()
|
||||||
|
insertId := ret.InsertedID.(primitive.ObjectID)
|
||||||
|
log.Println(insertId.Hex(), err)
|
||||||
|
|
||||||
|
//插入多行
|
||||||
|
var ss []interface{}
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
var s Student
|
||||||
|
s.ID = primitive.NewObjectID()
|
||||||
|
s.Age = i
|
||||||
|
s.Name = fmt.Sprintf("name_%d", i)
|
||||||
|
ss = append(ss, s)
|
||||||
|
}
|
||||||
|
manyRet, err := session.Collection("testdb", "test2").InsertMany(ctx, ss)
|
||||||
|
cancel()
|
||||||
|
log.Println(manyRet, err)
|
||||||
|
|
||||||
|
//3.更新数据
|
||||||
|
//update
|
||||||
|
var sUpdate Student
|
||||||
|
sUpdate.ID, _ = primitive.ObjectIDFromHex("62429c3b32a269dcbe0cdc7b")
|
||||||
|
sUpdate.Age = 35
|
||||||
|
sUpdate.Name = "xxxx555555"
|
||||||
|
|
||||||
|
//update := bson.M{"$set": bson.M{"age": 35}}
|
||||||
|
update := bson.M{"$set": sUpdate}
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
objectId, _ := primitive.ObjectIDFromHex("62429c3b32a269dcbe0cdc7b")
|
||||||
|
updateResult, err := session.Collection("testdb", "test2").UpdateOne(ctx, bson.M{"_id": objectId}, update)
|
||||||
|
cancel()
|
||||||
|
log.Println("collection.UpdateOne:", updateResult, err)
|
||||||
|
|
||||||
|
//upset
|
||||||
|
var s_upset Student
|
||||||
|
s_upset.ID, _ = primitive.ObjectIDFromHex("62429c3b32a269dcbe0cdc7e")
|
||||||
|
s_upset.Name = "皇商xx"
|
||||||
|
s_upset.Age = 35099
|
||||||
|
s_upset.Sid = "Sid22"
|
||||||
|
s_upset.MapData = make(map[int64]int64)
|
||||||
|
s_upset.MapData[3434] = 13424234
|
||||||
|
s_upset.MapData[444] = 656565656
|
||||||
|
update = bson.M{"$set": s_upset}
|
||||||
|
updateOpts := options.Update().SetUpsert(true)
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
|
||||||
|
updateResult, err = session.Collection("testdb", "test2").UpdateOne(ctx, bson.M{"_id": s_upset.ID}, update, updateOpts)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("collection.UpdateOne:", updateResult)
|
||||||
|
|
||||||
|
//4.删除
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
Id, _ := primitive.ObjectIDFromHex("62429aa71b6445d1f5bf9aee")
|
||||||
|
deleteResult, err := session.Collection("testdb", "test2").DeleteOne(ctx, bson.M{"_id": Id})
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("collection.DeleteOne:", deleteResult)
|
||||||
|
|
||||||
|
//5.查询
|
||||||
|
//查询单条
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
|
||||||
|
var sel_One Student
|
||||||
|
Ids, _ := primitive.ObjectIDFromHex("62429b13bbff8acf147ef8d7")
|
||||||
|
err = session.Collection("testdb", "test2").FindOne(context.Background(), bson.M{"_id": Ids}).Decode(&sel_One)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("collection.FindOne: ", sel_One)
|
||||||
|
|
||||||
|
//查询多条1
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
cur, err := session.Collection("testdb", "test2").Find(ctx, bson.M{})
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := cur.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var sSlice []Student
|
||||||
|
ctx, cancel = session.GetDefaultContext()
|
||||||
|
err = cur.All(ctx, &sSlice)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
cur.Close(ctx)
|
||||||
|
cancel()
|
||||||
|
log.Println("collection.Find curl.All: ", sSlice)
|
||||||
|
for _, one := range sSlice {
|
||||||
|
log.Println(one)
|
||||||
|
}
|
||||||
|
|
||||||
|
//查询多条2
|
||||||
|
cur, err = session.Collection("testdb", "test2").Find(context.Background(), bson.M{})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := cur.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for cur.Next(context.Background()) {
|
||||||
|
var s Student
|
||||||
|
if err = cur.Decode(&s); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("collection.Find cur.Next:", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
cur.Close(context.Background())
|
||||||
|
|
||||||
|
//模糊查询
|
||||||
|
cur, err = session.Collection("testdb", "test2").Find(context.Background(), bson.M{"name": primitive.Regex{Pattern: "xxx"}})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := cur.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var sSlices []Student
|
||||||
|
err = cur.All(context.Background(), &sSlices)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
cur.Close(context.Background())
|
||||||
|
|
||||||
|
//6.获取数据总行数
|
||||||
|
count, err := session.Collection("testdb", "test2").CountDocuments(context.Background(), bson.D{})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(count)
|
||||||
|
}
|
||||||
|
log.Println("collection.CountDocuments:", count)
|
||||||
|
|
||||||
|
//7.自动序号
|
||||||
|
Id, _ = primitive.ObjectIDFromHex("62429b13bbff8acf147ef8d7")
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
seq, _ := session.NextSeq("testdb", "test2", Id)
|
||||||
|
log.Println(seq)
|
||||||
|
}
|
||||||
|
}
|
||||||
15
util/buildtime/build.go
Normal file
15
util/buildtime/build.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package buildtime
|
||||||
|
|
||||||
|
/*
|
||||||
|
//查询buildtime包中的位置,在github.com/duanhf2012/origin/util/buildtime.BuildTime中
|
||||||
|
go tool nm ./originserver.exe |grep buildtime
|
||||||
|
|
||||||
|
//编译传入编译时间信息
|
||||||
|
go build -ldflags "-X 'github.com/duanhf2012/origin/util/buildtime.BuildTime=20200101'"
|
||||||
|
*/
|
||||||
|
var BuildTime string
|
||||||
|
|
||||||
|
|
||||||
|
func GetBuildDateTime() string {
|
||||||
|
return BuildTime
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user