private Map bootstrapFromEureka()

in evcache-core/src/main/java/com/netflix/evcache/pool/SimpleNodeListProvider.java [96:184]


    private Map<ServerGroup, EVCacheServerGroupConfig> bootstrapFromEureka(String appName) throws IOException {
        
        if(env == null || region == null) return Collections.<ServerGroup, EVCacheServerGroupConfig> emptyMap();

        final String url = "http://discoveryreadonly." + region + ".dyn" + env + ".netflix.net:7001/v2/apps/" + appName;
        final CloseableHttpClient httpclient = HttpClients.createDefault();
        final long start = System.currentTimeMillis();
        PropertyRepository props = EVCacheConfig.getInstance().getPropertyRepository();
        CloseableHttpResponse httpResponse = null;
        try {
            final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("Accept", "application/json");
            httpGet.setConfig(requestConfig);
            httpResponse = httpclient.execute(httpGet);
            final int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (!(statusCode >= 200 && statusCode < 300)) {
                log.error("Status Code : " + statusCode + " for url " + url);
                return Collections.<ServerGroup, EVCacheServerGroupConfig> emptyMap();
            }
            
            final InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent(), Charset.defaultCharset());
            final JSONTokener js = new JSONTokener(in);
            final JSONObject jsonObj = new JSONObject(js);
            final JSONObject application = jsonObj.getJSONObject("application");
            final JSONArray instances = application.getJSONArray("instance");
            final Map<ServerGroup, EVCacheServerGroupConfig> serverGroupMap = new HashMap<ServerGroup, EVCacheServerGroupConfig>();
            final int securePort = Integer.parseInt(props.get("evcache.secure.port", String.class)
                    .orElse(EVCacheClientPool.DEFAULT_SECURE_PORT).get());

            for(int i = 0; i < instances.length(); i++) {
                final JSONObject instanceObj = instances.getJSONObject(i);
                final JSONObject metadataObj = instanceObj.getJSONObject("dataCenterInfo").getJSONObject("metadata");

                final String asgName = instanceObj.getString("asgName");
                final Property<Boolean> asgEnabled = props.get(asgName + ".enabled", Boolean.class).orElse(true);
                final boolean isSecure = props.get(asgName + ".use.secure", Boolean.class)
                        .orElseGet(appName + ".use.secure")
                        .orElseGet("evcache.use.secure")
                        .orElse(false).get();

                if (!asgEnabled.get()) {
                    if(log.isDebugEnabled()) log.debug("ASG " + asgName + " is disabled so ignoring it");
                    continue;
                }
                
                final String zone = metadataObj.getString("availability-zone");
                final ServerGroup rSet = new ServerGroup(zone, asgName);
                final String localIp = metadataObj.getString("local-ipv4");
                final JSONObject instanceMetadataObj = instanceObj.getJSONObject("metadata");
                final String evcachePortString = instanceMetadataObj.optString("evcache.port",
                        EVCacheClientPool.DEFAULT_PORT);
                final int evcachePort = Integer.parseInt(evcachePortString);
                final int port = isSecure ? securePort : evcachePort;

                EVCacheServerGroupConfig config = serverGroupMap.get(rSet);
                if(config == null) {
                    config = new EVCacheServerGroupConfig(rSet, new HashSet<InetSocketAddress>());
                    serverGroupMap.put(rSet, config);
//                    final ArrayList<Tag> tags = new ArrayList<Tag>(2);
//                    tags.add(new BasicTag(EVCacheMetricsFactory.CACHE, appName));
//                    tags.add(new BasicTag(EVCacheMetricsFactory.SERVERGROUP, rSet.getName()));
//                    EVCacheMetricsFactory.getInstance().getLongGauge(EVCacheMetricsFactory.CONFIG, tags).set(Long.valueOf(port));
                }

                final InetAddress add = InetAddresses.forString(localIp);
                final InetAddress inetAddress = InetAddress.getByAddress(localIp, add.getAddress());
                final InetSocketAddress address = new InetSocketAddress(inetAddress, port);
                config.getInetSocketAddress().add(address);
            }
            if (log.isDebugEnabled()) log.debug("Returning : " + serverGroupMap);
            return serverGroupMap;
        } catch (Exception e) {
            if (log.isDebugEnabled()) log.debug("URL : " + url + "; Timeout " + timeout, e);
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {

                }
            }
            final List<Tag> tagList = new ArrayList<Tag>(2);
            EVCacheMetricsFactory.getInstance().addAppNameTags(tagList, appName);
            if (log.isDebugEnabled()) log.debug("Total Time to execute " + url + " " + (System.currentTimeMillis() - start) + " msec.");
            EVCacheMetricsFactory.getInstance().getPercentileTimer(EVCacheMetricsFactory.INTERNAL_BOOTSTRAP_EUREKA, tagList, Duration.ofMillis(100)).record(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
        }
        return Collections.<ServerGroup, EVCacheServerGroupConfig> emptyMap();
    }