CentOS install etcd 測試

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

1,關於etcd

etcd是一個類似於zookeeper的,服務發現工具。
參考簡介:
http://dockone.io/article/667
https://coreos.com/blog/performance-of-etcd.html
etcd效能對比:

2,搭建環境

在linux 下面:安裝最新的3.1.1

https://github.com/coreos/etcd/releases/download/v3.1.1/etcd-v3.1.1-linux-amd64.tar.gz

解壓縮。直接啟動。
./etcd version
./etcd
我使用的是virtualbox安裝的,因為是虛擬機器。所以使用了NAT的網路。遠端存取做了一個連接埠轉寄。

同時啟動一個etcd需要綁定到ip 10.0.2.15 ,預設啟動之後是綁定到了127.0.0.1這個ip。

./etcd --name test-etcd --advertise-client-urls http://10.0.2.15:2379 --listen-client-urls http://10.0.2.15:2379

不知道為啥要使用兩次命令,沒有直接叫 addr 的參數。
etcd 目前預設使用 2379 連接埠提供 HTTP API 服務,2380 連接埠和 peer 通訊。

3,使用java用戶端串連

https://github.com/coreos/jetcd
下載lib庫。依賴google的 grpc,protobuf。
maven 下載比較慢,可以使用ali的鏡像。

java代碼:

import com.coreos.jetcd.EtcdClient;import com.coreos.jetcd.EtcdClientBuilder;import com.coreos.jetcd.EtcdKV;import com.coreos.jetcd.api.KeyValue;import com.coreos.jetcd.api.RangeResponse;import com.coreos.jetcd.exception.AuthFailedException;import com.coreos.jetcd.exception.ConnectException;import com.google.common.util.concurrent.ListenableFuture;import com.google.protobuf.ByteString;import java.io.UnsupportedEncodingException;import java.util.List;import java.util.concurrent.ExecutionException;/** */public class TestJetcd {    public static void main(String[] args) throws ExecutionException, InterruptedException, UnsupportedEncodingException, AuthFailedException, ConnectException {        EtcdClient client = EtcdClientBuilder.newBuilder().endpoints("http://localhost:2379").build();        EtcdKV kvClient = client.getKVClient();        System.out.println(kvClient);        ByteString key = ByteString.copyFrom("test_key", "UTF-8");        ByteString value = ByteString.copyFrom("test_value", "UTF-8");//        System.out.println(kvClient.get(key).get());        // put the key-value        kvClient.put(key, value).get();        //        List<KeyValue> list = kvClient.get(key).get().getKvsList();        //迴圈顯示所有資料。        for (KeyValue keyValue : list) {            //轉換成utf-8 項目。            String tmpKey = keyValue.getKey().toStringUtf8();            System.out.println("key : " + tmpKey);            String tmpVal = keyValue.getValue().toStringUtf8();            System.out.println("value : " + tmpVal);            System.out.println("version : " + keyValue.getVersion());        }        // delete the key        //kvClient.delete(key).get();    }}

4,報錯

Exception in thread "main" java.util.concurrent.ExecutionException: io.grpc.StatusRuntimeException: UNAVAILABLE    at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:476)    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:455)

有的時候會這樣報錯。就得檢查下etcd服務啟動沒有。
看看virtualbox的連接埠通不通。
或者直接用http訪問就行。
http://127.0.0.1:2379/version

就是一個http調用。
其他命令參考:
http://cizixs.com/2016/08/02/intro-to-etcd
這個哥們寫的挺全的。

5,總結

本文的原文串連是: http://blog.csdn.net/freewebsys/article/details/56276386 未經博主允許不得轉載。
博主地址是:http://blog.csdn.net/freewebsys

etcd 的效能還是非常的強悍的。
java的用戶端使用的是grpc進行通訊的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.