init.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package vehicle
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "gd_service/common.in/clinit"
  8. "gd_service/common.in/config"
  9. "github.com/smallnest/rpcx/client"
  10. "github.com/smallnest/rpcx/protocol"
  11. "github.com/smallnest/rpcx/share"
  12. "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. projectName := cfg.Section("").Key("project_name").String()
  25. // 先行于读配置
  26. clinit.InitEtcd(etcdAddrs)
  27. conf := config.GetConfig(projectName+"/"+runmode, encryptKey, clinit.GetEtcdClient())
  28. if conf == nil {
  29. fmt.Printf("get conf failed\n\n")
  30. os.Exit(1)
  31. }
  32. opt := client.Option{
  33. Retries: 1,
  34. RPCPath: share.DefaultRPCPath,
  35. ConnectTimeout: 10 * time.Second,
  36. SerializeType: protocol.JSON,
  37. CompressType: protocol.None,
  38. BackupLatency: 10 * time.Millisecond,
  39. }
  40. d := client.NewEtcdDiscovery(conf.Rpc.BasePath, conf.Rpc.Vehicle.Name, etcdAddrs, nil)
  41. xcli = client.NewXClient(conf.Rpc.Vehicle.Name, client.Failtry, client.RandomSelect, d, opt)
  42. }