Skip to content

Commit

Permalink
Merge pull request #668 from lzp0412/1.4
Browse files Browse the repository at this point in the history
fix not inovke nacos destroy when graceful shutdown
  • Loading branch information
fangyincheng authored Jul 28, 2020
2 parents 6e0c4c7 + d869a1d commit 11a1391
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config/graceful_shutdown_signal_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
// ShutdownSignals ...
ShutdownSignals = []os.Signal{os.Interrupt, os.Kill, syscall.SIGKILL, syscall.SIGSTOP,
syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP,
syscall.SIGABRT, syscall.SIGSYS}
syscall.SIGABRT, syscall.SIGSYS, syscall.SIGTERM}

// DumpHeapShutdownSignals ...
DumpHeapShutdownSignals = []os.Signal{syscall.SIGQUIT, syscall.SIGILL,
Expand Down
2 changes: 1 addition & 1 deletion config/graceful_shutdown_signal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
// ShutdownSignals ...
ShutdownSignals = []os.Signal{os.Interrupt, os.Kill, syscall.SIGKILL, syscall.SIGSTOP,
syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP,
syscall.SIGABRT, syscall.SIGSYS}
syscall.SIGABRT, syscall.SIGSYS, syscall.SIGTERM}

// DumpHeapShutdownSignals ...
DumpHeapShutdownSignals = []os.Signal{syscall.SIGQUIT, syscall.SIGILL,
Expand Down
2 changes: 1 addition & 1 deletion config/graceful_shutdown_signal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
// ShutdownSignals ...
ShutdownSignals = []os.Signal{os.Interrupt, os.Kill, syscall.SIGKILL,
syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP,
syscall.SIGABRT}
syscall.SIGABRT, syscall.SIGTERM}

// DumpHeapShutdownSignals ...
DumpHeapShutdownSignals = []os.Signal{syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, syscall.SIGABRT}
Expand Down
38 changes: 38 additions & 0 deletions registry/nacos/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
type nacosRegistry struct {
*common.URL
namingClient naming_client.INamingClient
registryUrls []common.URL
}

func getNacosConfig(url *common.URL) (map[string]interface{}, error) {
Expand Down Expand Up @@ -116,6 +117,7 @@ func newNacosRegistry(url *common.URL) (registry.Registry, error) {
registry := nacosRegistry{
URL: url,
namingClient: client,
registryUrls: []common.URL{},
}
return &registry, nil
}
Expand Down Expand Up @@ -176,6 +178,21 @@ func createRegisterParam(url common.URL, serviceName string) vo.RegisterInstance
return instance
}

func createDeregisterParam(url common.URL, serviceName string) vo.DeregisterInstanceParam {
if len(url.Ip) == 0 {
url.Ip = localIP
}
if len(url.Port) == 0 || url.Port == "0" {
url.Port = "80"
}
port, _ := strconv.Atoi(url.Port)
return vo.DeregisterInstanceParam{
Ip: url.Ip,
Port: uint64(port),
ServiceName: serviceName,
Ephemeral: true,
}
}
func (nr *nacosRegistry) Register(url common.URL) error {
serviceName := getServiceName(url)
param := createRegisterParam(url, serviceName)
Expand All @@ -186,6 +203,20 @@ func (nr *nacosRegistry) Register(url common.URL) error {
if !isRegistry {
return perrors.New("registry [" + serviceName + "] to nacos failed")
}
nr.registryUrls = append(nr.registryUrls, url)
return nil
}

func (nr *nacosRegistry) DeRegister(url common.URL) error {
serviceName := getServiceName(url)
param := createDeregisterParam(url, serviceName)
isDeRegistry, err := nr.namingClient.DeregisterInstance(param)
if err != nil {
return err
}
if !isDeRegistry {
return perrors.New("DeRegistry [" + serviceName + "] to nacos failed")
}
return nil
}

Expand Down Expand Up @@ -236,5 +267,12 @@ func (nr *nacosRegistry) IsAvailable() bool {
}

func (nr *nacosRegistry) Destroy() {
for _, url := range nr.registryUrls {
err := nr.DeRegister(url)
logger.Infof("DeRegister Nacos url:%+v", url)
if err != nil {
logger.Errorf("Deregister url:%+v err:%v", url, err.Error())
}
}
return
}
12 changes: 6 additions & 6 deletions registry/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
)

var (
once sync.Once
regProtocol *registryProtocol
)

Expand Down Expand Up @@ -154,6 +155,7 @@ func (proto *registryProtocol) Export(invoker protocol.Invoker) protocol.Exporte
if regI, loaded := proto.registries.Load(registryUrl.Key()); !loaded {
reg = getRegistry(registryUrl)
proto.registries.Store(registryUrl.Key(), reg)
logger.Infof("Export proto:%p registries address:%p", proto, proto.registries)
} else {
reg = regI.(registry.Registry)
}
Expand Down Expand Up @@ -307,14 +309,12 @@ func (proto *registryProtocol) Destroy() {
ivk.Destroy()
}
proto.invokers = []protocol.Invoker{}

proto.bounds.Range(func(key, value interface{}) bool {
exporter := value.(protocol.Exporter)
exporter.Unexport()
proto.bounds.Delete(key)
return true
})

proto.registries.Range(func(key, value interface{}) bool {
reg := value.(registry.Registry)
if reg.IsAvailable() {
Expand Down Expand Up @@ -348,10 +348,10 @@ func setProviderUrl(regURL *common.URL, providerURL *common.URL) {

// GetProtocol ...
func GetProtocol() protocol.Protocol {
if regProtocol != nil {
return regProtocol
}
return newRegistryProtocol()
once.Do(func() {
regProtocol = newRegistryProtocol()
})
return regProtocol
}

type wrappedInvoker struct {
Expand Down

0 comments on commit 11a1391

Please sign in to comment.