|
@@ -4,6 +4,9 @@ import (
|
|
|
"context"
|
|
|
"crypto/md5"
|
|
|
"encoding/hex"
|
|
|
+ "git.nspix.com/golang/micro/helper/random"
|
|
|
+ "git.nspix.com/golang/micro/helper/utils"
|
|
|
+ "io/ioutil"
|
|
|
"math"
|
|
|
"math/rand"
|
|
|
"net"
|
|
@@ -11,6 +14,7 @@ import (
|
|
|
"net/http/pprof"
|
|
|
"os"
|
|
|
"os/signal"
|
|
|
+ "path"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"sync/atomic"
|
|
@@ -208,15 +212,36 @@ func (svr *Service) Environment() string {
|
|
|
return svr.environment
|
|
|
}
|
|
|
|
|
|
+func (svr *Service) getMachineID() (machineID string) {
|
|
|
+ var (
|
|
|
+ buf []byte
|
|
|
+ err error
|
|
|
+ )
|
|
|
+ cacheFile := path.Join(utils.CacheDir(), utils.HiddenFilePrefix()+svr.opts.ShortName())
|
|
|
+ if utils.FileExists(cacheFile) {
|
|
|
+ if buf, err = ioutil.ReadFile(cacheFile); err == nil {
|
|
|
+ machineID = string(buf)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if machineID == "" {
|
|
|
+ if machineID, err = machineid.Code(); err != nil {
|
|
|
+ machineID = random.String(64)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _ = os.MkdirAll(path.Dir(cacheFile), 0644)
|
|
|
+ _ = ioutil.WriteFile(cacheFile, []byte(machineID), 0755)
|
|
|
+ return machineID
|
|
|
+}
|
|
|
+
|
|
|
func (svr *Service) instance() *registry.ServiceNode {
|
|
|
var (
|
|
|
- err error
|
|
|
- id string
|
|
|
- dockerID string
|
|
|
- tcpAddr *net.TCPAddr
|
|
|
- ipLocal string
|
|
|
- machineCode string
|
|
|
- node *registry.ServiceNode
|
|
|
+ err error
|
|
|
+ id string
|
|
|
+ dockerID string
|
|
|
+ tcpAddr *net.TCPAddr
|
|
|
+ ipLocal string
|
|
|
+ node *registry.ServiceNode
|
|
|
)
|
|
|
if id, err = docker.SelfContainerID(); err != nil {
|
|
|
//生成唯一ID
|
|
@@ -249,9 +274,7 @@ func (svr *Service) instance() *registry.ServiceNode {
|
|
|
node.Port = svr.opts.Port
|
|
|
}
|
|
|
//上报机器码
|
|
|
- if machineCode, err = machineid.Code(); err == nil {
|
|
|
- node.Metadata["machine-code"] = machineCode
|
|
|
- }
|
|
|
+ node.Metadata["machine-code"] = svr.getMachineID()
|
|
|
node.Metadata["docker-id"] = dockerID
|
|
|
if svr.opts.EnableHttp {
|
|
|
node.Metadata["enable-http"] = "true"
|
|
@@ -403,6 +426,7 @@ func (svr *Service) prepare() (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+//destroy stop and destroy service
|
|
|
func (svr *Service) destroy() (err error) {
|
|
|
if !atomic.CompareAndSwapInt32(&svr.exitFlag, 0, 1) {
|
|
|
return
|