-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Implement the logic of the NACOS service in the Discovery module #604
Changes from 11 commits
a217d9b
872bafd
64cfd54
95acd6b
3b5a4c5
565a053
96c8783
5783ed6
b5bdd90
95e828a
c895229
723a0eb
07ae69c
e12a73f
3c16adf
3eb6020
861d4df
76f3863
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mock | ||
|
||
import "github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client" | ||
|
||
type NacosClient interface { | ||
naming_client.INamingClient | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,81 @@ | |
|
||
package discovery | ||
|
||
type NacosRegistryService struct{} | ||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/nacos-group/nacos-sdk-go/v2/clients" | ||
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client" | ||
"github.com/nacos-group/nacos-sdk-go/v2/common/constant" | ||
"github.com/nacos-group/nacos-sdk-go/v2/vo" | ||
"github.com/seata/seata-go/pkg/util/log" | ||
) | ||
|
||
type NacosRegistryService struct { | ||
nc naming_client.INamingClient | ||
nacosServerConfig NacosConfig | ||
} | ||
|
||
func NewNacosRegistryService(nacosConfig NacosConfig) RegistryService { | ||
properties := make(map[string]interface{}) | ||
serverConfigs, _ := parseNacosServerConfig(nacosConfig) | ||
properties[constant.KEY_SERVER_CONFIGS] = serverConfigs | ||
client, err := clients.CreateNamingClient(properties) | ||
if err != nil { | ||
log.Fatalf("nacos client init error") | ||
panic("nacos client init error") | ||
|
||
} | ||
return &NacosRegistryService{client, nacosConfig} | ||
} | ||
|
||
func parseNacosServerConfig(config NacosConfig) ([]constant.ServerConfig, error) { | ||
serviceConfigs := make([]constant.ServerConfig, 0) | ||
serverAddr := config.ServerAddr | ||
addresses := strings.Split(serverAddr, ";") | ||
for _, addr := range addresses { | ||
ipPort := strings.Split(addr, ":") | ||
if len(ipPort) != 2 { | ||
return nil, fmt.Errorf("endpoint format should like ip:port. endpoint: %s", addr) | ||
} | ||
ip := ipPort[0] | ||
port, err := strconv.Atoi(ipPort[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
serviceConfigs = append(serviceConfigs, constant.ServerConfig{ | ||
IpAddr: ip, | ||
Port: uint64(port), | ||
}) | ||
} | ||
return serviceConfigs, nil | ||
} | ||
|
||
func (s *NacosRegistryService) Lookup(key string) ([]*ServiceInstance, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
instances, err := s.nc.SelectAllInstances(vo.SelectAllInstancesParam{ | ||
GroupName: s.nacosServerConfig.Group, | ||
ServiceName: s.nacosServerConfig.Application, | ||
}) | ||
if err != nil { | ||
log.Fatalf("select all nacos service instance error") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
return nil, err | ||
} | ||
serviceInstances := make([]*ServiceInstance, 0) | ||
for _, instance := range instances { | ||
if !instance.Healthy { | ||
log.Warnf("%s status is un-healthy", instance.InstanceId) | ||
continue | ||
} | ||
serviceInstances = append(serviceInstances, &ServiceInstance{ | ||
Addr: instance.Ip, | ||
Port: int(instance.Port), | ||
}) | ||
} | ||
log.Infof("look up %d service instance from nacos server", len(serviceInstances)) | ||
return serviceInstances, nil | ||
|
||
} | ||
|
||
func (NacosRegistryService) Close() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以在nacos.go内部维护一份缓存,并监听nacos server的节点变化,更新本地缓存。