dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/event/protocol_ports_metadata_customizer.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package event 19 20 import ( 21 "encoding/json" 22 "strconv" 23 ) 24 25 import ( 26 "github.com/dubbogo/gost/log/logger" 27 ) 28 29 import ( 30 "dubbo.apache.org/dubbo-go/v3/common/constant" 31 "dubbo.apache.org/dubbo-go/v3/common/extension" 32 "dubbo.apache.org/dubbo-go/v3/metadata/service/local" 33 "dubbo.apache.org/dubbo-go/v3/registry" 34 ) 35 36 func init() { 37 extension.AddCustomizers(&ProtocolPortsMetadataCustomizer{}) 38 } 39 40 // ProtocolPortsMetadataCustomizer will update the endpoints 41 type ProtocolPortsMetadataCustomizer struct { 42 } 43 44 // GetPriority will return 0, which means it will be invoked at the beginning 45 func (p *ProtocolPortsMetadataCustomizer) GetPriority() int { 46 return 0 47 } 48 49 // Customize put the the string like [{"protocol": "dubbo", "port": 123}] into instance's metadata 50 func (p *ProtocolPortsMetadataCustomizer) Customize(instance registry.ServiceInstance) { 51 metadataService, err := local.GetLocalMetadataService() 52 if err != nil { 53 logger.Errorf("Could not init the MetadataService", err) 54 return 55 } 56 57 // 4 is enough... we don't have many protocol 58 protocolMap := make(map[string]int, 4) 59 60 list, err := metadataService.GetExportedServiceURLs() 61 if list == nil || len(list) == 0 || err != nil { 62 logger.Debugf("Could not find exported urls", err) 63 return 64 } 65 66 for _, u := range list { 67 if err != nil || len(u.Protocol) == 0 { 68 logger.Errorf("the url string is invalid: %s", u, err) 69 continue 70 } 71 72 port, err := strconv.Atoi(u.Port) 73 if err != nil { 74 logger.Errorf("Could not customize the metadata of port. ", err) 75 } 76 protocolMap[u.Protocol] = port 77 } 78 79 instance.GetMetadata()[constant.ServiceInstanceEndpoints] = endpointsStr(protocolMap) 80 } 81 82 // endpointsStr convert the map to json like [{"protocol": "dubbo", "port": 123}] 83 func endpointsStr(protocolMap map[string]int) string { 84 if len(protocolMap) == 0 { 85 return "" 86 } 87 88 endpoints := make([]registry.Endpoint, 0, len(protocolMap)) 89 for k, v := range protocolMap { 90 endpoints = append(endpoints, registry.Endpoint{ 91 Port: v, 92 Protocol: k, 93 }) 94 } 95 96 str, err := json.Marshal(endpoints) 97 if err != nil { 98 logger.Errorf("could not convert the endpoints to json", err) 99 return "" 100 } 101 return string(str) 102 }