init
This commit is contained in:
		
						commit
						32bad0506b
					
				|  | @ -0,0 +1,8 @@ | ||||||
|  | # 默认忽略的文件 | ||||||
|  | /shelf/ | ||||||
|  | /workspace.xml | ||||||
|  | # 基于编辑器的 HTTP 客户端请求 | ||||||
|  | /httpRequests/ | ||||||
|  | # Datasource local storage ignored files | ||||||
|  | /dataSources/ | ||||||
|  | /dataSources.local.xml | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <module type="WEB_MODULE" version="4"> | ||||||
|  |   <component name="Go" enabled="true" /> | ||||||
|  |   <component name="NewModuleRootManager"> | ||||||
|  |     <content url="file://$MODULE_DIR$" /> | ||||||
|  |     <orderEntry type="inheritedJdk" /> | ||||||
|  |     <orderEntry type="sourceFolder" forTests="false" /> | ||||||
|  |   </component> | ||||||
|  | </module> | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | <component name="ProjectDictionaryState"> | ||||||
|  |   <dictionary name="wangjian" /> | ||||||
|  | </component> | ||||||
|  | @ -0,0 +1,6 @@ | ||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="ThriftCompiler"> | ||||||
|  |     <compilers /> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
|  | @ -0,0 +1,8 @@ | ||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="ProjectModuleManager"> | ||||||
|  |     <modules> | ||||||
|  |       <module fileurl="file://$PROJECT_DIR$/.idea/data2minio.iml" filepath="$PROJECT_DIR$/.idea/data2minio.iml" /> | ||||||
|  |     </modules> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
|  | @ -0,0 +1,6 @@ | ||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="VcsDirectoryMappings"> | ||||||
|  |     <mapping directory="" vcs="Git" /> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -0,0 +1,100 @@ | ||||||
|  | package cmd | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"fmt" | ||||||
|  | 	"github.com/spf13/cobra" | ||||||
|  | 	"os" | ||||||
|  | 	"os/signal" | ||||||
|  | 	"syscall" | ||||||
|  | 
 | ||||||
|  | 	"go.uber.org/zap" | ||||||
|  | 
 | ||||||
|  | 	"data_minio/config" | ||||||
|  | 	"data_minio/global" | ||||||
|  | 	"data_minio/model" | ||||||
|  | 	"data_minio/mq" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var ( | ||||||
|  | 	ConfigFileFlag string = "./config/config.yaml" | ||||||
|  | 	ConsulAddress  string = "http://localhost:8500" | ||||||
|  | 	NodeName       string = "data_minio" | ||||||
|  | 	Mode           string = "dev" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func NewStartCmd() *cobra.Command { | ||||||
|  | 	cmd := &cobra.Command{ | ||||||
|  | 		Use:   "start", | ||||||
|  | 		Short: "Start folder monitor", | ||||||
|  | 		Run: func(cmd *cobra.Command, args []string) { | ||||||
|  | 			var ( | ||||||
|  | 				cfg *config.DataMinioConfig | ||||||
|  | 				err error | ||||||
|  | 			) | ||||||
|  | 			ctx, cancel := context.WithCancel(context.Background()) | ||||||
|  | 			defer cancel() | ||||||
|  | 			global.Must(err) | ||||||
|  | 			configFileFlag, err := cmd.Flags().GetString("c") | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Println("get local config err: ", err) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			NodeName, err = cmd.Flags().GetString("n") | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Println("get remote path config err: ", err) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			Mode, err = cmd.Flags().GetString("m") | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Println("get remote path config err: ", err) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			if len(configFileFlag) > 1 { | ||||||
|  | 				cfg, err = config.ParseConfigByFile(configFileFlag) | ||||||
|  | 				global.Must(err) | ||||||
|  | 				err = config.UpdateRemoteConfig(cfg) | ||||||
|  | 				global.Must(err) | ||||||
|  | 				ConfigFileFlag = configFileFlag | ||||||
|  | 			} else { | ||||||
|  | 				//获取consul注册中心的配置文件
 | ||||||
|  | 				cfg, err = config.GetRemoteConfig(ConsulAddress, fmt.Sprintf("hpds-pavement/hpds_web/%s/%s", Mode, NodeName)) | ||||||
|  | 				global.Must(err) | ||||||
|  | 				err = config.UpdateLocalConfig(cfg, ConfigFileFlag) | ||||||
|  | 			} | ||||||
|  | 			global.Cfg = cfg | ||||||
|  | 			global.Logger = config.LoadLoggerConfig(cfg.Logging) | ||||||
|  | 
 | ||||||
|  | 			//创建消息连接点
 | ||||||
|  | 			mq.MqList, err = mq.NewMqClient(cfg.Funcs, cfg.Node) | ||||||
|  | 			global.Must(err) | ||||||
|  | 			//连接数据库
 | ||||||
|  | 			global.DB = model.New(cfg.Db.DriveName, cfg.Db.Conn, cfg.Mode == "dev") | ||||||
|  | 			// 退出channel
 | ||||||
|  | 			exitChannel := make(chan os.Signal) | ||||||
|  | 			defer close(exitChannel) | ||||||
|  | 			// 退出信号监听
 | ||||||
|  | 			go func(c chan os.Signal) { | ||||||
|  | 				signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | ||||||
|  | 			}(exitChannel) | ||||||
|  | 			select { | ||||||
|  | 			case <-ctx.Done(): | ||||||
|  | 				global.Logger.With( | ||||||
|  | 					zap.String(cfg.Name, "exit"), | ||||||
|  | 				).Error(ctx.Err().Error()) | ||||||
|  | 				return | ||||||
|  | 			case errs := <-exitChannel: | ||||||
|  | 				global.Logger.With( | ||||||
|  | 					zap.String(cfg.Name, "服务退出"), | ||||||
|  | 				).Info(errs.String()) | ||||||
|  | 				os.Exit(-1) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path") | ||||||
|  | 	cmd.Flags().StringVar(&NodeName, "n", "data_minio", "The configuration name") | ||||||
|  | 	cmd.Flags().StringVar(&Mode, "m", "dev", "run mode : dev | test | releases") | ||||||
|  | 	return cmd | ||||||
|  | } | ||||||
|  | @ -0,0 +1,138 @@ | ||||||
|  | package config | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 
 | ||||||
|  | 	"git.hpds.cc/Component/logging" | ||||||
|  | 	consulApi "github.com/hashicorp/consul/api" | ||||||
|  | 	_ "github.com/spf13/viper/remote" | ||||||
|  | 	yaml "gopkg.in/yaml.v3" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type DataMinioConfig struct { | ||||||
|  | 	Name    string       `yaml:"name" json:"name" toml:"name"` | ||||||
|  | 	Mode    string       `yaml:"mode" json:"mode"` | ||||||
|  | 	Consul  ConsulConfig `yaml:"consul,omitempty"` | ||||||
|  | 	Db      DbConfig     `yaml:"db"` | ||||||
|  | 	Logging LogOptions   `yaml:"logging"` | ||||||
|  | 	Minio   MinioConfig  `yaml:"minio"` | ||||||
|  | 	Node    HpdsNode     `yaml:"node,omitempty"` | ||||||
|  | 	Funcs   []FuncConfig `yaml:"functions,omitempty"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type ConsulConfig struct { | ||||||
|  | 	Host       string   `yaml:"host,omitempty"` | ||||||
|  | 	Port       int      `yaml:"port,omitempty"` | ||||||
|  | 	Interval   int      `yaml:"interval,omitempty"` | ||||||
|  | 	Timeout    int      `yaml:"timeout,omitempty"` | ||||||
|  | 	Deregister int      `yaml:"deregister,omitempty"` | ||||||
|  | 	Tags       []string `yaml:"tags,omitempty"` | ||||||
|  | } | ||||||
|  | type DbConfig struct { | ||||||
|  | 	Conn      string `yaml:"conn"` | ||||||
|  | 	DriveName string `yaml:"drive_name"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type LogOptions struct { | ||||||
|  | 	Path            string `yaml:"path" json:"path" toml:"path"`                                  // 文件保存地方
 | ||||||
|  | 	Prefix          string `yaml:"prefix" json:"prefix" toml:"prefix"`                            // 日志文件前缀
 | ||||||
|  | 	ErrorFileSuffix string `yaml:"errorFileSuffix" json:"errorFileSuffix" toml:"errorFileSuffix"` // error日志文件后缀
 | ||||||
|  | 	WarnFileSuffix  string `yaml:"warnFileSuffix" json:"warnFileSuffix" toml:"warnFileSuffix"`    // warn日志文件后缀
 | ||||||
|  | 	InfoFileSuffix  string `yaml:"infoFileSuffix" json:"infoFileSuffix" toml:"infoFileSuffix"`    // info日志文件后缀
 | ||||||
|  | 	DebugFileSuffix string `yaml:"debugFileSuffix" json:"debugFileSuffix" toml:"debugFileSuffix"` // debug日志文件后缀
 | ||||||
|  | 	Level           string `yaml:"level" json:"level" toml:"level"`                               // 日志等级
 | ||||||
|  | 	MaxSize         int    `yaml:"maxSize" json:"maxSize" toml:"maxSize"`                         // 日志文件大小(M)
 | ||||||
|  | 	MaxBackups      int    `yaml:"maxBackups" json:"maxBackups" toml:"maxBackups"`                // 最多存在多少个切片文件
 | ||||||
|  | 	MaxAge          int    `yaml:"maxAge" json:"maxAge" toml:"maxAge"`                            // 保存的最大天数
 | ||||||
|  | 	Development     bool   `yaml:"development" json:"development" toml:"development"`             // 是否是开发模式
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type MinioConfig struct { | ||||||
|  | 	Protocol        string `yaml:"protocol"` //http or https
 | ||||||
|  | 	Endpoint        string `yaml:"endpoint"` | ||||||
|  | 	AccessKeyId     string `yaml:"accessKeyId"` | ||||||
|  | 	SecretAccessKey string `yaml:"secretAccessKey"` | ||||||
|  | 	Bucket          string `yaml:"bucket"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type HpdsNode struct { | ||||||
|  | 	Host  string `yaml:"host"` | ||||||
|  | 	Port  int    `yaml:"port"` | ||||||
|  | 	Token string `yaml:"token,omitempty"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type FuncConfig struct { | ||||||
|  | 	Name    string `yaml:"name"` | ||||||
|  | 	DataTag uint8  `yaml:"dataTag"` | ||||||
|  | 	MqType  uint   `yaml:"mqType"` //消息类型, 发布,1;订阅;2
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func ParseConfigByFile(path string) (cfg *DataMinioConfig, err error) { | ||||||
|  | 	buffer, err := os.ReadFile(path) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	return load(buffer) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func load(buf []byte) (cfg *DataMinioConfig, err error) { | ||||||
|  | 
 | ||||||
|  | 	cfg = new(DataMinioConfig) | ||||||
|  | 	cfg.Funcs = make([]FuncConfig, 0) | ||||||
|  | 	err = yaml.Unmarshal(buf, cfg) | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func UpdateLocalConfig(cfg *DataMinioConfig, fn string) error { | ||||||
|  | 	data, err := yaml.Marshal(cfg) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	err = os.WriteFile(fn, data, 0600) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func UpdateRemoteConfig(cfg *DataMinioConfig) error { | ||||||
|  | 	consulClient, err := consulApi.NewClient(&consulApi.Config{Address: fmt.Sprintf("%s:%d", cfg.Consul.Host, cfg.Consul.Port)}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	val, err := yaml.Marshal(cfg) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	p := &consulApi.KVPair{Key: fmt.Sprintf("hpds-pavement/hpds_data_minio/%s/%s", cfg.Mode, cfg.Name), Value: val} | ||||||
|  | 	if _, err = consulClient.KV().Put(p, nil); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetRemoteConfig(remoteAddr, path string) (cfg *DataMinioConfig, err error) { | ||||||
|  | 	consulClient, err := consulApi.NewClient(&consulApi.Config{Address: remoteAddr}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	kv, _, err := consulClient.KV().Get(path, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	return load(kv.Value) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func LoadLoggerConfig(opt LogOptions) *logging.Logger { | ||||||
|  | 	return logging.NewLogger( | ||||||
|  | 		logging.SetPath(opt.Path), | ||||||
|  | 		logging.SetPrefix(opt.Prefix), | ||||||
|  | 		logging.SetDevelopment(opt.Development), | ||||||
|  | 		logging.SetDebugFileSuffix(opt.DebugFileSuffix), | ||||||
|  | 		logging.SetWarnFileSuffix(opt.WarnFileSuffix), | ||||||
|  | 		logging.SetErrorFileSuffix(opt.ErrorFileSuffix), | ||||||
|  | 		logging.SetInfoFileSuffix(opt.InfoFileSuffix), | ||||||
|  | 		logging.SetMaxAge(opt.MaxAge), | ||||||
|  | 		logging.SetMaxBackups(opt.MaxBackups), | ||||||
|  | 		logging.SetMaxSize(opt.MaxSize), | ||||||
|  | 		logging.SetLevel(logging.LogLevel["debug"]), | ||||||
|  | 	) | ||||||
|  | } | ||||||
|  | @ -0,0 +1,36 @@ | ||||||
|  | name: data_minio | ||||||
|  | mode: dev | ||||||
|  | consul: | ||||||
|  |   host: http://consul.hpds.cc | ||||||
|  |   port: 80 | ||||||
|  |   interval: 300 | ||||||
|  |   timeout: 5 | ||||||
|  |   deregister: 1 | ||||||
|  | db: | ||||||
|  |   conn: root:123456@tcp(127.0.0.1:3306)/hpds_jky?charset=utf8mb4 | ||||||
|  |   drive_name: mysql | ||||||
|  | minio: | ||||||
|  |   protocol: http | ||||||
|  |   endpoint: 127.0.0.1:9000 | ||||||
|  |   accessKeyId: root | ||||||
|  |   secretAccessKey: OIxv7QptYBO3 | ||||||
|  |   bucket: jky-data | ||||||
|  | node: | ||||||
|  |   host: 127.0.0.1 | ||||||
|  |   port: 27188 | ||||||
|  |   token: 06d36c6f5705507dae778fdce90d0767 | ||||||
|  | functions: | ||||||
|  |   - name: dataset-request | ||||||
|  |     dataTag : 10 | ||||||
|  |     mqType: 2 | ||||||
|  | logging: | ||||||
|  |   path: ./logs | ||||||
|  |   prefix: data_minio | ||||||
|  |   errorFileSuffix: error.log | ||||||
|  |   warnFileSuffix: warn.log | ||||||
|  |   infoFileSuffix: info.log | ||||||
|  |   debugFileSuffix: debug.log | ||||||
|  |   maxSize: 100 | ||||||
|  |   maxBackups: 3000 | ||||||
|  |   maxAge: 30 | ||||||
|  |   development: true | ||||||
|  | @ -0,0 +1,28 @@ | ||||||
|  | package global | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 
 | ||||||
|  | 	"git.hpds.cc/Component/logging" | ||||||
|  | 	"go.uber.org/zap" | ||||||
|  | 	"xorm.io/xorm" | ||||||
|  | 
 | ||||||
|  | 	"data_minio/config" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var ( | ||||||
|  | 	DB     *xorm.Engine | ||||||
|  | 	Cfg    *config.DataMinioConfig | ||||||
|  | 	Logger *logging.Logger | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func Must(err error) { | ||||||
|  | 	if err != nil { | ||||||
|  | 		_, _ = fmt.Fprint(os.Stderr, err) | ||||||
|  | 		Logger.With( | ||||||
|  | 			zap.String("应用名称", Cfg.Name), | ||||||
|  | 		).Error(err.Error()) | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,107 @@ | ||||||
|  | module data_minio | ||||||
|  | 
 | ||||||
|  | go 1.19 | ||||||
|  | 
 | ||||||
|  | require ( | ||||||
|  | 	git.hpds.cc/Component/logging v0.0.0-20230106105738-e378e873921b | ||||||
|  | 	git.hpds.cc/pavement/hpds_node v0.0.0-20230326152949-a1c0ad2f7052 | ||||||
|  | 	github.com/go-sql-driver/mysql v1.7.0 | ||||||
|  | 	github.com/hashicorp/consul/api v1.20.0 | ||||||
|  | 	github.com/klauspost/compress v1.16.3 | ||||||
|  | 	github.com/minio/minio-go/v7 v7.0.50 | ||||||
|  | 	github.com/spf13/cobra v1.6.1 | ||||||
|  | 	github.com/spf13/viper v1.15.0 | ||||||
|  | 	go.uber.org/zap v1.24.0 | ||||||
|  | 	gopkg.in/yaml.v3 v3.0.1 | ||||||
|  | 	xorm.io/xorm v1.3.2 | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | require ( | ||||||
|  | 	cloud.google.com/go v0.105.0 // indirect | ||||||
|  | 	cloud.google.com/go/compute v1.14.0 // indirect | ||||||
|  | 	cloud.google.com/go/compute/metadata v0.2.3 // indirect | ||||||
|  | 	cloud.google.com/go/firestore v1.9.0 // indirect | ||||||
|  | 	cloud.google.com/go/longrunning v0.3.0 // indirect | ||||||
|  | 	git.hpds.cc/Component/mq_coder v0.0.0-20221010064749-174ae7ae3340 // indirect | ||||||
|  | 	git.hpds.cc/Component/network v0.0.0-20230326151855-3c157f531d86 // indirect | ||||||
|  | 	github.com/armon/go-metrics v0.4.0 // indirect | ||||||
|  | 	github.com/coreos/go-semver v0.3.0 // indirect | ||||||
|  | 	github.com/coreos/go-systemd/v22 v22.3.2 // indirect | ||||||
|  | 	github.com/dustin/go-humanize v1.0.1 // indirect | ||||||
|  | 	github.com/fatih/color v1.13.0 // indirect | ||||||
|  | 	github.com/fsnotify/fsnotify v1.6.0 // indirect | ||||||
|  | 	github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect | ||||||
|  | 	github.com/goccy/go-json v0.8.1 // indirect | ||||||
|  | 	github.com/gogo/protobuf v1.3.2 // indirect | ||||||
|  | 	github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||||||
|  | 	github.com/golang/mock v1.6.0 // indirect | ||||||
|  | 	github.com/golang/protobuf v1.5.2 // indirect | ||||||
|  | 	github.com/golang/snappy v0.0.4 // indirect | ||||||
|  | 	github.com/google/go-cmp v0.5.9 // indirect | ||||||
|  | 	github.com/google/uuid v1.3.0 // indirect | ||||||
|  | 	github.com/googleapis/enterprise-certificate-proxy v0.2.1 // indirect | ||||||
|  | 	github.com/googleapis/gax-go/v2 v2.7.0 // indirect | ||||||
|  | 	github.com/hashicorp/go-cleanhttp v0.5.2 // indirect | ||||||
|  | 	github.com/hashicorp/go-hclog v1.2.0 // indirect | ||||||
|  | 	github.com/hashicorp/go-immutable-radix v1.3.1 // indirect | ||||||
|  | 	github.com/hashicorp/go-rootcerts v1.0.2 // indirect | ||||||
|  | 	github.com/hashicorp/golang-lru v0.5.4 // indirect | ||||||
|  | 	github.com/hashicorp/hcl v1.0.0 // indirect | ||||||
|  | 	github.com/hashicorp/serf v0.10.1 // indirect | ||||||
|  | 	github.com/inconshreveable/mousetrap v1.0.1 // indirect | ||||||
|  | 	github.com/json-iterator/go v1.1.12 // indirect | ||||||
|  | 	github.com/klauspost/cpuid/v2 v2.2.4 // indirect | ||||||
|  | 	github.com/lucas-clemente/quic-go v0.29.1 // indirect | ||||||
|  | 	github.com/magiconair/properties v1.8.7 // indirect | ||||||
|  | 	github.com/marten-seemann/qtls-go1-18 v0.1.2 // indirect | ||||||
|  | 	github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect | ||||||
|  | 	github.com/matoous/go-nanoid/v2 v2.0.0 // indirect | ||||||
|  | 	github.com/mattn/go-colorable v0.1.12 // indirect | ||||||
|  | 	github.com/mattn/go-isatty v0.0.14 // indirect | ||||||
|  | 	github.com/minio/md5-simd v1.1.2 // indirect | ||||||
|  | 	github.com/minio/sha256-simd v1.0.0 // indirect | ||||||
|  | 	github.com/mitchellh/go-homedir v1.1.0 // indirect | ||||||
|  | 	github.com/mitchellh/mapstructure v1.5.0 // indirect | ||||||
|  | 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||||||
|  | 	github.com/modern-go/reflect2 v1.0.2 // indirect | ||||||
|  | 	github.com/nxadm/tail v1.4.8 // indirect | ||||||
|  | 	github.com/onsi/ginkgo v1.16.4 // indirect | ||||||
|  | 	github.com/pelletier/go-toml/v2 v2.0.6 // indirect | ||||||
|  | 	github.com/pkg/errors v0.9.1 // indirect | ||||||
|  | 	github.com/rs/xid v1.4.0 // indirect | ||||||
|  | 	github.com/sagikazarmark/crypt v0.9.0 // indirect | ||||||
|  | 	github.com/sirupsen/logrus v1.9.0 // indirect | ||||||
|  | 	github.com/spf13/afero v1.9.3 // indirect | ||||||
|  | 	github.com/spf13/cast v1.5.0 // indirect | ||||||
|  | 	github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||||||
|  | 	github.com/spf13/pflag v1.0.5 // indirect | ||||||
|  | 	github.com/subosito/gotenv v1.4.2 // indirect | ||||||
|  | 	github.com/syndtr/goleveldb v1.0.0 // indirect | ||||||
|  | 	go.etcd.io/etcd/api/v3 v3.5.6 // indirect | ||||||
|  | 	go.etcd.io/etcd/client/pkg/v3 v3.5.6 // indirect | ||||||
|  | 	go.etcd.io/etcd/client/v2 v2.305.6 // indirect | ||||||
|  | 	go.etcd.io/etcd/client/v3 v3.5.6 // indirect | ||||||
|  | 	go.opencensus.io v0.24.0 // indirect | ||||||
|  | 	go.uber.org/atomic v1.9.0 // indirect | ||||||
|  | 	go.uber.org/multierr v1.8.0 // indirect | ||||||
|  | 	golang.org/x/crypto v0.6.0 // indirect | ||||||
|  | 	golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect | ||||||
|  | 	golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect | ||||||
|  | 	golang.org/x/net v0.7.0 // indirect | ||||||
|  | 	golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect | ||||||
|  | 	golang.org/x/sync v0.1.0 // indirect | ||||||
|  | 	golang.org/x/sys v0.5.0 // indirect | ||||||
|  | 	golang.org/x/text v0.7.0 // indirect | ||||||
|  | 	golang.org/x/time v0.1.0 // indirect | ||||||
|  | 	golang.org/x/tools v0.1.12 // indirect | ||||||
|  | 	golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | ||||||
|  | 	google.golang.org/api v0.107.0 // indirect | ||||||
|  | 	google.golang.org/appengine v1.6.7 // indirect | ||||||
|  | 	google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect | ||||||
|  | 	google.golang.org/grpc v1.52.0 // indirect | ||||||
|  | 	google.golang.org/protobuf v1.28.1 // indirect | ||||||
|  | 	gopkg.in/ini.v1 v1.67.0 // indirect | ||||||
|  | 	gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect | ||||||
|  | 	gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect | ||||||
|  | 	xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect | ||||||
|  | ) | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | {"level":"error","ts":"2023-04-01 14:10:11.510","caller":"mq/index.go:28","msg":"启动错误","数据落地服务":"错误信息","error":"timeout: no recent network activity","stacktrace":"data_minio/mq.must\n\t/Users/wangjian/GoPath/hpds.cc/data_minio/mq/index.go:28\ndata_minio/mq.NewMqClient\n\t/Users/wangjian/GoPath/hpds.cc/data_minio/mq/index.go:52\ndata_minio/cmd.NewStartCmd.func1\n\t/Users/wangjian/GoPath/hpds.cc/data_minio/cmd/server.go:70\ngithub.com/spf13/cobra.(*Command).execute\n\t/Users/wangjian/go/pkg/mod/github.com/spf13/cobra@v0.0.3/command.go:766\ngithub.com/spf13/cobra.(*Command).ExecuteC\n\t/Users/wangjian/go/pkg/mod/github.com/spf13/cobra@v0.0.3/command.go:852\ngithub.com/spf13/cobra.(*Command).Execute\n\t/Users/wangjian/go/pkg/mod/github.com/spf13/cobra@v0.0.3/command.go:800\nmain.main\n\t/Users/wangjian/GoPath/hpds.cc/data_minio/main.go:25\nruntime.main\n\t/usr/local/opt/go/libexec/src/runtime/proc.go:250"} | ||||||
|  | @ -0,0 +1,4 @@ | ||||||
|  | {"level":"info","ts":"2023-04-01 14:29:45.672","caller":"cmd/server.go:90","msg":"interrupt","data_minio":"服务退出"} | ||||||
|  | {"level":"info","ts":"2023-04-01 15:32:47.647","caller":"cmd/server.go:90","msg":"interrupt","data_minio":"服务退出"} | ||||||
|  | {"level":"info","ts":"2023-04-01 15:40:47.862","caller":"cmd/server.go:90","msg":"interrupt","data_minio":"服务退出"} | ||||||
|  | {"level":"info","ts":"2023-04-01 16:33:29.215","caller":"cmd/server.go:90","msg":"interrupt","data_minio":"服务退出"} | ||||||
|  | @ -0,0 +1,29 @@ | ||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 
 | ||||||
|  | 	"github.com/spf13/cobra" | ||||||
|  | 
 | ||||||
|  | 	"data_minio/cmd" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var ( | ||||||
|  | 	rootCmd = &cobra.Command{ | ||||||
|  | 		Use:     "data_minio", | ||||||
|  | 		Long:    "data_minio is a data to minio & database tool", | ||||||
|  | 		Version: "v 1.0", | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func init() { | ||||||
|  | 	rootCmd.AddCommand(cmd.NewStartCmd()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	if err := rootCmd.Execute(); err != nil { | ||||||
|  | 		_, _ = fmt.Fprint(os.Stderr, err.Error()) | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,23 @@ | ||||||
|  | package minio | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	minio "github.com/minio/minio-go/v7" | ||||||
|  | 	"github.com/minio/minio-go/v7/pkg/credentials" | ||||||
|  | 
 | ||||||
|  | 	"data_minio/global" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func NewMinioClient(endpoint, successKeyId, secretAccessKey string) *minio.Client { | ||||||
|  | 	opt := &minio.Options{ | ||||||
|  | 		Creds:  credentials.NewStaticV4(successKeyId, secretAccessKey, ""), | ||||||
|  | 		Secure: false, | ||||||
|  | 	} | ||||||
|  | 	minioClient, err := minio.New(endpoint, opt) | ||||||
|  | 	if err != nil { | ||||||
|  | 		global.Logger.With( | ||||||
|  | 			zap.String("应用名称", Cfg.Name), | ||||||
|  | 		).Error(err.Error()) | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	return minioClient | ||||||
|  | } | ||||||
|  | @ -0,0 +1,11 @@ | ||||||
|  | package model | ||||||
|  | 
 | ||||||
|  | type DatasetDetail struct { | ||||||
|  | 	DetailId  int64  `xorm:"not null pk autoincr INT(11)" json:"detailId"`      //编号
 | ||||||
|  | 	DatasetId int64  `xorm:"INT(11) not null default 0 index" json:"datasetId"` //数据集编号
 | ||||||
|  | 	AccessUrl string `xorm:"varchar(200)" json:"accessUrl"`                     //访问地址
 | ||||||
|  | 	FileSize  int64  `xorm:"BIGINT" json:"fileSize"`                            //文件大小
 | ||||||
|  | 	FileMd5   string `xorm:"VARCHAR(64)" json:"fileMd5"`                        //文件MD5
 | ||||||
|  | 	CreateAt  int64  `xorm:"created" json:"createAt"` | ||||||
|  | 	UpdateAt  int64  `xorm:"updated" json:"updateAt"` | ||||||
|  | } | ||||||
|  | @ -0,0 +1,32 @@ | ||||||
|  | package model | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"data_minio/global" | ||||||
|  | 	"fmt" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type Dataset struct { | ||||||
|  | 	DatasetId   int64  `xorm:"not null pk autoincr INT(11)" json:"datasetId"` | ||||||
|  | 	DatasetName string `xorm:"varchar(200) not null" json:"datasetName"` | ||||||
|  | 	DatasetDesc string `xorm:"varchar(200)" json:"datasetDesc"` | ||||||
|  | 	StoreName   string `xorm:"varchar(200)" json:"storeName"` | ||||||
|  | 	CategoryId  int    `xorm:"not null SMALLINT default 1" json:"categoryId"` //业务分类, 1:道路 2:桥梁 3:隧道 4:边坡
 | ||||||
|  | 	ProjectId   int64  `xorm:"INT(11) not null default 0 index" json:"projectId"` | ||||||
|  | 	OwnerId     int64  `xorm:"INT(11) not null default 0 index" json:"ownerId"` | ||||||
|  | 	Creator     int64  `xorm:"INT(11) not null default 0 index" json:"creator"` | ||||||
|  | 	Status      int    `xorm:"SMALLINT not null default 1" json:"status"` | ||||||
|  | 	CreateAt    int64  `xorm:"created" json:"createAt"` | ||||||
|  | 	UpdateAt    int64  `xorm:"updated" json:"updateAt"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetDatasetById(id int64) (data *Dataset, err error) { | ||||||
|  | 	data = new(Dataset) | ||||||
|  | 	b, err := global.DB.ID(id).Get(data) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	if !b { | ||||||
|  | 		return nil, fmt.Errorf("未能找到对应的数据集") | ||||||
|  | 	} | ||||||
|  | 	return data, nil | ||||||
|  | } | ||||||
|  | @ -0,0 +1,14 @@ | ||||||
|  | package model | ||||||
|  | 
 | ||||||
|  | type FileManager struct { | ||||||
|  | 	FileId    int64  `xorm:"not null pk autoincr INT(11)" json:"fileId"` //文件编号
 | ||||||
|  | 	FileName  string `xorm:"VARCHAR(200)" json:"fileName"`               //文件名
 | ||||||
|  | 	AccessUrl string `xorm:"VARCHAR(400)" json:"url"`                    //访问路径
 | ||||||
|  | 	Scene     string `xorm:"VARCHAR(40)" json:"scene"`                   //应用场景,0 : 其他, 1: 道路; 2: 桥梁; 3:隧道; 4: 边坡
 | ||||||
|  | 	DatasetId int64  `xorm:"INT(11) index default 0" json:"datasetId"`   //数据集
 | ||||||
|  | 	FileSize  int64  `xorm:"BIGINT" json:"fileSize"`                     //文件大小
 | ||||||
|  | 	FileMd5   string `xorm:"VARCHAR(64)" json:"fileMd5"`                 //文件MD5
 | ||||||
|  | 	Creator   int64  `xorm:"INT(11) index" json:"creator"`               //上传人
 | ||||||
|  | 	CreateAt  int64  `xorm:"created" json:"createAt"`                    //上传时间
 | ||||||
|  | 	UpdateAt  int64  `xorm:"updated" json:"updateAt"`                    //更新时间
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,42 @@ | ||||||
|  | package model | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"os" | ||||||
|  | 
 | ||||||
|  | 	_ "github.com/go-sql-driver/mysql" | ||||||
|  | 	"go.uber.org/zap" | ||||||
|  | 	"xorm.io/xorm" | ||||||
|  | 	"xorm.io/xorm/dialects" | ||||||
|  | 
 | ||||||
|  | 	"data_minio/global" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type Statistics struct { | ||||||
|  | 	Total int64 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func New(driveName, dsn string, showSql bool) (db *xorm.Engine) { | ||||||
|  | 	db, _ = NewDbConnection(driveName, dsn) | ||||||
|  | 	db.ShowSQL(showSql) | ||||||
|  | 	db.Dialect().SetQuotePolicy(dialects.QuotePolicyReserved) | ||||||
|  | 	err := db.Sync2( | ||||||
|  | 		&Dataset{}, | ||||||
|  | 		&DatasetDetail{}, | ||||||
|  | 		&FileManager{}, | ||||||
|  | 	) | ||||||
|  | 	if err != nil { | ||||||
|  | 		global.Logger.Error("同步数据库表结构", zap.Error(err)) | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
|  | 	return db | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewDbConnection(driveName, dsn string) (db *xorm.Engine, err error) { | ||||||
|  | 	db, err = xorm.NewEngine(driveName, dsn) | ||||||
|  | 	if err != nil { | ||||||
|  | 		zap.L().Error("创建数据库连接", zap.Error(err)) | ||||||
|  | 		os.Exit(-1) | ||||||
|  | 	} | ||||||
|  | 	db.SetMaxOpenConns(300) | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | package mq | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"github.com/klauspost/compress/zstd" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // Compress 压缩
 | ||||||
|  | func Compress(src []byte) []byte { | ||||||
|  | 	encoder, _ := zstd.NewWriter(nil) | ||||||
|  | 	zstd.WithEncoderConcurrency(3) | ||||||
|  | 	return encoder.EncodeAll(src, make([]byte, 0, len(src))) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func UnCompress(src []byte) ([]byte, error) { | ||||||
|  | 	d, err := zstd.NewReader(nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	defer d.Close() | ||||||
|  | 
 | ||||||
|  | 	uncompressed, err := d.DecodeAll(src, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return uncompressed, nil | ||||||
|  | } | ||||||
|  | @ -0,0 +1,123 @@ | ||||||
|  | package mq | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
|  | 	"git.hpds.cc/pavement/hpds_node" | ||||||
|  | 	"go.uber.org/zap" | ||||||
|  | 
 | ||||||
|  | 	"data_minio/config" | ||||||
|  | 	"data_minio/global" | ||||||
|  | 	"data_minio/model" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var MqList []HpdsMqNode | ||||||
|  | 
 | ||||||
|  | type HpdsMqNode struct { | ||||||
|  | 	MqType   uint | ||||||
|  | 	Topic    string | ||||||
|  | 	Node     config.HpdsNode | ||||||
|  | 	EndPoint interface{} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func must(err error) { | ||||||
|  | 	if err != nil { | ||||||
|  | 		global.Logger.With(zap.String("数据落地服务", "错误信息")).Error("启动错误", zap.Error(err)) | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewMqClient(funcs []config.FuncConfig, node config.HpdsNode) (mqList []HpdsMqNode, err error) { | ||||||
|  | 	mqList = make([]HpdsMqNode, 0) | ||||||
|  | 	for _, v := range funcs { | ||||||
|  | 		switch v.MqType { | ||||||
|  | 		case 2: | ||||||
|  | 			sf := hpds_node.NewStreamFunction( | ||||||
|  | 				v.Name, | ||||||
|  | 				hpds_node.WithMqAddr(fmt.Sprintf("%s:%d", node.Host, node.Port)), | ||||||
|  | 				hpds_node.WithObserveDataTags(v.DataTag), | ||||||
|  | 				hpds_node.WithCredential(node.Token), | ||||||
|  | 			) | ||||||
|  | 			_ = sf.SetHandler(handler) | ||||||
|  | 			err = sf.Connect() | ||||||
|  | 			nodeInfo := HpdsMqNode{ | ||||||
|  | 				MqType:   2, | ||||||
|  | 				Topic:    v.Name, | ||||||
|  | 				Node:     node, | ||||||
|  | 				EndPoint: sf, | ||||||
|  | 			} | ||||||
|  | 			must(err) | ||||||
|  | 
 | ||||||
|  | 			mqList = append(mqList, nodeInfo) | ||||||
|  | 		default: | ||||||
|  | 			ap := hpds_node.NewAccessPoint( | ||||||
|  | 				v.Name, | ||||||
|  | 				hpds_node.WithMqAddr(fmt.Sprintf("%s:%d", node.Host, node.Port)), | ||||||
|  | 				hpds_node.WithCredential(node.Token), | ||||||
|  | 			) | ||||||
|  | 			err = ap.Connect() | ||||||
|  | 			nodeInfo := HpdsMqNode{ | ||||||
|  | 				MqType:   1, | ||||||
|  | 				Topic:    v.Name, | ||||||
|  | 				Node:     node, | ||||||
|  | 				EndPoint: ap, | ||||||
|  | 			} | ||||||
|  | 			must(err) | ||||||
|  | 			ap.SetDataTag(v.DataTag) | ||||||
|  | 			mqList = append(mqList, nodeInfo) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 	} | ||||||
|  | 	return mqList, err | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetMqClient(topic string, mqType uint) *HpdsMqNode { | ||||||
|  | 	for _, v := range MqList { | ||||||
|  | 		if v.Topic == topic && v.MqType == mqType { | ||||||
|  | 			return &v | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GenerateAndSendData(stream hpds_node.AccessPoint, data []byte) error { | ||||||
|  | 	_, err := stream.Write(data) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	time.Sleep(1000 * time.Millisecond) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func handler(data []byte) (byte, []byte) { | ||||||
|  | 	req := new(InstructionReq) | ||||||
|  | 	err := json.Unmarshal(data, req) | ||||||
|  | 	if err != nil { | ||||||
|  | 		global.Must(err) | ||||||
|  | 	} else { | ||||||
|  | 		switch req.Command { | ||||||
|  | 		case DatasetRequest: | ||||||
|  | 			payload := req.Payload.(map[string]interface{}) | ||||||
|  | 			dt, err := model.GetDatasetById(int64(payload["datasetId"].(float64))) | ||||||
|  | 			global.Must(err) | ||||||
|  | 			fileContent, _ := UnCompress([]byte(payload["file"].(string))) | ||||||
|  | 
 | ||||||
|  | 			fm := new(model.FileManager) | ||||||
|  | 			fm.FileName = payload["fileName"].(string) | ||||||
|  | 			//fm.AccessUrl = payload["filePath"].(string)
 | ||||||
|  | 			fm.Scene = fmt.Sprintf("%d", dt.CategoryId) | ||||||
|  | 			fm.DatasetId = dt.DatasetId | ||||||
|  | 			fm.FileSize = int64(payload["fileSize"].(float64)) | ||||||
|  | 			fm.FileMd5 = payload["fileMd5"].(string) | ||||||
|  | 			fm.Creator = 0 | ||||||
|  | 			fm.CreateAt = time.Now().Unix() | ||||||
|  | 			fm.UpdateAt = time.Now().Unix() | ||||||
|  | 			_, err = global.DB.Insert(fm) | ||||||
|  | 			global.Must(err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return 0x12, nil | ||||||
|  | } | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | package mq | ||||||
|  | 
 | ||||||
|  | const ( | ||||||
|  | 	DatasetRequest = iota + 10 | ||||||
|  | 	DatasetResponse | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type InstructionReq struct { | ||||||
|  | 	Command int         `json:"command"` | ||||||
|  | 	Payload interface{} `json:"payload"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type FileTransferInfo struct { | ||||||
|  | 	FileName   string | ||||||
|  | 	FilePath   string | ||||||
|  | 	DatasetId  int | ||||||
|  | 	File       string | ||||||
|  | 	IsCompress bool | ||||||
|  | 	FileMd5    string | ||||||
|  | } | ||||||
|  | @ -0,0 +1,87 @@ | ||||||
|  | package discover | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 
 | ||||||
|  | 	"github.com/hashicorp/consul/api" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type ConsulConfig struct { | ||||||
|  | 	Client                         *api.Client `json:"client"`                         // consul client
 | ||||||
|  | 	ConsulAddress                  string      `json:"consulAddress"`                  // consul 服务地址:IP+port
 | ||||||
|  | 	ServiceId                      string      `json:"serviceId"`                      // 服务ID
 | ||||||
|  | 	ServiceName                    string      `json:"serviceName"`                    // 服务名称
 | ||||||
|  | 	ServiceIP                      string      `json:"serviceIP"`                      // 服务IP
 | ||||||
|  | 	ServicePort                    int         `json:"servicePort"`                    // 服务端口
 | ||||||
|  | 	Tags                           []string    `json:"tags"`                           // 服务标签列表
 | ||||||
|  | 	DeregisterCriticalServiceAfter int         `json:"deregisterCriticalServiceAfter"` // 指定与服务关联的检查应在此时间之后注销
 | ||||||
|  | 	Interval                       int         `json:"interval"`                       // 指定运行此检查的频率
 | ||||||
|  | 	Timeout                        int         `json:"timeout"`                        // 在脚本、HTTP、TCP 或 gRPC 检查的情况下指定传出连接的超时时间
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewConsulConfig(consulAddress string, | ||||||
|  | 	serviceId string, | ||||||
|  | 	serviceName string, | ||||||
|  | 	serviceIP string, | ||||||
|  | 	servicePort int, | ||||||
|  | 	tags []string, | ||||||
|  | 	deregisterCriticalServiceAfter int, | ||||||
|  | 	interval int, | ||||||
|  | 	timeout int) (*ConsulConfig, error) { | ||||||
|  | 	// 1.consul配置
 | ||||||
|  | 	config := api.DefaultConfig() | ||||||
|  | 	config.Address = consulAddress | ||||||
|  | 	// 2.client
 | ||||||
|  | 	client, err := api.NewClient(config) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	return &ConsulConfig{ | ||||||
|  | 		Client:                         client, | ||||||
|  | 		ConsulAddress:                  consulAddress, | ||||||
|  | 		ServiceId:                      serviceId, | ||||||
|  | 		ServiceName:                    serviceName, | ||||||
|  | 		ServiceIP:                      serviceIP, | ||||||
|  | 		ServicePort:                    servicePort, | ||||||
|  | 		Tags:                           tags, | ||||||
|  | 		DeregisterCriticalServiceAfter: deregisterCriticalServiceAfter, | ||||||
|  | 		Interval:                       interval, | ||||||
|  | 		Timeout:                        timeout, | ||||||
|  | 	}, nil | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // ServiceRegister 服务注册
 | ||||||
|  | func (cf *ConsulConfig) ServiceRegister() (err error) { | ||||||
|  | 	// 注册器
 | ||||||
|  | 	reg := &api.AgentServiceRegistration{ | ||||||
|  | 		ID:      cf.ServiceId, | ||||||
|  | 		Name:    cf.ServiceName, | ||||||
|  | 		Address: cf.ServiceIP, | ||||||
|  | 		Port:    cf.ServicePort, | ||||||
|  | 		Tags:    cf.Tags, | ||||||
|  | 		Check: &api.AgentServiceCheck{ | ||||||
|  | 			Interval:                       fmt.Sprintf("%vs", cf.Interval),                                  // 健康检查间隔
 | ||||||
|  | 			HTTP:                           fmt.Sprintf("http://%v:%v/health", cf.ServiceIP, cf.ServicePort), // HTTP 支持,执行健康检查的地址,service 会传到 Health.Check 函数中
 | ||||||
|  | 			Timeout:                        fmt.Sprintf("%vs", cf.Timeout),                                   // 健康检查超时时间
 | ||||||
|  | 			DeregisterCriticalServiceAfter: fmt.Sprintf("%vs", cf.DeregisterCriticalServiceAfter),            // 注销时间,相当于过期时间
 | ||||||
|  | 			Notes:                          "Consul check service health status.", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	// 注册服务
 | ||||||
|  | 	err = cf.Client.Agent().ServiceRegister(reg) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // ServiceDeregister 服务注销
 | ||||||
|  | func (cf *ConsulConfig) ServiceDeregister() error { | ||||||
|  | 	return cf.Client.Agent().ServiceDeregister(cf.ServiceId) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // ServiceDiscover 服务发现
 | ||||||
|  | func (cf *ConsulConfig) ServiceDiscover(service string, tags []string, q *api.QueryOptions) ([]*api.CatalogService, *api.QueryMeta, error) { | ||||||
|  | 	return cf.Client.Catalog().ServiceMultipleTags(service, tags, q) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue