init.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package order
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "gd_vehicle/common.in/clinit"
  8. "gd_vehicle/common.in/config"
  9. "github.com/smallnest/rpcx/client"
  10. "github.com/smallnest/rpcx/protocol"
  11. "github.com/smallnest/rpcx/share"
  12. ini "gopkg.in/ini.v1"
  13. )
  14. var xcli client.XClient
  15. func init() {
  16. cfg, err := ini.Load("../../conf/app.conf")
  17. if err != nil {
  18. fmt.Printf("Fail to read file: %v\n\n", err)
  19. os.Exit(1)
  20. }
  21. runmode := cfg.Section("").Key("runmode").String()
  22. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  23. encryptKey := cfg.Section("").Key("encrypt_key").String()
  24. // 先行于读配置
  25. clinit.InitEtcd(etcdAddrs)
  26. conf := config.GetConfig("/gd/"+runmode, encryptKey, clinit.GetEtcdClient())
  27. if conf == nil {
  28. fmt.Printf("get conf failed\n\n")
  29. os.Exit(1)
  30. }
  31. opt := client.Option{
  32. Retries: 1,
  33. RPCPath: share.DefaultRPCPath,
  34. ConnectTimeout: 10 * time.Second,
  35. SerializeType: protocol.JSON,
  36. CompressType: protocol.None,
  37. BackupLatency: 10 * time.Millisecond,
  38. }
  39. d := client.NewEtcdDiscovery(conf.Rpc.BasePath, conf.Rpc.Vehicle.Name, etcdAddrs, nil)
  40. xcli = client.NewXClient(conf.Rpc.Vehicle.Name, client.Failtry, client.RandomSelect, d, opt)
  41. }