All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.eureka2.example.client.SimpleApp Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.eureka2.example.client;

import com.netflix.eureka2.client.Eureka;
import com.netflix.eureka2.client.EurekaClient;
import com.netflix.eureka2.client.resolver.WriteServerResolverSet;
import com.netflix.eureka2.interests.ChangeNotification;
import com.netflix.eureka2.interests.Interests;
import com.netflix.eureka2.registry.InstanceInfo;
import com.netflix.eureka2.registry.InstanceInfo.Builder;
import com.netflix.eureka2.registry.InstanceInfo.Status;
import com.netflix.eureka2.registry.datacenter.BasicDataCenterInfo;
import rx.Subscriber;

/**
 * This example demonstrates how to register an application using {@link EurekaClient},
 * and how to access registry data.
 *
 * @author Tomasz Bak
 */
public final class SimpleApp {

    public static final InstanceInfo SERVICE_A = new Builder()
            .withId("id_serviceA")
            .withApp("ServiceA")
            .withAppGroup("ServiceA_1")
            .withStatus(Status.UP)
            .withDataCenterInfo(BasicDataCenterInfo.fromSystemData())
            .build();

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Local service info: " + SERVICE_A);

        WriteServerResolverSet writeServerResolverSet = WriteServerResolverSet.just("localhost", 13100, 13101);
        String readServerVip = "ReadServer";

        EurekaClient client = Eureka.newClientBuilder(writeServerResolverSet, readServerVip).build();

        client.forInterest(Interests.forApplications("WriteServer", "ReadServer", "ServiceA")).subscribe(
                new Subscriber>() {
                    @Override
                    public void onCompleted() {
                        System.out.println("Change notification stream closed");
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("Error in the notification channel: " + e);
                    }

                    @Override
                    public void onNext(ChangeNotification changeNotification) {
                        System.out.println("Received notification: " + changeNotification);
                    }
                });

        // Register client 1
        System.out.println("Registering SERVICE_A with Eureka...");
        client.register(SERVICE_A).toBlocking().singleOrDefault(null);
        Thread.sleep(1000);

        // Modify client 1 status
        System.out.println("Updating service status to DOWN...");
        InstanceInfo updatedInfo = new Builder().withInstanceInfo(SERVICE_A).withStatus(Status.DOWN).build();
        client.update(updatedInfo).toBlocking().singleOrDefault(null);
        Thread.sleep(1000);

        // Unregister client 1
        System.out.println("Unregistering SERVICE_A from Eureka...");
        client.unregister(updatedInfo).toBlocking().singleOrDefault(null);
        Thread.sleep(1000);

        Thread.sleep(5000);

        // Terminate both clients.
        System.out.println("Shutting down clients");
        client.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy