mousio.etcd4j.EtcdClient Java Examples

The following examples show how to use mousio.etcd4j.EtcdClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ITEtcdConfigurationTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    final ApplicationConfiguration applicationConfiguration = new ApplicationConfiguration();
    loadConfig(applicationConfiguration);

    final ModuleManager moduleManager = new ModuleManager();
    moduleManager.init(applicationConfiguration);

    final String etcdHost = System.getProperty("etcd.host");
    final String etcdPort = System.getProperty("etcd.port");
    logger.info("etcdHost: {}, etcdPort: {}", etcdHost, etcdPort);
    Properties properties = new Properties();
    properties.setProperty("serverAddr", etcdHost + ":" + etcdPort);

    List<URI> uris = EtcdUtils.parseProp(properties);
    client = new EtcdClient(uris.toArray(new URI[] {}));

    provider = (EtcdConfigurationTestProvider) moduleManager.find(EtcdConfigurationTestModule.NAME).provider();

    assertNotNull(provider);
}
 
Example #2
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessAfterRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(sequence(FAILURE, SUCCESS));

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 10))
                .send();

        EtcdKeysResponse resp = promise.get();

        assertThat(resp.node.value).isEqualTo("bar");
        assertThat(promise.getException()).isNull();

    }

    verifyHttp(server).times(2,
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example #3
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessWithoutRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(SUCCESS);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 10))
                .send();

        EtcdKeysResponse resp = promise.get();

        assertThat(resp.node.value).isEqualTo("bar");
        assertThat(promise.getException()).isNull();

    }

    verifyHttp(server).once(
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example #4
Source File: EtcdKeystoreTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testSslCliAgainstRegularEtcd() throws SecurityContextException, URISyntaxException, IOException, EtcdAuthenticationException, TimeoutException, EtcdException {
    expectedEx.expect(IOException.class);
    expectedEx.expectMessage(matchesRegex("DecoderException: io.netty.handler.ssl.NotSslRecordException"));

    EtcdClient etcd = new EtcdClient(SecurityContextBuilder.forKeystoreAndTruststore(
            KEYSTORE_PATH,
            KEYSTORE_PASS,
            TRUSTSTORE_PATH,
            TRUSTSTORE_PASS
    ));

    // expected not to work when using certificates against a non-secured etcd
    etcd.put("/test", "1234").send().get();
}
 
Example #5
Source File: EtcdNettyClientTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomEtcdNettyClient() throws Exception {
  NioEventLoopGroup evl = new NioEventLoopGroup();
  EtcdNettyConfig config = new EtcdNettyConfig()
      .setConnectTimeout(100)
      .setSocketChannelClass(NioSocketChannel.class)
      .setMaxFrameSize(1024 * 1024)
      .setEventLoopGroup(evl)
      .setHostName("localhost");


  URI[] endpoints = CLUSTER.endpoints();
  EtcdNettyClient client = new EtcdNettyClient(config, endpoints);
  EtcdClient etcdClient = new EtcdClient(client);
  etcdClient.setRetryHandler(new RetryNTimes(0, 0));

  assertNotNull(etcdClient.version());
}
 
Example #6
Source File: EtcdNettyClientTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testAuth() throws Exception {
  URI[] endpoints = CLUSTER.endpoints();
  EtcdClient client = new EtcdClient("test", "test", endpoints);
  assertNotNull(client.get("/test/messages").send().get());
}
 
Example #7
Source File: EtcdCoordinator.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void renew(EtcdClient client, String key, String json) {
    service.scheduleAtFixedRate(() -> {
        try {
            client.refresh(key, KEY_TTL).send().get();
        } catch (Exception e) {
            try {
                client.put(key, json).ttl(KEY_TTL).send().get();
            } catch (Exception ee) {
                logger.error(ee.getMessage(), ee);
            }
        }
    }, 5 * 1000, 30 * 1000, TimeUnit.MILLISECONDS);
}
 
Example #8
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
public EtcdClientWrapper(EtcdClient etcd, String prefix) {
    this.etcd = etcd;
    this.prefix = prefix;
    //possibly we need to create better id ob bus
    this.bus = MessageBusImpl.builder(KvStorageEvent.class, (s) -> new ConditionalMessageBusWrapper<>(s, KvStorageEvent::getKey, KvUtils::predicate))
      .id(getClass().getName())
      .build();
    this.executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
      .setNameFormat(getClass().getName() + "-bus-%d")
      .setDaemon(true)
      .build());
    eventWhirligig(-1);
}
 
Example #9
Source File: ITClusterEtcdPluginTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
    String etcdHost = System.getProperty("etcd.host");
    String port = System.getProperty("etcd.port");
    String baseUrl = "http://" + etcdHost + ":" + port;
    LOGGER.info("etcd baseURL: {}", baseUrl);
    etcdConfig = new ClusterModuleEtcdConfig();
    etcdConfig.setServiceName(SERVICE_NAME);
    client = new EtcdClient(URI.create(baseUrl));
    coordinator = new EtcdCoordinator(etcdConfig, client);
}
 
Example #10
Source File: EtcdKeystoreTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
protected void cleanup(EtcdClient etcd) {
    try {
        for (EtcdKeysResponse.EtcdNode node: etcd.getAll().send().get().getNode().getNodes()) {
            if (node.isDir()) {
                etcd.deleteDir(node.key).recursive().send().get();
            } else {
                etcd.delete(node.key).send().get();
            }
        }
    } catch (Exception e) {}
}
 
Example #11
Source File: EtcdKeystoreTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testSslCliAgainstSslEtcd() throws SecurityContextException, URISyntaxException, IOException, EtcdAuthenticationException, TimeoutException, EtcdException {
    // expected to work only on a secured etcd
    EtcdClient etcd = new EtcdClient(SecurityContextBuilder.forKeystoreAndTruststore(
            KEYSTORE_PATH,
            KEYSTORE_PASS,
            TRUSTSTORE_PATH,
            TRUSTSTORE_PASS
    ), new URI(SECURED_ETCD_SERVICE));

    etcd.put("/test", "1234").send().get();
    assertNotNull(etcd.version());
    cleanup(etcd);
}
 
Example #12
Source File: EtcdKeystoreTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test(timeout = 1000)
public void testCliAgainstSslEtcd() throws URISyntaxException, IOException, EtcdAuthenticationException, TimeoutException, EtcdException {
    expectedEx.expect(TestTimedOutException.class);
    EtcdClient etcd = new EtcdClient(new URI(SECURED_ETCD_SERVICE));
    etcd.put("/test", "1234").send().get();
}
 
Example #13
Source File: ITClusterModuleEtcdProviderFunctionalTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void unregisterRemoteOfCluster() throws Exception {
    final String serviceName = "unregister_remote_cluster";
    ModuleProvider providerA = createProvider(serviceName);
    ModuleProvider providerB = createProvider(serviceName);

    Address addressA = new Address("127.0.0.4", 1000, true);
    Address addressB = new Address("127.0.0.5", 1000, true);

    RemoteInstance instanceA = new RemoteInstance(addressA);
    RemoteInstance instanceB = new RemoteInstance(addressB);

    getClusterRegister(providerA).registerRemote(instanceA);
    getClusterRegister(providerB).registerRemote(instanceB);

    List<RemoteInstance> remoteInstancesOfA = queryRemoteNodes(providerA, 2);
    validateServiceInstance(addressA, addressB, remoteInstancesOfA);

    List<RemoteInstance> remoteInstancesOfB = queryRemoteNodes(providerB, 2);
    validateServiceInstance(addressB, addressA, remoteInstancesOfB);

    // unregister A
    EtcdClient client = Whitebox.getInternalState(providerA, "client");
    client.close();

    // only B
    remoteInstancesOfB = queryRemoteNodes(providerB, 1, 120);
    assertEquals(1, remoteInstancesOfB.size());
    Address address = remoteInstancesOfB.get(0).getAddress();
    assertEquals(address, addressB);
    assertTrue(addressB.isSelf());
}
 
Example #14
Source File: EtcdNettyClientTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test(expected = EtcdAuthenticationException.class)
public void testAuthFailure() throws Exception {
  URI[] endpoints = CLUSTER.endpoints();
  EtcdClient client = new EtcdClient("test", "test_", endpoints);
  client.get("/test/messages").send().get();
}
 
Example #15
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureWithoutRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(FAILURE);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 0))
                .send();

        Exception err = null;

        try {
            promise.get();
        } catch (Exception e) {
            err = e;
        }

        assertThat(err).isNotNull();
        assertThat(err).isEqualTo(promise.getException());

    }

    verifyHttp(server).once(
            method(Method.GET),
            uri("/v2/keys/foo")
    );

}
 
Example #16
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureAfterRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(FAILURE);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 3))
                .send();

        Exception err = null;

        try {
            promise.get();
        } catch (Exception e) {
            err = e;
        }

        assertThat(err).isNotNull();
        assertThat(err).isEqualTo(promise.getException());

    }

    verifyHttp(server).times(4,
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example #17
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.etcd = new EtcdClient(this.CLUSTER.endpoints());
    this.etcd.setRetryHandler(new RetryWithExponentialBackOff(20, 4, 10000));

    File file = new File("src/test/resources/test_data.json");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode mesos_sec = mapper.readTree(file);
    EtcdUtil.putAsJson("/etcd4j_test", mesos_sec, etcd);
}
 
Example #18
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAndPut() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    EtcdNettyConfig config = new EtcdNettyConfig();
    config.setMaxFrameSize(1024 * 1024); // Desired max size

    this.etcd.close();

    EtcdNettyClient nettyClient = new EtcdNettyClient(config, CLUSTER.endpoints());
    this.etcd = new EtcdClient(nettyClient);

    File testJson = new File("src/test/resources/test_data.json");
    JsonNode original = mapper.readTree(testJson);

    JsonNode fromEtcd = EtcdUtil.getAsJson("/etcd4j_test", this.etcd );

    // flatten both and compare
    Map<String, Object> rootFlat = new JsonFlattener(EtcdUtil.jsonToString(original))
            .withFlattenMode(FlattenMode.MONGODB)
            .withSeparator('/')
            .flattenAsMap();

    Map<String, Object> testFlat = new JsonFlattener(EtcdUtil.jsonToString(fromEtcd))
            .withFlattenMode(FlattenMode.MONGODB)
            .withSeparator('/')
            .flattenAsMap();

    assertEquals(rootFlat.size(), testFlat.size());
    for (Map.Entry<String, Object> entry : rootFlat.entrySet()) {
        assertNotNull(testFlat.get(entry.getKey()));
    }
}
 
Example #19
Source File: EtcdLeaderAutoConfiguration.java    From spring-cloud-cluster with Apache License 2.0 5 votes vote down vote up
@Bean
public EtcdClient etcdInstance() {
	String[] uriList = StringUtils.commaDelimitedListToStringArray(ecp.getConnect());
	URI[] uris = new URI[uriList.length];
	for (int i = 0; i < uriList.length; i ++) {
		uris[i] = URI.create(uriList[i]);
	}
	return new EtcdClient(uris);
}
 
Example #20
Source File: ClusterModuleEtcdProvider.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare() throws ServiceNotProvidedException, ModuleStartException {

    List<URI> uris = EtcdUtils.parse(config);

    //TODO check isSSL
    client = new EtcdClient(uris.toArray(new URI[] {}));
    EtcdCoordinator coordinator = new EtcdCoordinator(config, client);
    this.registerServiceImplementation(ClusterRegister.class, coordinator);
    this.registerServiceImplementation(ClusterNodesQuery.class, coordinator);
}
 
Example #21
Source File: EtcdConfiguration.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public EtcdClientWrapper client() {
    List<URI> uris = new ArrayList<>();
    for (String etcdUrl : etcdUrls) {
        uris.add(URI.create(etcdUrl));
    }
    log.info("About to connect to etcd: {}", (Object)etcdUrls);
    EtcdClient etcd = new EtcdClient(uris.toArray(new URI[uris.size()]));
    return new EtcdClientWrapper(etcd, prefix.trim());
}
 
Example #22
Source File: EtcdFlagFieldUpdater.java    From java-flagz with MIT License 5 votes vote down vote up
/** Init performs the initial read of values from etcd. */
public void init(String flagzDirectory) throws FlagException, EtcdFlagFieldUpdaterException {
  this.directoryPrefix = MoreObjects.firstNonNull(flagzDirectory, directoryFlag.get());
  client = new EtcdClient(uris.toArray(new URI[uris.size()]));
  client.setRetryHandler(retryPolicy);
  initialSetAllFlagz();
}
 
Example #23
Source File: EtcdFlagFieldUpdaterTest.java    From java-flagz with MIT License 5 votes vote down vote up
@BeforeClass
public static void connectEtcd() {
  try {
    client = new EtcdClient(URI.create(ETCD_SERVER));
    client.setRetryHandler(ETCD_RETRY);
    Long etcdIndex = client.getAll().send().get().etcdIndex;
    LOG.info("Connected to Etcd version={} at EtcdIndex({}).", client.version(), etcdIndex);
  } catch (Exception e) {
    throw new RuntimeException("Etc.d must be running on localhost for these tests.", e);
  }
}
 
Example #24
Source File: EtcdPropertySource.java    From spring-cloud-etcd with Apache License 2.0 5 votes vote down vote up
public EtcdPropertySource(String root, EtcdClient source, EtcdConfigProperties config) {
	super(root, source);
	this.properties = new HashMap<>();
	this.prefix = root.startsWith(EtcdConstants.PATH_SEPARATOR) ? root
			+ EtcdConstants.PATH_SEPARATOR : EtcdConstants.PATH_SEPARATOR + root
			+ EtcdConstants.PATH_SEPARATOR;
	this.config = config;
}
 
Example #25
Source File: EtcdAutoConfiguration.java    From spring-cloud-etcd with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public EtcdClient etcdClient() {
	// TODO: support ssl
	// TODO: support authentication
	return new EtcdClient(etcdProperties().getUris().toArray(new URI[] {}));
}
 
Example #26
Source File: EtcdConfigWatcherRegister.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public EtcdConfigWatcherRegister(EtcdServerSettings settings) {
    super(settings.getPeriod());
    this.settings = settings;
    this.configItemKeyedByName = new ConcurrentHashMap<>();
    this.client = new EtcdClient(EtcdUtils.parse(settings).toArray(new URI[] {}));
    this.listenersByKey = new ConcurrentHashMap<>();
    responsePromiseByKey = new ConcurrentHashMap<>();
}
 
Example #27
Source File: EtcdEndpoint.java    From spring-cloud-etcd with Apache License 2.0 4 votes vote down vote up
public EtcdEndpoint(EtcdClient etcd) {
	super("etcd", false, true);
	this.etcd = etcd;
}
 
Example #28
Source File: EtcdCoordinator.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public EtcdCoordinator(ClusterModuleEtcdConfig config, EtcdClient client) {
    this.config = config;
    this.client = client;
    this.serviceName = config.getServiceName();
}
 
Example #29
Source File: EtcdTests.java    From spring-cloud-cluster with Apache License 2.0 4 votes vote down vote up
@Bean
public EtcdClient etcdInstance() {
	return new EtcdClient(URI.create("http://localhost:4001"));
}
 
Example #30
Source File: EtcdTests.java    From spring-cloud-cluster with Apache License 2.0 4 votes vote down vote up
@Bean
public EtcdClient etcdInstance() {
	return new EtcdClient(URI.create("http://localhost:4001"));
}