com.orbitz.consul.KeyValueClient Java Examples

The following examples show how to use com.orbitz.consul.KeyValueClient. 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: ITConsulConfigurationTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void shouldReadUpdated() {
    assertNull(provider.watcher.value());

    String hostAndPort = System.getProperty("consul.address", "127.0.0.1:8500");
    Consul consul = Consul.builder()
                          .withHostAndPort(HostAndPort.fromString(hostAndPort))
                          .withConnectTimeoutMillis(5000)
                          .build();
    KeyValueClient client = consul.keyValueClient();

    assertTrue(client.putValue("test-module.default.testKey", "1000"));

    for (String v = provider.watcher.value(); v == null; v = provider.watcher.value()) {
    }

    assertEquals("1000", provider.watcher.value());

    client.deleteKey("test-module.default.testKey");

    for (String v = provider.watcher.value(); v != null; v = provider.watcher.value()) {
    }

    assertNull(provider.watcher.value());
}
 
Example #2
Source File: ConsulModule.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final Binder binder) {
    loading();

    cfgs.forEach((id, cfg) -> {
        final Consul consul = build(cfg);
        binder.bind(Consul.class).annotatedWith(Names.named(CONSUL_PREFIX + id)).toInstance(consul);
        binder.bind(AgentClient.class).annotatedWith(Names.named(CONSUL_AGENT_CLIENT_PREFIX + id)).toInstance(consul.agentClient());
        binder.bind(HealthClient.class).annotatedWith(Names.named(CONSUL_HEALTH_CLIENT_PREFIX + id)).toInstance(consul.healthClient());
        binder.bind(KeyValueClient.class).annotatedWith(Names.named(CONSUL_KEY_VALUE_CLIENT_PREFIX + id)).toInstance(consul.keyValueClient());
        binder.bind(CatalogClient.class).annotatedWith(Names.named(CONSUL_CATALOG_CLIENT_PREFIX + id)).toInstance(consul.catalogClient());
        binder.bind(StatusClient.class).annotatedWith(Names.named(CONSUL_STATUS_CLIENT_PREFIX + id)).toInstance(consul.statusClient());
        binder.bind(SessionClient.class).annotatedWith(Names.named(CONSUL_SESSION_CLIENT_PREFIX + id)).toInstance(consul.sessionClient());
        binder.bind(EventClient.class).annotatedWith(Names.named(CONSUL_EVENT_CLIENT_PREFIX + id)).toInstance(consul.eventClient());
        binder.bind(PreparedQueryClient.class).annotatedWith(Names.named(CONSUL_PREPARED_QUERY_CLIENT_PREFIX + id))
                .toInstance(consul.preparedQueryClient());
        binder.bind(CoordinateClient.class).annotatedWith(Names.named(CONSUL_COORDINATE_CLIENT_PREFIX + id))
                .toInstance(consul.coordinateClient());
        binder.bind(OperatorClient.class).annotatedWith(Names.named(CONSUL_OPERATOR_CLIENT + id)).toInstance(consul.operatorClient());
    });

}
 
Example #3
Source File: ConsulConfigurationWatcherRegisterTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUnsubscribeWhenKeyRemoved() {
    cacheByKey = new ConcurrentHashMap<>();
    KVCache existedCache = mock(KVCache.class);
    cacheByKey.put("existedKey", existedCache);

    configItemKeyedByName = new ConcurrentHashMap<>();
    Whitebox.setInternalState(register, "cachesByKey", cacheByKey);
    Whitebox.setInternalState(register, "configItemKeyedByName", configItemKeyedByName);

    KVCache cache1 = mock(KVCache.class);
    KVCache cache2 = mock(KVCache.class);

    ArgumentCaptor<ConsulCache.Listener> listener1 = ArgumentCaptor.forClass(ConsulCache.Listener.class);
    ArgumentCaptor<ConsulCache.Listener> listener2 = ArgumentCaptor.forClass(ConsulCache.Listener.class);

    PowerMockito.mockStatic(KVCache.class);
    PowerMockito.when(KVCache.newCache(any(KeyValueClient.class), eq("key1"))).thenReturn(cache1);
    PowerMockito.when(KVCache.newCache(any(KeyValueClient.class), eq("key2"))).thenReturn(cache2);

    when(register.readConfig(any(Set.class))).thenCallRealMethod();

    register.readConfig(Sets.newHashSet("key1", "key2"));

    verify(cache1).addListener(listener1.capture());
    verify(cache2).addListener(listener2.capture());
    verify(existedCache).stop();
}
 
Example #4
Source File: ClusterSchedulerLoader.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
protected void init() {
    final Injector injector = Globals.get(Injector.class);
    clusterId = injector.getInstance(Key.get(String.class, Names.named(Keys.CLUSTER_ID)));
    nodeId = injector.getInstance(Key.get(String.class, Names.named(Keys.NODE_ID)));
    configure = injector.getInstance(Configure.class);
    kvClient = injector.getInstance(Key.get(KeyValueClient.class, Names.named(ConsulSources.KV_SCHEDULER_CLUSTER)));
    listener = injector.getInstance(SchedulerListener.class);
    nodeStatusSync = injector.getInstance(NodeStatusSyncScheduler.class);
}
 
Example #5
Source File: ApolloConsul.java    From apollo with Apache License 2.0 4 votes vote down vote up
public KeyValueClient getClient() {
    Consul consul = Consul.builder().withUrl(getURL()).build();

    return consul.keyValueClient();
}
 
Example #6
Source File: ConsulConfigurationWatcherRegisterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldUpdateCachesWhenNotified() {
    cacheByKey = new ConcurrentHashMap<>();
    configItemKeyedByName = new ConcurrentHashMap<>();
    Whitebox.setInternalState(register, "cachesByKey", cacheByKey);
    Whitebox.setInternalState(register, "configItemKeyedByName", configItemKeyedByName);

    KVCache cache1 = mock(KVCache.class);
    KVCache cache2 = mock(KVCache.class);

    ArgumentCaptor<ConsulCache.Listener> listener1 = ArgumentCaptor.forClass(ConsulCache.Listener.class);
    ArgumentCaptor<ConsulCache.Listener> listener2 = ArgumentCaptor.forClass(ConsulCache.Listener.class);

    PowerMockito.mockStatic(KVCache.class);
    PowerMockito.when(KVCache.newCache(any(KeyValueClient.class), eq("key1"))).thenReturn(cache1);
    PowerMockito.when(KVCache.newCache(any(KeyValueClient.class), eq("key2"))).thenReturn(cache2);

    when(register.readConfig(any(Set.class))).thenCallRealMethod();

    register.readConfig(Sets.newHashSet("key1", "key2"));

    verify(cache1).addListener(listener1.capture());
    verify(cache2).addListener(listener2.capture());

    listener1.getValue()
             .notify(ImmutableMap.of("key1", ImmutableValue.builder()
                                                           .createIndex(0)
                                                           .modifyIndex(0)
                                                           .lockIndex(0)
                                                           .key("key1")
                                                           .flags(0)
                                                           .value(BaseEncoding.base64().encode("val1".getBytes()))
                                                           .build()));
    listener2.getValue()
             .notify(ImmutableMap.of("key2", ImmutableValue.builder()
                                                           .createIndex(0)
                                                           .modifyIndex(0)
                                                           .lockIndex(0)
                                                           .key("key2")
                                                           .flags(0)
                                                           .value(BaseEncoding.base64().encode("val2".getBytes()))
                                                           .build()));

    assertEquals(2, configItemKeyedByName.size());
    assertEquals("val1", configItemKeyedByName.get("key1").get());
    assertEquals("val2", configItemKeyedByName.get("key2").get());
}