public Statistics(GrpcURL url){ this.url = url; this.application = url.getParameter(MonitorService.APPLICATION); this.service = url.getParameter(MonitorService.INTERFACE); this.method = url.getParameter(MonitorService.METHOD); this.group = url.getParameter(MonitorService.GROUP); this.version = url.getParameter(MonitorService.VERSION); this.client = url.getParameter(MonitorService.CONSUMER, url.getAddress()); this.server = url.getParameter(MonitorService.PROVIDER, url.getAddress()); }
public static boolean isMatch(GrpcURL subscribedUrl, GrpcURL providerUrl) { String subscribedInterface = subscribedUrl.getServiceInterface(); String providerInterface = providerUrl.getServiceInterface(); if (StringUtils.equals(subscribedInterface, providerInterface)) { return false; } if (!providerUrl.getParameter(Constants.ENABLED_KEY, true)) { return false; } String subscribedGroup = subscribedUrl.getGroup(); String subscribedVersion = subscribedUrl.getVersion(); String providerGroup = providerUrl.getGroup(); String providerVersion = providerUrl.getVersion(); return StringUtils.equals(subscribedGroup, providerGroup) && StringUtils.equals(subscribedVersion, providerVersion); }
public GrpcURL getUrlParameter(String key) { GrpcURL u = getUrls().get(key); if (u != null) { return u; } String value = getParameterAndDecoded(key); if (value == null || value.length() == 0) { return null; } u = GrpcURL.valueOf(value); getUrls().put(key, u); return u; }
private static String toServicePath(GrpcURL url) { String name = url.getServiceInterface(); String group = url.getGroup(); return group + Constants.PATH_SEPARATOR + GrpcURL.encode(name); }
public static String toRoutePath(GrpcURL url) { String name = url.getServiceInterface(); String group = url.getGroup(); return Constants.CONSUL_SERVICE_PRE + group + Constants.PATH_SEPARATOR + name; }
String service = providerUrl.getServiceInterface(); // 获取服务名称 String method = this.method.getName(); // 获取方法名 String consumer = this.remote;// 远程服务器地址 String host = providerUrl.getHost(); int rpcPort = providerUrl.getPort(); int registryRpcPort = providerUrl.getParameter(Constants.REGISTRY_RPC_PORT_KEY, rpcPort); salukiMonitor.collect(new GrpcURL(Constants.MONITOR_PROTOCOL, host, // MonitorService.APPLICATION, providerUrl.getParameter(Constants.APPLICATION_NAME), //
@Override public void collect(GrpcURL statistics) { if (!Constants.MONITOR_PROTOCOL.equals(statistics.getProtocol())) { return; GrpcInvoke invoke = new GrpcInvoke(); invoke.setId(MonitorUtil.createUUID()); if (statistics.hasParameter(PROVIDER)) { invoke.setType(CONSUMER); invoke.setConsumer(statistics.getAddress()); invoke.setProvider(statistics.getParameter(PROVIDER)); } else { invoke.setType(PROVIDER); invoke.setConsumer(statistics.getParameter(CONSUMER)); invoke.setProvider(statistics.getAddress()); invoke.setInvokeDate(new Date(Long.valueOf(statistics.getParameter(TIMESTAMP)))); invoke.setApplication(statistics.getParameter(APPLICATION)); invoke.setService(statistics.getServiceInterface()); invoke.setMethod(statistics.getParameter(METHOD)); invoke.setConcurrent(statistics.getParameter(CONCURRENT, 1)); invoke.setMaxElapsed(statistics.getParameter(MAX_ELAPSED, 0)); invoke.setMaxConcurrent(statistics.getParameter(MAX_CONCURRENT, 0)); invoke.setMaxInput(statistics.getParameter(MAX_INPUT, 0)); invoke.setMaxOutput(statistics.getParameter(MAX_OUTPUT, 0)); invoke.setSuccess(statistics.getParameter(SUCCESS, 0)); invoke.setFailure(statistics.getParameter(FAILURE, 0)); invoke.setElapsed(statistics.getParameter(MonitorService.ELAPSED, 0)); invoke.setInput(statistics.getParameter(MonitorService.INPUT, 0)); invoke.setOutput(statistics.getParameter(MonitorService.OUTPUT, 0));
public String getVersion() { String group = getParameter(Constants.VERSION_KEY, Constants.DEFAULT_VERSION); return group; }
private ConsulService buildConsulHealthService(GrpcURL url) { return ConsulService.newSalukiService()// .withAddress(url.getHost())// .withPort(Integer.valueOf(url.getPort()).toString())// .withName(GrpcURLUtils.toServiceName(url.getGroup()))// .withTag(GrpcURLUtils.healthServicePath(url, ThrallRoleType.PROVIDER))// .withId(url.getHost() + ":" + url.getPort() + "-" + url.getPath() + "-" + url.getVersion())// .withCheckInterval(Integer.valueOf(ConsulConstants.TTL).toString()).build(); }
/** * ==============help method============= */ private Triple<String, String, String> getmachineInfo(String providerAndConsumerKv, String groupService) { String thralUrl = consulClient.getKVValue(providerAndConsumerKv).getValue().getDecodedValue(); GrpcURL url = GrpcURL.valueOf(thralUrl); String flagAndIp = StringUtils.remove(providerAndConsumerKv, groupService + "/"); String[] serverInfos = StringUtils.split(flagAndIp, "/"); String machineFlag = serverInfos[1]; return new ImmutableTriple<String, String, String>(machineFlag, url.getAddress(), url.getParameter(Constants.HTTP_PORT_KEY)); } private Triple<String, String, String> getPortHostService(String serviceId) {
public short getParameter(String key, short defaultValue) { Number n = getNumbers().get(key); if (n != null) { return n.shortValue(); } String value = getParameter(key); if (value == null || value.length() == 0) { return defaultValue; } short s = Short.parseShort(value); getNumbers().put(key, s); return s; }
@Override public String getServiceName() { return refUrl.getServiceInterface(); }
host = getIp(); } else { host = getHost(); path = getServiceKey(); } else { path = getPath(); buildParameters(buf, true, parameters);
public static String filterLocalHost(String host) { if (host == null || host.length() == 0) { return host; } if (host.contains("://")) { GrpcURL u = GrpcURL.valueOf(host); if (NetUtils.isInvalidLocalHost(u.getHost())) { return u.setHost(NetUtils.getLocalHost()).toFullString(); } } else if (host.contains(":")) { int i = host.lastIndexOf(':'); if (NetUtils.isInvalidLocalHost(host.substring(0, i))) { return NetUtils.getLocalHost() + host.substring(i); } } else { if (NetUtils.isInvalidLocalHost(host)) { return NetUtils.getLocalHost(); } } return host; }
public String getServiceKey() { String inf = getServiceInterface(); if (inf == null) return null; StringBuilder buf = new StringBuilder(); String group = getGroup(); if (group != null && group.length() > 0) { buf.append(group).append("/"); } buf.append(inf); String version = getVersion(); if (version != null && version.length() > 0) { buf.append(":").append(version); } return buf.toString(); }
@Override public List<GrpcURL> discover(GrpcURL url) { String group = url.getGroup(); try { Map<String, List<GrpcURL>> providerUrls = serviceCache.get(group, new Callable<Map<String, List<GrpcURL>>>() { @Override public Map<String, List<GrpcURL>> call() throws Exception { return lookupServiceUpdate(group); } }); return providerUrls.get(url.getServiceKey()); } catch (ExecutionException e) { log.error(e.getMessage(), e); } return null; }
public String getParameterAndDecoded(String key, String defaultValue) { return decode(getParameter(key, defaultValue)); }
private GrpcURL buildURL(ConsulService service) { try { for (String tag : service.getTags()) { if (StringUtils.indexOf(tag, Constants.PROVIDERS_CATEGORY) != -1) { String toUrlPath = StringUtils.substringAfter(tag, Constants.PROVIDERS_CATEGORY); GrpcURL salukiUrl = GrpcURL.valueOf(GrpcURL.decode(toUrlPath)); return salukiUrl; } } } catch (Exception e) { log.error("convert consul service to url fail! service:" + service, e); } return null; }
public String getLocalAddressString() { return this.providerUrl.getAddress(); }