for test
This commit is contained in:
commit
1685ff59ff
71
logdb.go
Normal file
71
logdb.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package logdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// LogDB 属性结构
|
||||
type LogDB struct {
|
||||
Charset string `yaml:"charset"`
|
||||
DB string `yaml:"db"`
|
||||
Hosts []string `yaml:"hosts"`
|
||||
Password string `yaml:"password"`
|
||||
Port string `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
|
||||
hostid int
|
||||
driver *sql.DB
|
||||
errorcount int
|
||||
}
|
||||
|
||||
// NewLogDB 创建一个logdb的配置
|
||||
func NewLogDB(filename string) *LogDB {
|
||||
logdb := LogDB{}
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = yaml.Unmarshal(data, &logdb)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logdb.hostid = 0
|
||||
|
||||
logdb.Connect()
|
||||
return &logdb
|
||||
}
|
||||
|
||||
// Connect 重连
|
||||
func (logdb *LogDB) Connect() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
hostlen := len(logdb.Hosts)
|
||||
for i := 0; i < hostlen; i++ {
|
||||
|
||||
logdb.errorcount++
|
||||
if logdb.errorcount >= hostlen*2 {
|
||||
panic(err)
|
||||
}
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", logdb.User, logdb.Password, logdb.Hosts[logdb.hostid], logdb.Port, logdb.DB))
|
||||
if err != nil {
|
||||
log.Println(err, logdb.Hosts[logdb.hostid], " is connect fail")
|
||||
continue
|
||||
}
|
||||
logdb.driver = db
|
||||
logdb.hostid = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", logdb.User, logdb.Password, logdb.Hosts[logdb.hostid], logdb.Port, logdb.DB))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logdb.driver = db
|
||||
}
|
6
logdb.yaml
Normal file
6
logdb.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
charset: utf8mb4
|
||||
db: test_log
|
||||
hosts: [192.168.6.101,192.168.6.102,192.168.6.103,192.168.6.104,192.168.6.105]
|
||||
password: ag-spider-log
|
||||
port: 4000
|
||||
user: spider
|
20
logdb_test.go
Normal file
20
logdb_test.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package logdb
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// charset: utf8mb4
|
||||
// db: test_log
|
||||
// hosts: [192.168.6.101, 192.168.6.102, 192.168.6.103, 192.168.6.104, 192.168.6.105]
|
||||
// password: ag-spider-log
|
||||
// port: 4000
|
||||
// user: spider
|
||||
|
||||
func TestSelect(t *testing.T) {
|
||||
logdb := NewLogDB("logdb.yaml")
|
||||
t.Error(logdb)
|
||||
logdb.Connect()
|
||||
}
|
Loading…
Reference in New Issue
Block a user