ShannonDing commented on a change in pull request #315: [ISSUE #314] support
name server domain
URL: https://github.com/apache/rocketmq-client-go/pull/315#discussion_r359181606
##########
File path: internal/namesrv.go
##########
@@ -125,3 +138,88 @@ func (s *namesrvs) SetCredentials(credentials
primitive.Credentials) {
func (s *namesrvs) AddrList() []string {
return s.srvs
}
+
+// UpdateNameServerAddress will update srvs.
+// docs: https://rocketmq.apache.org/docs/best-practice-namesvr/
+func (s *namesrvs) UpdateNameServerAddress(nameServerDomain, instanceName
string) {
+ if nameServerDomain == "" {
+ nameServerDomain = os.Getenv("NAMESRV_ADDR") // try to get from
environment variable
+ if nameServerDomain == "" {
+ nameServerDomain = DEFAULT_NAMESRV_ADDR
+ }
+ }
+ s.lock.Lock()
+ defer s.lock.Unlock()
+
+ nameServers := []string{}
+ //load snapshot
+ homeDir := ""
+ if usr, err := user.Current(); err == nil {
+ homeDir = usr.HomeDir
+ } else {
+ rlog.Error("name server domain, can't get user home directory",
map[string]interface{}{
+ "err": err,
+ })
+ }
+ storePath := path.Join(homeDir, "/logs/rocketmq-go/snapshot")
+ if _, err := os.Stat(storePath); os.IsNotExist(err) {
+ if err = os.MkdirAll(storePath, 0755); err != nil {
+ rlog.Fatal("can't create name server snapshot
directory", map[string]interface{}{
+ "path": storePath,
+ "err": err,
+ })
+ }
+ }
+ filePath := path.Join(storePath, fmt.Sprintf("nameserver_addr-%s",
instanceName))
+ if _, err := os.Stat(filePath); !os.IsNotExist(err) {
+ if bs, err := ioutil.ReadFile(filePath); err == nil {
+ rlog.Info("load the name server snapshot local file",
map[string]interface{}{
+ "filePath": filePath,
+ })
+ nameServers = strings.Split(string(bs), ";")
+ }
+ } else {
+ rlog.Warning("name server snapshot local file not exists",
map[string]interface{}{
+ "filePath": filePath,
+ })
+ }
+
+ client := http.Client{Timeout: 10 * time.Second}
+ resp, err := client.Get(nameServerDomain)
+ if err == nil {
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err == nil {
+ if err := ioutil.WriteFile(filePath, body, 0644); err
== nil {
+ rlog.Info("name server snapshot save
successfully", map[string]interface{}{
+ "filePath": filePath,
+ })
+ } else {
+ rlog.Error("name server snapshot save failed",
map[string]interface{}{
+ "filePath": filePath,
+ "err": err,
+ })
+ }
+
+ if bodyStr := string(body); bodyStr != "" {
+ if oldBodyStr := strings.Join(s.srvs, ";");
oldBodyStr != "" && oldBodyStr != bodyStr {
+ rlog.Info("name server address
changed", map[string]interface{}{
+ "old": oldBodyStr,
+ "new": bodyStr,
+ })
+ }
+ nameServers = strings.Split(string(body), ";")
+ rlog.Info("name server http fetch
successfully", map[string]interface{}{
+ "addrs": bodyStr,
+ })
+ }
+ } else {
+ rlog.Error("name server http fetch failed",
map[string]interface{}{
+ "NameServerDomain": nameServerDomain,
+ "err": err,
+ })
+ }
+ }
+
+ s.srvs = nameServers
Review comment:
it is better to check if it is changed or not.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services