fl061157 edited a comment on issue #9156:
URL: https://github.com/apache/dubbo/issues/9156#issuecomment-983517973


   我现在开始用 DUBBO 3.0.4。也是完全没有办法, 只能搞一个 TransporterWrapper 在最终Bind时候,修改Port。
   
   public class TransporterWrapper implements Transporter {
   
       private final Transporter transporter;
   
       public TransporterWrapper(Transporter transporter) {
           this.transporter = transporter;
       }
   
       @Override
       public RemotingServer bind(URL url, ChannelHandler handler) throws 
RemotingException {
           ServiceModel sm = url.getServiceModel();
           if (Objects.isNull(sm)) {
               return transporter.bind(url, handler);
           }
           AbstractInterfaceConfig config = sm.getConfig();
           if (Objects.isNull(config) || !(config instanceof ServiceConfig)) {
               return transporter.bind(url, handler);
           }
           ServiceConfig<?> sc = (ServiceConfig<?>) config;
           List<ProtocolConfig> pcs = sc.getProtocols();
           if (CollectionUtils.isEmpty(pcs)) {
               return transporter.bind(url, handler);
           }
           Map<String, ProtocolConfig> pcm = toMap(pcs);
           if (MapUtils.isEmpty(pcm)) {
               return transporter.bind(url, handler);
           }
           ProtocolConfig pc = pcm.get(url.getProtocol());
           if (Objects.isNull(pc)) {
               return transporter.bind(url, handler);
           }
            Integer port = findPort(pc, sc.getProvider(), url.getProtocol());
           URL nURL = Objects.nonNull(port) ? 
url.addParameter(Constants.BIND_PORT_KEY, String.valueOf(port))
                   : url;
           return transporter.bind(nURL, handler);
       }
   
       private Integer findPort(ProtocolConfig protocolConfig, ProviderConfig 
provider, String protocol) {
           Integer portToBind;
           String port = getValueFromConfig(protocolConfig, DUBBO_PORT_TO_BIND);
           portToBind = parsePort(port);
           if (Objects.isNull(portToBind)) {
               portToBind = protocolConfig.getPort();
               if (Objects.nonNull(provider) && (Objects.isNull(portToBind) || 
portToBind == 0)) {
                   portToBind = provider.getPort();
               }
               final int defaultPort = 
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocol).getDefaultPort();
               if ((portToBind == null || portToBind == 0) && defaultPort != 0) 
{
                   portToBind = defaultPort;
               }
           }
           return portToBind;
       }
   
       private String getValueFromConfig(ProtocolConfig protocolConfig, String 
key) {
           String protocolPrefix = protocolConfig.getName().toUpperCase() + "_";
           String value = ConfigUtils.getSystemProperty(protocolPrefix + key);
           if (org.apache.dubbo.common.utils.StringUtils.isEmpty(value)) {
               value = ConfigUtils.getSystemProperty(key);
           }
           return value;
       }
   
       private Integer parsePort(String configPort) {
           if (StringUtils.isBlank(configPort)) {
               return null;
           }
           try {
               int intPort = Integer.parseInt(configPort);
               return isInvalidPort(intPort) ? null : intPort;
           } catch (Throwable throwable) {
               return null;
           }
       }
   
       private Map<String, ProtocolConfig> toMap(List<ProtocolConfig> pcs) {
           Map<String, ProtocolConfig> map = new HashMap<>();
           for (ProtocolConfig pc : pcs) {
               if (Objects.nonNull(pc) && StringUtils.isNotBlank(pc.getName())) 
{
                   map.put(pc.getName(), pc);
               }
           }
           return map;
       }
   
   
       @Override
       public Client connect(URL url, ChannelHandler handler) throws 
RemotingException {
           return transporter.connect(url, handler);
       }
   }
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to