Skip to content

ego-component/emongo

Repository files navigation

emongo 组件使用指南

goproxy.cn Release License: MIT Example Doc

1 简介

mongo-driver 进行了轻量封装,并提供了以下功能:

  • 规范了标准配置格式,提供了统一的 Load().Build() 方法。
  • 支持自定义拦截器
  • 提供了默认的 Debug 拦截器,开启 Debug 后可输出 Request、Response 至终端。
  • 提供了默认的 Metric 拦截器,开启后可采集 Prometheus 指标数据

2 使用方式

go get github.com/ego-component/emongo

3 mongo配置

type Config struct { DSN string `json:"dsn" toml:"dsn"` // DSN DSN地址 Debug bool `json:"debug" toml:"debug"` // Debug 是否开启debug模式 DialTimeout time.Duration // 连接超时 SocketTimeout time.Duration `json:"socketTimeout" toml:"socketTimeout"` // SocketTimeout 创建连接的超时时间 MaxConnIdleTime time.Duration `json:"maxConnIdleTime"` MinPoolSize int // MinPoolSize 连接池大小(最小连接数) MaxPoolSize int `json:"maxPoolSize" toml:"maxPoolSize"` // MaxPoolSize 连接池大小(最大连接数) EnableMetricInterceptor bool `json:"enableMetricInterceptor" toml:"enableMetricInterceptor"` // EnableMetricInterceptor 是否启用prometheus metric拦截器 EnableAccessInterceptorReq bool `json:"enableAccessInterceptorReq" toml:"enableAccessInterceptorReq"` // EnableAccessInterceptorReq 是否启用access req拦截器,此配置只有在EnableAccessInterceptor=true时才会生效 EnableAccessInterceptorRes bool `json:"enableAccessInterceptorRes" toml:"enableAccessInterceptorRes"` // EnableAccessInterceptorRes 是否启用access res拦截器,此配置只有在EnableAccessInterceptor=true时才会生效 EnableAccessInterceptor bool `json:"enableAccessInterceptor" toml:"enableAccessInterceptor"` // EnableAccessInterceptor 是否启用access拦截器 EnableTraceInterceptor bool `json:"enableTraceInterceptor" toml:"enableTraceInterceptor"` // EnableTraceInterceptor 是否启用trace拦截器 SlowLogThreshold time.Duration // SlowLogThreshold 慢日志门限值,超过该门限值的请求,将被记录到慢日志中 // TLS 支持 Authentication Authentication }

4 优雅的Debug

通过开启debug配置和命令行的export EGO_DEBUG=true,我们就可以在测试环境里看到请求里的配置名、地址、耗时、请求数据、响应数据 img.png

5 用户配置

[mongo] debug=true dsn="mongodb://user:password@localhost:27017,localhost:27018" [mongo.authentication] [mongo.authentication.tls] enabled=false CAFile="" CertFile="./cert/tls.pem" KeyFile="./cert/tls.key" insecureSkipVerify=true

6 用户代码

var stopCh = make(chan bool) // 假设你配置的toml如下所示 conf := ` [mongo] debug=true dsn="mongodb://user:password@localhost:27017,localhost:27018" ` // 加载配置文件 err := econf.LoadFromReader(strings.NewReader(conf), toml.Unmarshal) if err != nil { panic("LoadFromReader fail," + err.Error()) } // 初始化emongo组件 cmp := emongo.Load("mongo").Build() coll := cmp.Client.Database("test").Collection("cells") findOne(coll) stopCh <- true

About

emongo,mongo,ego

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages