Search in sources :

Example 1 with Key

use of com.netflix.eureka.registry.Key in project eureka by Netflix.

the class ApplicationsResource method getContainerDifferential.

/**
     * Get information about all delta changes in {@link com.netflix.discovery.shared.Applications}.
     *
     * <p>
     * The delta changes represent the registry information change for a period
     * as configured by
     * {@link EurekaServerConfig#getRetentionTimeInMSInDeltaQueue()}. The
     * changes that can happen in a registry include
     * <em>Registrations,Cancels,Status Changes and Expirations</em>. Normally
     * the changes to the registry are infrequent and hence getting just the
     * delta will be much more efficient than getting the complete registry.
     * </p>
     *
     * <p>
     * Since the delta information is cached over a period of time, the requests
     * may return the same data multiple times within the window configured by
     * {@link EurekaServerConfig#getRetentionTimeInMSInDeltaQueue()}.The clients
     * are expected to handle this duplicate information.
     * <p>
     *
     * @param version the version of the request.
     * @param acceptHeader the accept header to indicate whether to serve  JSON or XML data.
     * @param acceptEncoding the accept header to indicate whether to serve compressed or uncompressed data.
     * @param eurekaAccept an eureka accept extension, see {@link com.netflix.appinfo.EurekaAccept}
     * @param uriInfo  the {@link java.net.URI} information of the request made.
     * @return response containing the delta information of the
     *         {@link AbstractInstanceRegistry}.
     */
@Path("delta")
@GET
public Response getContainerDifferential(@PathParam("version") String version, @HeaderParam(HEADER_ACCEPT) String acceptHeader, @HeaderParam(HEADER_ACCEPT_ENCODING) String acceptEncoding, @HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) String eurekaAccept, @Context UriInfo uriInfo, @Nullable @QueryParam("regions") String regionsStr) {
    boolean isRemoteRegionRequested = null != regionsStr && !regionsStr.isEmpty();
    // has been disabled, redirect clients to get all instances
    if ((serverConfig.shouldDisableDelta()) || (!registry.shouldAllowAccess(isRemoteRegionRequested))) {
        return Response.status(Status.FORBIDDEN).build();
    }
    String[] regions = null;
    if (!isRemoteRegionRequested) {
        EurekaMonitors.GET_ALL_DELTA.increment();
    } else {
        regions = regionsStr.toLowerCase().split(",");
        // So we don't have different caches for same regions queried in different order.
        Arrays.sort(regions);
        EurekaMonitors.GET_ALL_DELTA_WITH_REMOTE_REGIONS.increment();
    }
    CurrentRequestVersion.set(Version.toEnum(version));
    KeyType keyType = Key.KeyType.JSON;
    String returnMediaType = MediaType.APPLICATION_JSON;
    if (acceptHeader == null || !acceptHeader.contains(HEADER_JSON_VALUE)) {
        keyType = Key.KeyType.XML;
        returnMediaType = MediaType.APPLICATION_XML;
    }
    Key cacheKey = new Key(Key.EntityType.Application, ResponseCacheImpl.ALL_APPS_DELTA, keyType, CurrentRequestVersion.get(), EurekaAccept.fromString(eurekaAccept), regions);
    if (acceptEncoding != null && acceptEncoding.contains(HEADER_GZIP_VALUE)) {
        return Response.ok(responseCache.getGZIP(cacheKey)).header(HEADER_CONTENT_ENCODING, HEADER_GZIP_VALUE).header(HEADER_CONTENT_TYPE, returnMediaType).build();
    } else {
        return Response.ok(responseCache.get(cacheKey)).build();
    }
}
Also used : KeyType(com.netflix.eureka.registry.Key.KeyType) Key(com.netflix.eureka.registry.Key) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with Key

use of com.netflix.eureka.registry.Key in project eureka by Netflix.

the class ApplicationsResource method getContainers.

/**
     * Get information about all {@link com.netflix.discovery.shared.Applications}.
     *
     * @param version the version of the request.
     * @param acceptHeader the accept header to indicate whether to serve JSON or XML data.
     * @param acceptEncoding the accept header to indicate whether to serve compressed or uncompressed data.
     * @param eurekaAccept an eureka accept extension, see {@link com.netflix.appinfo.EurekaAccept}
     * @param uriInfo the {@link java.net.URI} information of the request made.
     * @param regionsStr A comma separated list of remote regions from which the instances will also be returned.
     *                   The applications returned from the remote region can be limited to the applications
     *                   returned by {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)}
     *
     * @return a response containing information about all {@link com.netflix.discovery.shared.Applications}
     *         from the {@link AbstractInstanceRegistry}.
     */
@GET
public Response getContainers(@PathParam("version") String version, @HeaderParam(HEADER_ACCEPT) String acceptHeader, @HeaderParam(HEADER_ACCEPT_ENCODING) String acceptEncoding, @HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) String eurekaAccept, @Context UriInfo uriInfo, @Nullable @QueryParam("regions") String regionsStr) {
    boolean isRemoteRegionRequested = null != regionsStr && !regionsStr.isEmpty();
    String[] regions = null;
    if (!isRemoteRegionRequested) {
        EurekaMonitors.GET_ALL.increment();
    } else {
        regions = regionsStr.toLowerCase().split(",");
        // So we don't have different caches for same regions queried in different order.
        Arrays.sort(regions);
        EurekaMonitors.GET_ALL_WITH_REMOTE_REGIONS.increment();
    }
    // ready to serve traffic depending on various reasons.
    if (!registry.shouldAllowAccess(isRemoteRegionRequested)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    CurrentRequestVersion.set(Version.toEnum(version));
    KeyType keyType = Key.KeyType.JSON;
    String returnMediaType = MediaType.APPLICATION_JSON;
    if (acceptHeader == null || !acceptHeader.contains(HEADER_JSON_VALUE)) {
        keyType = Key.KeyType.XML;
        returnMediaType = MediaType.APPLICATION_XML;
    }
    Key cacheKey = new Key(Key.EntityType.Application, ResponseCacheImpl.ALL_APPS, keyType, CurrentRequestVersion.get(), EurekaAccept.fromString(eurekaAccept), regions);
    Response response;
    if (acceptEncoding != null && acceptEncoding.contains(HEADER_GZIP_VALUE)) {
        response = Response.ok(responseCache.getGZIP(cacheKey)).header(HEADER_CONTENT_ENCODING, HEADER_GZIP_VALUE).header(HEADER_CONTENT_TYPE, returnMediaType).build();
    } else {
        response = Response.ok(responseCache.get(cacheKey)).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) KeyType(com.netflix.eureka.registry.Key.KeyType) Key(com.netflix.eureka.registry.Key) GET(javax.ws.rs.GET)

Example 3 with Key

use of com.netflix.eureka.registry.Key in project eureka by Netflix.

the class AbstractVIPResource method getVipResponse.

protected Response getVipResponse(String version, String entityName, String acceptHeader, EurekaAccept eurekaAccept, Key.EntityType entityType) {
    if (!registry.shouldAllowAccess(false)) {
        return Response.status(Response.Status.FORBIDDEN).build();
    }
    CurrentRequestVersion.set(Version.toEnum(version));
    Key.KeyType keyType = Key.KeyType.JSON;
    if (acceptHeader == null || !acceptHeader.contains("json")) {
        keyType = Key.KeyType.XML;
    }
    Key cacheKey = new Key(entityType, entityName, keyType, CurrentRequestVersion.get(), eurekaAccept);
    String payLoad = responseCache.get(cacheKey);
    if (payLoad != null) {
        logger.debug("Found: {}", entityName);
        return Response.ok(payLoad).build();
    } else {
        logger.debug("Not Found: {}", entityName);
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : Key(com.netflix.eureka.registry.Key)

Example 4 with Key

use of com.netflix.eureka.registry.Key in project eureka by Netflix.

the class ApplicationResource method getApplication.

/**
     * Gets information about a particular {@link com.netflix.discovery.shared.Application}.
     *
     * @param version
     *            the version of the request.
     * @param acceptHeader
     *            the accept header of the request to indicate whether to serve
     *            JSON or XML data.
     * @return the response containing information about a particular
     *         application.
     */
@GET
public Response getApplication(@PathParam("version") String version, @HeaderParam("Accept") final String acceptHeader, @HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) String eurekaAccept) {
    if (!registry.shouldAllowAccess(false)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    EurekaMonitors.GET_APPLICATION.increment();
    CurrentRequestVersion.set(Version.toEnum(version));
    KeyType keyType = Key.KeyType.JSON;
    if (acceptHeader == null || !acceptHeader.contains("json")) {
        keyType = Key.KeyType.XML;
    }
    Key cacheKey = new Key(Key.EntityType.Application, appName, keyType, CurrentRequestVersion.get(), EurekaAccept.fromString(eurekaAccept));
    String payLoad = responseCache.get(cacheKey);
    if (payLoad != null) {
        logger.debug("Found: {}", appName);
        return Response.ok(payLoad).build();
    } else {
        logger.debug("Not Found: {}", appName);
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : KeyType(com.netflix.eureka.registry.Key.KeyType) Key(com.netflix.eureka.registry.Key) GET(javax.ws.rs.GET)

Aggregations

Key (com.netflix.eureka.registry.Key)4 KeyType (com.netflix.eureka.registry.Key.KeyType)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1