【Java】用etcd做服務註冊和發現

來源:互聯網
上載者:User

最近嘗試了一下etcd來做服務的註冊發現

【etcd服務】

從etcd官網下載二進位檔案即可,分配了三台機器做叢集

10.0.1.98    etcd-001

10.0.1.205 etcd-002

10.0.1.182  etcd-003

然後用指令碼啟動服務

etcd --name etcd-002 --initial-advertise-peer-urls http://10.0.1.205:2380 --listen-peer-urls http://10.0.1.205:2380 --listen-client-urls http://10.0.1.205:2379,http://127.0.0.1:2379 --advertise-client-urls http://10.0.1.205:2379 --initial-cluster-token etcd-cluster --initial-cluster etcd-001=http://10.0.1.98:2380,etcd-002=http://10.0.1.205:2380,etcd-003=http://10.0.1.182:2380 --initial-cluster-state new


etcd --name etcd-001 --initial-advertise-peer-urls http://10.0.1.98:2380 --listen-peer-urls http://10.0.1.98:2380 --listen-client-urls http://10.0.1.98:2379,http://127.0.0.1:2379 --advertise-client-urls http://10.0.1.98:2379 --initial-cluster-token etcd-cluster --initial-cluster etcd-001=http://10.0.1.98:2380,etcd-002=http://10.0.1.205:2380,etcd-003=http://10.0.1.182:2380 --initial-cluster-state new


etcd --name etcd-003 --initial-advertise-peer-urls http://10.0.1.182:2380 --listen-peer-urls http://10.0.1.182:2380 --listen-client-urls http://10.0.1.182:2379,http://127.0.0.1:2379 --advertise-client-urls http://10.0.1.182:2379 --initial-cluster-token etcd-cluster --initial-cluster etcd-001=http://10.0.1.98:2380,etcd-002=http://10.0.1.205:2380,etcd-003=http://10.0.1.182:2380 --initial-cluster-state new

即可。


【服務發布】

etcd和zk不一樣,他自身沒有臨時節點,需要用戶端自己來實現。實現的大概邏輯是這樣的:


設定一個一段時間逾時的節點,比如60秒逾時,如果逾時了etcd上就找不到這個節點,

然後用戶端用一個更小的時間間隔重新整理這個節點的逾時時間,比如每隔40秒重新整理一次,重新把ttl設定成60秒。這樣就可以保證在etcd上只要服務存活節點就一定存在,當服務關掉的時候,節點過一陣就消失了。

當然,如果能探測到服務關閉發一個del給etcd主動刪除節點就更完美了。

在maven中添加依賴

 <dependency><groupId>org.mousio</groupId><artifactId>etcd4j</artifactId><version>2.13.0</version></dependency>


以spring boot上為例

package com.seeplant.etcd;import java.io.IOException;import java.net.URI;import java.util.concurrent.TimeoutException;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.seeplant.util.Property;import com.seeplant.util.ServerLogger;import mousio.client.retry.RetryOnce;import mousio.etcd4j.EtcdClient;import mousio.etcd4j.promises.EtcdResponsePromise;import mousio.etcd4j.responses.EtcdAuthenticationException;import mousio.etcd4j.responses.EtcdException;import mousio.etcd4j.responses.EtcdKeysResponse;@Component@Scope("singleton")public class EtcdUtil {    private EtcdClient client;    private final String serverName = Property.getProperty("serverName"); // 自訂的服務名字,我定義成roomServer    private final String dirString = "/roomServerList";    private final String zoneId = Property.getProperty("zeonId"); // 自訂的一個標識,我定義成1    private final String etcdKey = dirString + "/" + zoneId + "/" + serverName; // 這裡就是發布的節點    public EtcdUtil() {        int nodeCount = Integer.parseInt(Property.getProperty("etcdGroupNodeCount"));        URI[] uris = new URI[nodeCount]; // 對於叢集,把所有叢集節點地址加進來,etcd的代碼裡會輪詢這些地址來發布節點,直到成功        for (int iter = 0; iter < nodeCount; iter++) {            String urlString = Property.getProperty("etcdHost" + new Integer(iter).toString());            System.out.println(urlString);            uris[iter] = URI.create(urlString);        }        client = new EtcdClient(uris);        client.setRetryHandler(new RetryOnce(20)); //retry策略    }    public void regist() { // 註冊節點,放在程式啟動的入口        try { // 用put方法發布一個節點            EtcdResponsePromise<EtcdKeysResponse> p = client                     .putDir(etcdKey + "_" + Property.getProperty("serverIp") + "_" + Property.getProperty("serverPort"))                    .ttl(60).send();            p.get(); // 加上這個get()用來保證設定完成,走下一步,get會阻塞,由上面client的retry策略決定阻塞的方式            new Thread(new GuardEtcd()).start(); // 啟動一個守護線程來定時重新整理節點        } catch (Exception e) {            // TODO: handle exception            ServerLogger.log("etcd Server not available.");        }    }    public void destory() {        try {            EtcdResponsePromise<EtcdKeysResponse> p = client                    .deleteDir(                            etcdKey + "_" + Property.getProperty("serverIp") + "_" + Property.getProperty("serverPort"))                    .recursive().send();            p.get();            client.close();        } catch (IOException | EtcdException | EtcdAuthenticationException | TimeoutException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    private class GuardEtcd implements Runnable {        @Override        public void run() {            // TODO Auto-generated method stub            while (true) {                try {                    Thread.sleep(40*1000l);                                        client.refresh(                            etcdKey + "_" + Property.getProperty("serverIp") + "_" + Property.getProperty("serverPort"),                            60).send();                } catch (IOException | InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}
對於spring boot架構來說,可以實現CommandLineRunner來完成載入和銷毀的主動調用


package com.seeplant;import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import com.seeplant.etcd.EtcdUtil;import com.seeplant.netty.RoomServer;import com.seeplant.util.Property;import io.netty.channel.ChannelFuture;@SpringBootApplicationpublic class App implements CommandLineRunner{@Autowiredprivate RoomServer roomServer;@Autowiredprivate EtcdUtil etcdUtil;    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }        @Bean    public RoomServer roomServer() {    return new RoomServer(Integer.parseInt(Property.getProperty("serverPort")));    }@Overridepublic void run(String... args) throws Exception {    etcdUtil.regist();ChannelFuture future = roomServer.start();Runtime.getRuntime().addShutdownHook(new Thread(){@Overridepublic void run() {    roomServer.destroy();}});future.channel().closeFuture().syncUninterruptibly();}@PreDestroypublic void destory() {    etcdUtil.destory();}}


以上的retry策略是嘗試一次就放棄,另外還寫了一個retry策略是等待時間軸性增長策略,僅供參考

package com.seeplant.etcd;import com.seeplant.util.ServerLogger;import mousio.client.ConnectionState;import mousio.client.retry.RetryPolicy;/** * 為Etcd提供倍增的重試策略 * @author yuantao * */public class RetryDoublePolicy extends RetryPolicy {    private final int timeSlot; // 步長    private int stepTimes = 1; // 倍數        public RetryDoublePolicy(int startRetryTime) {        super(startRetryTime);        // TODO Auto-generated constructor stub        this.timeSlot = startRetryTime;    }    @Override    public boolean shouldRetry(ConnectionState connectionState) {        // TODO Auto-generated method stub        stepTimes *= 2;        connectionState.msBeforeRetry = stepTimes*timeSlot; // 重設時間        System.out.println("try " + stepTimes);                if (stepTimes > 128) {            ServerLogger.log("etcd connection failed");            stepTimes = 1;        }        return true;    }    }


以上是註冊服務,對於服務管理、負載平衡方,只需要按照http://127.0.0.1:2379/v2/keys/roomServerList/1/ 這種方式get就可以得到節點內容。比如我的服務發布的節點通過get返回的內容如下:

{
  "action": "get",
  "node": {
    "key": "/roomServerList/1",
    "dir": true,
    "nodes": [
      {
        "key": "/roomServerList/1/roomServer0_127.0.0.1_9090",
        "dir": true,
        "expiration": "2017-09-04T05:10:21.214199005Z",
        "ttl": 42,
        "modifiedIndex": 202,
        "createdIndex": 202
      }
    ],
    "modifiedIndex": 36,
    "createdIndex": 36
  }
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.