io.etcd.jetcd.KV Java Examples

The following examples show how to use io.etcd.jetcd.KV. 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: EtcdClusterUsingTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseEtcd() throws Exception {
    try (EtcdCluster etcd = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false)) {
        etcd.start();
        try (Client client = Client.builder().endpoints(etcd.getClientEndpoints()).build()) {
            try (KV kvClient = client.getKVClient()) {
                ByteSequence key = bytesOf("test_key");
                ByteSequence value = bytesOf("test_value");
                kvClient.put(key, value).get();

                CompletableFuture<GetResponse> getFuture = kvClient.get(key);
                GetResponse response = getFuture.get();
                List<KeyValue> values = response.getKvs();
                assertThat(values.size()).isEqualTo(1);
                KeyValue value1 = values.get(0);
                assertThat(value1.getValue()).isEqualTo(value);
                assertThat(value1.getKey()).isEqualTo(key);
            }
        }
    }
}
 
Example #2
Source File: EtcdDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadSource() throws Exception {
    EtcdDataSource dataSource = new EtcdDataSource("foo", value -> value);
    KV kvClient = Client.builder()
            .endpoints(endPoints)
            .build().getKVClient();

    kvClient.put(ByteSequence.from("foo".getBytes()), ByteSequence.from("test".getBytes()));
    Assert.assertNotNull(dataSource.readSource().equals("test"));

    kvClient.put(ByteSequence.from("foo".getBytes()), ByteSequence.from("test2".getBytes()));
    Assert.assertNotNull(dataSource.getProperty().equals("test2"));
}
 
Example #3
Source File: EtcdDataSourceTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testDynamicUpdate() throws InterruptedException {
    String demo_key = "etcd_demo_key";
    ReadableDataSource<String, List<FlowRule>> flowRuleEtcdDataSource = new EtcdDataSource<>(demo_key, (value) -> JSON.parseArray(value, FlowRule.class));
    FlowRuleManager.register2Property(flowRuleEtcdDataSource.getProperty());

    KV kvClient = Client.builder()
            .endpoints(endPoints)
            .build().getKVClient();

    final String rule1 = "[\n"
            + "  {\n"
            + "    \"resource\": \"TestResource\",\n"
            + "    \"controlBehavior\": 0,\n"
            + "    \"count\": 5.0,\n"
            + "    \"grade\": 1,\n"
            + "    \"limitApp\": \"default\",\n"
            + "    \"strategy\": 0\n"
            + "  }\n"
            + "]";

    kvClient.put(ByteSequence.from(demo_key.getBytes()), ByteSequence.from(rule1.getBytes()));
    Thread.sleep(1000);

    FlowRule flowRule = FlowRuleManager.getRules().get(0);
    Assert.assertTrue(flowRule.getResource().equals("TestResource"));
    Assert.assertTrue(flowRule.getCount() == 5.0);
    Assert.assertTrue(flowRule.getGrade() == 1);

    final String rule2 = "[\n"
            + "  {\n"
            + "    \"resource\": \"TestResource\",\n"
            + "    \"controlBehavior\": 0,\n"
            + "    \"count\": 6.0,\n"
            + "    \"grade\": 3,\n"
            + "    \"limitApp\": \"default\",\n"
            + "    \"strategy\": 0\n"
            + "  }\n"
            + "]";

    kvClient.put(ByteSequence.from(demo_key.getBytes()), ByteSequence.from(rule2.getBytes()));
    Thread.sleep(1000);

    flowRule = FlowRuleManager.getRules().get(0);
    Assert.assertTrue(flowRule.getResource().equals("TestResource"));
    Assert.assertTrue(flowRule.getCount() == 6.0);
    Assert.assertTrue(flowRule.getGrade() == 3);


}