dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/event/metadata_service_url_params_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 ) 23 24 import ( 25 gxset "github.com/dubbogo/gost/container/set" 26 "github.com/dubbogo/gost/log/logger" 27 ) 28 29 import ( 30 "dubbo.apache.org/dubbo-go/v3/common" 31 "dubbo.apache.org/dubbo-go/v3/common/constant" 32 "dubbo.apache.org/dubbo-go/v3/common/extension" 33 "dubbo.apache.org/dubbo-go/v3/metadata/service/local" 34 "dubbo.apache.org/dubbo-go/v3/registry" 35 ) 36 37 func init() { 38 exceptKeys := gxset.NewSet( 39 // remove ApplicationKey because service name must be present 40 constant.ApplicationKey, 41 // remove GroupKey, always uses service name. 42 constant.GroupKey, 43 // remove TimestampKey because it's nonsense 44 constant.TimestampKey) 45 extension.AddCustomizers(&metadataServiceURLParamsMetadataCustomizer{exceptKeys: exceptKeys}) 46 } 47 48 type metadataServiceURLParamsMetadataCustomizer struct { 49 exceptKeys *gxset.HashSet 50 } 51 52 // GetPriority will return 0 so that it will be invoked in front of user defining Customizer 53 func (m *metadataServiceURLParamsMetadataCustomizer) GetPriority() int { 54 return 0 55 } 56 57 func (m *metadataServiceURLParamsMetadataCustomizer) Customize(instance registry.ServiceInstance) { 58 ms, err := local.GetLocalMetadataService() 59 if err != nil { 60 logger.Errorf("could not find the metadata service", err) 61 return 62 } 63 url, err := ms.GetMetadataServiceURL() 64 if url == nil || err != nil { 65 logger.Errorf("the metadata service url is nil") 66 return 67 } 68 ps := m.convertToParams(url) 69 str, err := json.Marshal(ps) 70 if err != nil { 71 logger.Errorf("could not transfer the map to json", err) 72 return 73 } 74 instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName] = string(str) 75 } 76 77 func (m *metadataServiceURLParamsMetadataCustomizer) convertToParams(url *common.URL) map[string]string { 78 // those keys are useless 79 p := make(map[string]string, len(url.GetParams())) 80 for k, v := range url.GetParams() { 81 // we will ignore that 82 if !common.IncludeKeys.Contains(k) || len(v) == 0 || len(v[0]) == 0 { 83 continue 84 } 85 p[k] = v[0] 86 } 87 p[constant.PortKey] = url.Port 88 p[constant.ProtocolKey] = url.Protocol 89 return p 90 }