gitee.com/woood2/luca@v1.0.4/internal/conf/attr.go (about) 1 package conf 2 3 import ( 4 "gitee.com/woood2/luca/internal/util" 5 "github.com/hashicorp/consul/api" 6 "gopkg.in/yaml.v2" 7 "log" 8 "path/filepath" 9 ) 10 11 const ( 12 DevEnv = "dev" 13 DemoEnv = "demo" 14 TestEnv = "test" 15 PreEnv = "pre" 16 ProEnv = "pro" 17 ) 18 19 const ( 20 ConfigTypeFile = "file" 21 ConfigTypeConsul = "consul" 22 ) 23 24 func IsPro(env string) bool { 25 return env == ProEnv 26 } 27 28 type Config struct { 29 //stub part.总是在application.yml 30 Host string `yaml:"host"` 31 ConfigType string `yaml:"configType"` //file|consul 32 DataKey string `yaml:"dataKey"` //consul kv key, such as "config/luca/attr" 33 Consul *Consul `yaml:"consul"` 34 //detail part.当configType=file,下列配置项位于application.yml;当configType=consul,下列配置项位于consul kv存储 35 Project string `yaml:"project"` 36 Env string `yaml:"env"` 37 ConsoleLog bool `yaml:"consoleLog"` //本地dev通常使用console,远程dev通常使用file。且本地经常需要切换试验。 38 //components 39 Pprof *Pprof `yaml:"pprof"` 40 Mysql *Mysql `yaml:"mysql"` 41 Mongo *Mongo `yaml:"mongo"` 42 Redis *Redis `yaml:"redis"` 43 Zipkin *Zipkin `yaml:"zipkin"` 44 Kafka *Kafka `yaml:"kafka"` 45 //entries 46 Backend *Restful `yaml:"backend"` 47 Micro *Micro `yaml:"micro"` 48 Consumer *Consumer `yaml:"consumer"` 49 Cron *Cron `yaml:"cron"` 50 } 51 52 type Pprof struct { 53 User string `yaml:"user"` 54 Pwd string `yaml:"pwd"` 55 } 56 57 type Mysql struct { 58 User string `yaml:"user"` 59 Pwd string `yaml:"pwd"` 60 Host string `yaml:"host"` 61 Port int `yaml:"port"` 62 DB string `yaml:"db"` 63 MaxOpenConns int `yaml:"maxOpenConns"` //yml中若不设置,则最大连接数无限制 64 MaxIdleConns int `yaml:"maxIdleConns"` //yml中若不设置,则不保持任何空闲连接 65 } 66 67 type Mongo struct { 68 User string `yaml:"user"` 69 Pwd string `yaml:"pwd"` 70 Host string `yaml:"host"` 71 Port int `yaml:"port"` 72 DB string `yaml:"db"` 73 MaxPoolSize int `yaml:"maxPoolSize"` //yml中若不设置,则最大连接数为math.MaxInt64,等于无限制 74 MinPoolSize int `yaml:"minPoolSize"` //yml中若不设置,则最小连接数为0,允许不保持任何空闲连接 75 } 76 77 type Redis struct { 78 /** 79 single-mode ":7001" 80 cluster-mode ":7001,:7002,:7003" 81 */ 82 Addr string `yaml:"addr"` 83 Pwd string `yaml:"pwd"` 84 PoolSize int `yaml:"poolSize"` //yml中若不设置,则最大连接数为 10 * runtime.NumCPU() 85 MinIdleConns int `yaml:"minIdleConns"` //yml中若不设置,则最小空闲连接数为0,允许不保持任何空闲连接 86 } 87 88 type Zipkin struct { 89 ReporterAddr string `yaml:"reporterAddr"` 90 } 91 92 type Consul struct { 93 Host string `yaml:"host"` 94 Port int `yaml:"port"` 95 } 96 97 type Kafka struct { 98 Version string `yaml:"version"` 99 Brokers []string `yaml:"brokers"` 100 EnableSASL bool `yaml:"enableSASL"` 101 User string `yaml:"user"` 102 Password string `yaml:"password"` 103 Algorithm string `yaml:"algorithm"` //"sha512" | "sha256" 104 } 105 106 type Restful struct { 107 Addr string `yaml:"addr"` 108 Register bool `yaml:"register"` 109 PprofAddr string `yaml:"pprofAddr"` 110 MetricsAddr string `yaml:"metricsAddr"` 111 HystrixPort int `yaml:"hystrixPort"` 112 /** 113 multi: ["http://google.com", "http://facebook.com"] 114 all: ["*"] 115 */ 116 AllowOrigins []string `yaml:"allowOrigins"` 117 } 118 119 type Micro struct { 120 Port int `yaml:"port"` 121 Register bool `yaml:"register"` 122 PprofAddr string `yaml:"pprofAddr"` 123 MetricsAddr string `yaml:"metricsAddr"` 124 HystrixPort int `yaml:"hystrixPort"` 125 } 126 127 type Consumer struct { 128 PprofAddr string `yaml:"pprofAddr"` 129 MetricsAddr string `yaml:"metricsAddr"` 130 HystrixPort int `yaml:"hystrixPort"` 131 } 132 133 type Cron struct { 134 PprofAddr string `yaml:"pprofAddr"` 135 HystrixPort int `yaml:"hystrixPort"` 136 } 137 138 func Load(relativePaths ...string) (cnf *Config) { 139 var c = new(Config) 140 for _, path := range relativePaths { 141 p := []string{path} 142 for i := 0; i < 10; i++ { 143 if i > 0 { 144 p = append([]string{".."}, p...) 145 } 146 f := filepath.Join(p...) 147 err := util.LoadYAML(f, &c) 148 if err == nil { 149 log.Printf("%s loaded successfully\n", f) 150 return c 151 } 152 } 153 } 154 log.Panicln("Failed to load configuration file") 155 return nil 156 } 157 158 func MergeConsul(attr *Config, client *api.Client) { 159 if attr.ConfigType == ConfigTypeConsul { 160 data, _, _ := client.KV().Get(attr.DataKey, nil) 161 if data == nil { 162 log.Panicf("Consul key does not exist: %s\n", attr.DataKey) 163 } 164 dataString := string(data.Value) 165 detail := new(Config) 166 if err := yaml.Unmarshal([]byte(dataString), &detail); err != nil { 167 log.Panicf("yaml.Unmarshal(%s, ...) error: %v\n", dataString, err) 168 } 169 attr.Project = detail.Project 170 attr.Env = detail.Env 171 attr.ConsoleLog = detail.ConsoleLog 172 attr.Pprof = detail.Pprof 173 174 attr.Mysql = detail.Mysql 175 attr.Mongo = detail.Mongo 176 attr.Redis = detail.Redis 177 attr.Zipkin = detail.Zipkin 178 attr.Kafka = detail.Kafka 179 180 attr.Backend = detail.Backend 181 attr.Micro = detail.Micro 182 attr.Consumer = detail.Consumer 183 attr.Cron = detail.Cron 184 log.Printf("Consul data merged, key=%s\n", attr.DataKey) 185 } else if attr.ConfigType != ConfigTypeFile { 186 log.Panicf("Invalid configType: %s\n", attr.ConfigType) 187 } 188 }