com.weibo.api.motan.rpc.URL.getServerPortStr()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(185)

本文整理了Java中com.weibo.api.motan.rpc.URL.getServerPortStr()方法的一些代码示例,展示了URL.getServerPortStr()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URL.getServerPortStr()方法的具体详情如下:
包路径:com.weibo.api.motan.rpc.URL
类名称:URL
方法名:getServerPortStr

URL.getServerPortStr介绍

[英]comma separated host:port pairs, e.g. "127.0.0.1:3000"
[中]逗号分隔的主机:端口对,例如“127.0.0.1:3000”

代码示例

代码示例来源:origin: weibocom/motan

public static String toNodePath(URL url, ZkNodeType nodeType) {
    return toNodeTypePath(url, nodeType) + MotanConstants.PATH_SEPARATOR + url.getServerPortStr();
  }
}

代码示例来源:origin: weibocom/motan

private static <T> String getServerPorts(List<Referer<T>> referers) {
    if (referers == null || referers.isEmpty()) {
      return "[]";
    }

    StringBuilder builder = new StringBuilder();
    builder.append("[");
    for (Referer<T> referer : referers) {
      builder.append(referer.getUrl().getServerPortStr()).append(",");
    }
    builder.setLength(builder.length() - 1);
    builder.append("]");

    return builder.toString();
  }
}

代码示例来源:origin: weibocom/motan

public ProviderMessageRouter initRequestRouter(URL url, Provider<?> provider) {
  String ipPort = url.getServerPortStr();
  ProviderMessageRouter requestRouter = ipPort2RequestRouter.get(ipPort);
  if (requestRouter == null) {
    ipPort2RequestRouter.putIfAbsent(ipPort, new YarMessageRouter());
    requestRouter = ipPort2RequestRouter.get(ipPort);
  }
  requestRouter.addProvider(provider);
  return requestRouter;
}

代码示例来源:origin: weibocom/motan

@Override
public boolean cancel() {
  Exception e =
      new MotanServiceException(this.getClass().getName() + " task cancel: serverPort=" + serverUrl.getServerPortStr() + " "
          + MotanFrameworkUtil.toString(request) + " cost=" + (System.currentTimeMillis() - createTime));
  return cancel(e);
}

代码示例来源:origin: weibocom/motan

protected ProviderMessageRouter initRequestRouter(URL url) {
    String ipPort = url.getServerPortStr();
    ProviderMessageRouter requestRouter = ipPort2RequestRouter.get(ipPort);

    if (requestRouter == null) {
      ProviderProtectedMessageRouter router = new ProviderProtectedMessageRouter();
      StatsUtil.registryStatisticCallback(router);
      ipPort2RequestRouter.putIfAbsent(ipPort, router);
      requestRouter = ipPort2RequestRouter.get(ipPort);
    }
    requestRouter.addProvider(provider);

    return requestRouter;
  }
}

代码示例来源:origin: weibocom/motan

@Override
public void safeReleaseResource(RestServer server, URL url) {
  String ipPort = url.getServerPortStr();
  String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
  synchronized (ipPort2ServerShareChannel) {
    if (server != ipPort2ServerShareChannel.get(ipPort)) {
      server.stop();
      return;
    }
    Set<String> urls = server2UrlsShareChannel.get(server);
    urls.remove(protocolKey);
    if (urls.isEmpty()) {
      server.stop();
      ipPort2ServerShareChannel.remove(ipPort);
      server2UrlsShareChannel.remove(server);
    }
  }
}

代码示例来源:origin: weibocom/motan

private void timeoutSoCancel() {
  this.processTime = System.currentTimeMillis() - createTime;
  synchronized (lock) {
    if (!isDoing()) {
      return;
    }
    state = FutureState.CANCELLED;
    exception =
        new MotanServiceException(this.getClass().getName() + " request timeout: serverPort=" + serverUrl.getServerPortStr()
            + " " + MotanFrameworkUtil.toString(request) + " cost=" + (System.currentTimeMillis() - createTime),
            MotanErrorMsgConstant.SERVICE_TIMEOUT);
    lock.notifyAll();
  }
  notifyListeners();
}

代码示例来源:origin: weibocom/motan

@Override
public void safeReleaseResource(ResteasyWebTarget client, URL url) {
  String ipPort = url.getServerPortStr();
  String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
  synchronized (ipPort2ClientShareChannel) {
    if (client != ipPort2ClientShareChannel.get(ipPort)) {
      client.getResteasyClient().close();
      return;
    }
    Set<String> urls = client2UrlsShareChannel.get(client);
    urls.remove(protocolKey);
    if (urls.isEmpty()) {
      client.getResteasyClient().close();
      ipPort2ClientShareChannel.remove(ipPort);
      client2UrlsShareChannel.remove(client);
    }
  }
}

代码示例来源:origin: weibocom/motan

public void unexport(URL url, Provider<?> provider){
  String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
  String ipPort = url.getServerPortStr();
  Exporter<?> exporter = (Exporter<?>) exporterMap.remove(protocolKey);
  if (exporter != null) {
    exporter.destroy();
  }
  synchronized (ipPort2RequestRouter) {
    ProviderMessageRouter requestRouter = ipPort2RequestRouter.get(ipPort);
    if (requestRouter != null) {
      requestRouter.removeProvider(provider);
    }
  }
  LoggerUtil.info("yarRpcExporter unexport Success: url={}", url);
}

代码示例来源:origin: weibocom/motan

@Override
public ResteasyWebTarget createClient(URL url) {
  String ipPort = url.getServerPortStr();
  String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
  LoggerUtil.info(this.getClass().getSimpleName() + " create share_channel client: url={}", url);
  synchronized (ipPort2ClientShareChannel) {
    ResteasyWebTarget client = ipPort2ClientShareChannel.get(ipPort);
    if (client != null) {
      saveEndpoint2Urls(client2UrlsShareChannel, client, protocolKey);
      return client;
    }
    client = innerCreateClient(url);
    ipPort2ClientShareChannel.put(ipPort, client);
    saveEndpoint2Urls(client2UrlsShareChannel, client, protocolKey);
    return client;
  }
}

代码示例来源:origin: weibocom/motan

@SuppressWarnings("unchecked")
@Override
public void unexport() {
  String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
  String ipPort = url.getServerPortStr();
  Exporter<T> exporter = (Exporter<T>) exporterMap.remove(protocolKey);
  if (exporter != null) {
    exporter.destroy();
  }
  ProviderMessageRouter requestRouter = ipPort2RequestRouter.get(ipPort);
  if (requestRouter != null) {
    requestRouter.removeProvider(provider);
  }
  LoggerUtil.info("DefaultRpcExporter unexport Success: url={}", url);
}

代码示例来源:origin: weibocom/motan

/**
 * 增加调用失败的次数:
 * <p>
 * <pre>
 *          如果连续失败的次数 >= maxClientConnection, 那么把client设置成不可用状态
 * </pre>
 */
void incrErrorCount() {
  long count = errorCount.incrementAndGet();
  // 如果节点是可用状态,同时当前连续失败的次数超过连接数,那么把该节点标示为不可用
  if (count >= connections && state.isAliveState()) {
    synchronized (this) {
      count = errorCount.longValue();
      if (count >= connections && state.isAliveState()) {
        LoggerUtil.error("NettyClient unavailable Error: url=" + url.getIdentity() + " "
            + url.getServerPortStr());
        state = ChannelState.UNALIVE;
      }
    }
  }
}

代码示例来源:origin: weibocom/motan

/**
 * 增加调用失败的次数:
 *
 * <pre>
 *          如果连续失败的次数 >= maxClientConnection, 那么把client设置成不可用状态
 * </pre>
 *
 */
void incrErrorCount() {
  long count = errorCount.incrementAndGet();
  // 如果节点是可用状态,同时当前连续失败的次数超过限制maxClientConnection次,那么把该节点标示为不可用
  if (count >= maxClientConnection && state.isAliveState()) {
    synchronized (this) {
      count = errorCount.longValue();
      if (count >= maxClientConnection && state.isAliveState()) {
        LoggerUtil.error("NettyClient unavailable Error: url=" + url.getIdentity() + " "
            + url.getServerPortStr());
        state = ChannelState.UNALIVE;
      }
    }
  }
}

代码示例来源:origin: weibocom/motan

@Override
protected Object encode(ChannelHandlerContext ctx, Channel nettyChannel, Object message) throws Exception {
  Object obj;
  if (codec instanceof MotanV2Codec) {
    obj = encodev2(ctx, nettyChannel, message);
  } else {
    obj = encodev1(ctx, nettyChannel, message);
  }
  MotanFrameworkUtil.logRequestEvent(getRequestId(message), "after encode rpc " + (message instanceof Request ? "request " : "response ") + this.client.getUrl().getServerPortStr(), System.currentTimeMillis());
  return obj;
}

代码示例来源:origin: weibocom/motan

private <T extends Endpoint> void safeReleaseResource(T endpoint, URL url, Map<String, T> ipPort2Endpoint,
    ConcurrentMap<T, Set<String>> endpoint2Urls) {
  boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
  if (!shareChannel) {
    destory(endpoint);
    return;
  }
  synchronized (ipPort2Endpoint) {
    String ipPort = url.getServerPortStr();
    String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
    if (endpoint != ipPort2Endpoint.get(ipPort)) {
      destory(endpoint);
      return;
    }
    Set<String> urls = endpoint2Urls.get(endpoint);
    urls.remove(protocolKey);
    if (urls.isEmpty()) {
      destory(endpoint);
      ipPort2Endpoint.remove(ipPort);
      endpoint2Urls.remove(endpoint);
    }
  }
}

代码示例来源:origin: weibocom/motan

/**
 * protocol key: protocol://host:port/group/interface/version
 *
 * @param url
 * @return
 */
public static String getProtocolKey(URL url) {
  return url.getProtocol() + MotanConstants.PROTOCOL_SEPARATOR + url.getServerPortStr() + MotanConstants.PATH_SEPARATOR
      + url.getGroup() + MotanConstants.PATH_SEPARATOR + url.getPath() + MotanConstants.PATH_SEPARATOR + url.getVersion();
}

代码示例来源:origin: weibocom/motan

/**
 * 重置调用失败的计数 :
 * <pre>
 * 把节点设置成可用
 * </pre>
 */
void resetErrorCount() {
  errorCount.set(0);
  if (state.isAliveState()) {
    return;
  }
  synchronized (this) {
    if (state.isAliveState()) {
      return;
    }
    // 如果节点是unalive才进行设置,而如果是 close 或者 uninit,那么直接忽略
    if (state.isUnAliveState()) {
      long count = errorCount.longValue();
      // 过程中有其他并发更新errorCount的,因此这里需要进行一次判断
      if (count < connections) {
        state = ChannelState.ALIVE;
        LoggerUtil.info("NettyClient recover available: url=" + url.getIdentity() + " "
            + url.getServerPortStr());
      }
    }
  }
}

代码示例来源:origin: weibocom/motan

@Override
public RestServer createServer(URL url) {
  String ipPort = url.getServerPortStr();
  String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
  LoggerUtil.info(this.getClass().getSimpleName() + " create share_channel server: url={}", url);
  synchronized (ipPort2ServerShareChannel) {
    RestServer server = ipPort2ServerShareChannel.get(ipPort);
    if (server != null) {
      saveEndpoint2Urls(server2UrlsShareChannel, server, protocolKey);
      return server;
    }
    url = url.createCopy();
    url.setPath(""); // 共享server端口,由于有多个interfaces存在,所以把path设置为空
    server = innerCreateServer(url);
    server.start();
    ipPort2ServerShareChannel.put(ipPort, server);
    saveEndpoint2Urls(server2UrlsShareChannel, server, protocolKey);
    return server;
  }
}

代码示例来源:origin: weibocom/motan

private static byte[] encodeMessage(Channel channel, Codec codec, Object msg) throws IOException {
  byte[] data;
  if (msg instanceof Response) {
    try {
      data = codec.encode(channel, msg);
    } catch (Exception e) {
      LoggerUtil.error("NettyEncoder encode error, identity=" + channel.getUrl().getIdentity(), e);
      long requestId = getRequestId(msg);
      Response response = buildExceptionResponse(requestId, e);
      data = codec.encode(channel, response);
    }
  } else {
    data = codec.encode(channel, msg);
  }
  MotanFrameworkUtil.logRequestEvent(getRequestId(msg), "after encode rpc " + (msg instanceof Request ? "request " : "response ") + channel.getUrl().getServerPortStr(), System.currentTimeMillis());
  return data;
}

代码示例来源:origin: weibocom/motan

private Response request(Request request, boolean async) throws TransportException {
  Channel channel;
  Response response;
  try {
    // return channel or throw exception(timeout or connection_fail)
    channel = getChannel();
    MotanFrameworkUtil.logRequestEvent(request.getRequestId(), "after get server connection " + this.getUrl().getServerPortStr(), System.currentTimeMillis());
    if (channel == null) {
      LoggerUtil.error("NettyClient borrowObject null: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request));
      return null;
    }
    // async request
    response = channel.request(request);
  } catch (Exception e) {
    LoggerUtil.error("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
    if (e instanceof MotanAbstractException) {
      throw (MotanAbstractException) e;
    } else {
      throw new MotanServiceException("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
    }
  }
  // aysnc or sync result
  response = asyncResponse(response, async);
  return response;
}

相关文章