Search in sources :

Example 1 with EurekaJerseyClient

use of com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient in project eureka by Netflix.

the class JerseyReplicationClient method createReplicationClient.

public static JerseyReplicationClient createReplicationClient(EurekaServerConfig config, ServerCodecs serverCodecs, String serviceUrl) {
    String name = JerseyReplicationClient.class.getSimpleName() + ": " + serviceUrl + "apps/: ";
    EurekaJerseyClient jerseyClient;
    try {
        String hostname;
        try {
            hostname = new URL(serviceUrl).getHost();
        } catch (MalformedURLException e) {
            hostname = serviceUrl;
        }
        String jerseyClientName = "Discovery-PeerNodeClient-" + hostname;
        EurekaJerseyClientBuilder clientBuilder = new EurekaJerseyClientBuilder().withClientName(jerseyClientName).withUserAgent("Java-EurekaClient-Replication").withEncoderWrapper(serverCodecs.getFullJsonCodec()).withDecoderWrapper(serverCodecs.getFullJsonCodec()).withConnectionTimeout(config.getPeerNodeConnectTimeoutMs()).withReadTimeout(config.getPeerNodeReadTimeoutMs()).withMaxConnectionsPerHost(config.getPeerNodeTotalConnectionsPerHost()).withMaxTotalConnections(config.getPeerNodeTotalConnections()).withConnectionIdleTimeout(config.getPeerNodeConnectionIdleTimeoutSeconds());
        if (serviceUrl.startsWith("https://") && "true".equals(System.getProperty("com.netflix.eureka.shouldSSLConnectionsUseSystemSocketFactory"))) {
            clientBuilder.withSystemSSLConfiguration();
        }
        jerseyClient = clientBuilder.build();
    } catch (Throwable e) {
        throw new RuntimeException("Cannot Create new Replica Node :" + name, e);
    }
    String ip = null;
    try {
        ip = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        logger.warn("Cannot find localhost ip", e);
    }
    ApacheHttpClient4 jerseyApacheClient = jerseyClient.getClient();
    jerseyApacheClient.addFilter(new DynamicGZIPContentEncodingFilter(config));
    EurekaServerIdentity identity = new EurekaServerIdentity(ip);
    jerseyApacheClient.addFilter(new EurekaIdentityHeaderFilter(identity));
    return new JerseyReplicationClient(jerseyClient, serviceUrl);
}
Also used : MalformedURLException(java.net.MalformedURLException) EurekaJerseyClientBuilder(com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl.EurekaJerseyClientBuilder) UnknownHostException(java.net.UnknownHostException) DynamicGZIPContentEncodingFilter(com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter) EurekaJerseyClient(com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient) URL(java.net.URL) EurekaIdentityHeaderFilter(com.netflix.discovery.EurekaIdentityHeaderFilter) EurekaServerIdentity(com.netflix.eureka.EurekaServerIdentity) ApacheHttpClient4(com.sun.jersey.client.apache4.ApacheHttpClient4)

Example 2 with EurekaJerseyClient

use of com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient in project eureka by Netflix.

the class DiscoveryClient method scheduleServerEndpointTask.

private void scheduleServerEndpointTask(EurekaTransport eurekaTransport, AbstractDiscoveryClientOptionalArgs args) {
    Collection<?> additionalFilters = args == null ? Collections.emptyList() : args.additionalFilters;
    EurekaJerseyClient providedJerseyClient = args == null ? null : args.eurekaJerseyClient;
    TransportClientFactories argsTransportClientFactories = null;
    if (args != null && args.getTransportClientFactories() != null) {
        argsTransportClientFactories = args.getTransportClientFactories();
    }
    // Ignore the raw types warnings since the client filter interface changed between jersey 1/2
    @SuppressWarnings("rawtypes") TransportClientFactories transportClientFactories = argsTransportClientFactories == null ? new Jersey1TransportClientFactories() : argsTransportClientFactories;
    Optional<SSLContext> sslContext = args == null ? Optional.empty() : args.getSSLContext();
    Optional<HostnameVerifier> hostnameVerifier = args == null ? Optional.empty() : args.getHostnameVerifier();
    // If the transport factory was not supplied with args, assume they are using jersey 1 for passivity
    eurekaTransport.transportClientFactory = providedJerseyClient == null ? transportClientFactories.newTransportClientFactory(clientConfig, additionalFilters, applicationInfoManager.getInfo(), sslContext, hostnameVerifier) : transportClientFactories.newTransportClientFactory(additionalFilters, providedJerseyClient);
    ApplicationsResolver.ApplicationsSource applicationsSource = new ApplicationsResolver.ApplicationsSource() {

        @Override
        public Applications getApplications(int stalenessThreshold, TimeUnit timeUnit) {
            long thresholdInMs = TimeUnit.MILLISECONDS.convert(stalenessThreshold, timeUnit);
            long delay = getLastSuccessfulRegistryFetchTimePeriod();
            if (delay > thresholdInMs) {
                logger.info("Local registry is too stale for local lookup. Threshold:{}, actual:{}", thresholdInMs, delay);
                return null;
            } else {
                return localRegionApps.get();
            }
        }
    };
    eurekaTransport.bootstrapResolver = EurekaHttpClients.newBootstrapResolver(clientConfig, transportConfig, eurekaTransport.transportClientFactory, applicationInfoManager.getInfo(), applicationsSource, endpointRandomizer);
    if (clientConfig.shouldRegisterWithEureka()) {
        EurekaHttpClientFactory newRegistrationClientFactory = null;
        EurekaHttpClient newRegistrationClient = null;
        try {
            newRegistrationClientFactory = EurekaHttpClients.registrationClientFactory(eurekaTransport.bootstrapResolver, eurekaTransport.transportClientFactory, transportConfig);
            newRegistrationClient = newRegistrationClientFactory.newClient();
        } catch (Exception e) {
            logger.warn("Transport initialization failure", e);
        }
        eurekaTransport.registrationClientFactory = newRegistrationClientFactory;
        eurekaTransport.registrationClient = newRegistrationClient;
    }
    // Configure new transport layer (candidate for injecting in the future)
    if (clientConfig.shouldFetchRegistry()) {
        EurekaHttpClientFactory newQueryClientFactory = null;
        EurekaHttpClient newQueryClient = null;
        try {
            newQueryClientFactory = EurekaHttpClients.queryClientFactory(eurekaTransport.bootstrapResolver, eurekaTransport.transportClientFactory, clientConfig, transportConfig, applicationInfoManager.getInfo(), applicationsSource, endpointRandomizer);
            newQueryClient = newQueryClientFactory.newClient();
        } catch (Exception e) {
            logger.warn("Transport initialization failure", e);
        }
        eurekaTransport.queryClientFactory = newQueryClientFactory;
        eurekaTransport.queryClient = newQueryClient;
    }
}
Also used : ApplicationsResolver(com.netflix.discovery.shared.resolver.aws.ApplicationsResolver) EurekaHttpClientFactory(com.netflix.discovery.shared.transport.EurekaHttpClientFactory) SSLContext(javax.net.ssl.SSLContext) EurekaJerseyClient(com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient) HostnameVerifier(javax.net.ssl.HostnameVerifier) Jersey1TransportClientFactories(com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories) EurekaHttpClient(com.netflix.discovery.shared.transport.EurekaHttpClient) Jersey1TransportClientFactories(com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories) TransportClientFactories(com.netflix.discovery.shared.transport.jersey.TransportClientFactories) TimeUnit(java.util.concurrent.TimeUnit)

Aggregations

EurekaJerseyClient (com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient)2 EurekaIdentityHeaderFilter (com.netflix.discovery.EurekaIdentityHeaderFilter)1 ApplicationsResolver (com.netflix.discovery.shared.resolver.aws.ApplicationsResolver)1 EurekaHttpClient (com.netflix.discovery.shared.transport.EurekaHttpClient)1 EurekaHttpClientFactory (com.netflix.discovery.shared.transport.EurekaHttpClientFactory)1 EurekaJerseyClientBuilder (com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl.EurekaJerseyClientBuilder)1 Jersey1TransportClientFactories (com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories)1 TransportClientFactories (com.netflix.discovery.shared.transport.jersey.TransportClientFactories)1 EurekaServerIdentity (com.netflix.eureka.EurekaServerIdentity)1 DynamicGZIPContentEncodingFilter (com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter)1 ApacheHttpClient4 (com.sun.jersey.client.apache4.ApacheHttpClient4)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 TimeUnit (java.util.concurrent.TimeUnit)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLContext (javax.net.ssl.SSLContext)1