@Autowired public SimpleMetricsContentFormatter() { hostName = NetUtils.getHostName(); if (StringUtils.isEmpty(hostName)) { hostName = NetUtils.getHostAddress(); } applicationName = DynamicPropertyFactory.getInstance().getStringProperty(METRICS_FILE_NAME_PREFIX, "metrics").get(); }
public static String getPublishAddress() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { return NetUtils.getHostAddress(); } // placeholder is network interface name if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { return NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostAddress(); } return publicAddressSetting; }
public ArrayList<IpPort> getIpPort() { DynamicStringProperty property = DynamicPropertyFactory.getInstance() .getStringProperty("cse.service.registry.address", "https://127.0.0.1:30100"); List<String> uriLsit = Arrays.asList(property.get().split(",")); ArrayList<IpPort> ipPortList = new ArrayList<IpPort>(); for (int i = 0; i < uriLsit.size(); i++) { try { URI uri = new URI(uriLsit.get(i)); StringBuilder sb = new StringBuilder(uri.getHost()); sb.append(':').append(uri.getPort() < 0 ? PROTOCOL_HTTP_PORT : uri.getPort()); this.ssl = uri.getScheme().startsWith("https"); ipPortList.add(NetUtils.parseIpPort(sb.toString())); } catch (Exception e) { LOGGER.error("cse.service.registry.address invalid : {}", uriLsit.get(i), e); } } return ipPortList; }
public static String getPublishHostName() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { return NetUtils.getHostName(); } if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { return NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostName(); } return publicAddressSetting; }
@Override public boolean canInit() { setListenAddressWithoutSchema(TransportConfig.getAddress()); URIEndpointObject ep = (URIEndpointObject) getEndpoint().getAddress(); if (ep == null) { return true; } if (!NetUtils.canTcpListen(ep.getSocketAddress().getAddress(), ep.getPort())) { log.info("can not listen {}, skip {}.", ep.getSocketAddress(), this.getClass().getName()); return false; } return true; }
protected void startListen(Future<Void> startFuture) { // 如果本地未配置grpc地址,则表示不必监听,只需要作为客户端使用即可 if (StringUtils.isEmpty(this.endpoint)) { LOGGER.warn("grpc listen address is not configured, will not listen."); startFuture.complete(); return; } Router mainRouter = Router.router(vertx); mainRouter.route().handler(new GrpcBodyHandler()); new GrpcServer(mainRouter); HttpServerOptions serverOptions = new HttpServerOptions(); serverOptions.setAcceptBacklog(ACCEPT_BACKLOG); serverOptions.setSendBufferSize(SEND_BUFFER_SIZE); serverOptions.setReceiveBufferSize(RECEIVE_BUFFER_SIZE); serverOptions.setUsePooledBuffers(true); String key = System.getProperty("store.key"); if (key != null && !key.isEmpty()) { serverOptions.setUseAlpn(true); serverOptions.setSsl(true); serverOptions.setKeyStoreOptions(new JksOptions().setPath(System.getProperty("store.key")) .setPassword(System.getProperty("store.pass"))); } HttpServer server = vertx.createHttpServer(serverOptions).requestHandler(mainRouter::accept); IpPort ipPort = NetUtils.parseIpPortFromURI(this.endpoint); if (ipPort == null) { LOGGER.error("wrong grpc listen address {}", this.endpoint); return; } startListen(server, ipPort, startFuture); }
/** * 将配置的URI转换为endpoint * addressWithoutSchema 配置的URI,没有schema部分 */ protected void setListenAddressWithoutSchema(String addressWithoutSchema, Map<String, String> pairs) { addressWithoutSchema = genAddressWithoutSchema(addressWithoutSchema, pairs); this.endpoint = new Endpoint(this, NetUtils.getRealListenAddress(getName(), addressWithoutSchema)); if (this.endpoint.getEndpoint() != null) { this.publishEndpoint = new Endpoint(this, RegistryUtils.getPublishAddress(getName(), addressWithoutSchema)); } else { this.publishEndpoint = null; } }
private static IpPort genPublishIpPort(String schema, IpPort ipPort) { String publicAddressSetting = DynamicPropertyFactory.getInstance() .getStringProperty(PUBLISH_ADDRESS, "") .get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { InetSocketAddress socketAddress = ipPort.getSocketAddress(); if (socketAddress.getAddress().isAnyLocalAddress()) { String host = NetUtils.getHostAddress(); LOGGER.warn("address {}, auto select a host address to publish {}:{}, maybe not the correct one", socketAddress, host, socketAddress.getPort()); return new IpPort(host, ipPort.getPort()); } return ipPort; } if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { publicAddressSetting = NetUtils .ensureGetInterfaceAddress( publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostAddress(); } String publishPortKey = PUBLISH_PORT.replace("{transport_name}", schema); int publishPortSetting = DynamicPropertyFactory.getInstance() .getIntProperty(publishPortKey, 0) .get(); int publishPort = publishPortSetting == 0 ? ipPort.getPort() : publishPortSetting; return new IpPort(publicAddressSetting, publishPort); }
/** * 对于配置为0.0.0.0的地址,通过查询网卡地址,转换为实际监听的地址。 */ public static String getPublishAddress(String schema, String address) { if (address == null) { return address; } try { URI originalURI = new URI(schema + "://" + address); IpPort ipPort = NetUtils.parseIpPort(originalURI.getAuthority()); if (ipPort == null) { LOGGER.warn("address {} not valid.", address); return null; } IpPort publishIpPort = genPublishIpPort(schema, ipPort); URIBuilder builder = new URIBuilder(originalURI); return builder.setHost(publishIpPort.getHostOrIp()).setPort(publishIpPort.getPort()).build().toString(); } catch (URISyntaxException e) { LOGGER.warn("address {} not valid.", address); return null; } }