Skip to content
Snippets Groups Projects
Unverified Commit a04db943 authored by fangyincheng's avatar fangyincheng Committed by GitHub
Browse files

Merge pull request #668 from lzp0412/1.4

fix not inovke nacos destroy when graceful shutdown
parents 428fe1a9 b8893fc3
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
......@@ -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,
......
......@@ -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}
......
......@@ -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) {
......@@ -116,6 +117,7 @@ func newNacosRegistry(url *common.URL) (registry.Registry, error) {
registry := nacosRegistry{
URL: url,
namingClient: client,
registryUrls: []common.URL{},
}
return &registry, nil
}
......@@ -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)
......@@ -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
}
......@@ -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
}
......@@ -44,6 +44,7 @@ import (
)
var (
once sync.Once
regProtocol *registryProtocol
)
......@@ -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)
}
......@@ -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() {
......@@ -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 {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment