nacos 的 cp 和 ap

若是隻有一個 nacos 節點,能夠直接以單機模式啓動,naming 功能是徹底正常的。app

也能夠以集羣模式啓動,配置文件中只配一個節點便可。可是,由於此時 nacos 的 raft 實現中,一個節點是選不了主的,因此 cp 模式的 raft 協議就不可用,即持久化的服務是沒法註冊的。可是,臨時服務是能夠註冊的。async

com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore.MasterElection#sendVoteide

拉票ui

public void sendVote() {

        RaftPeer local = peers.get(NetUtils.localServer());
        Loggers.RAFT.info("leader timeout, start voting,leader: {}, term: {}",
            JSON.toJSONString(getLeader()), local.term);

        peers.reset();

        local.term.incrementAndGet();
        local.voteFor = local.ip;
        local.state = RaftPeer.State.CANDIDATE;

        Map<String, String> params = new HashMap<String, String>(1);
        params.put("vote", JSON.toJSONString(local));
        for (final String server : peers.allServersWithoutMySelf()) {
            final String url = buildURL(server, API_VOTE);
            try {
                HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler<Integer>() {
                    @Override
                    public Integer onCompleted(Response response) throws Exception {
                        if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
                            Loggers.RAFT.error("NACOS-RAFT vote failed: {}, url: {}", response.getResponseBody(), url);
                            return 1;
                        }

                        RaftPeer peer = JSON.parseObject(response.getResponseBody(), RaftPeer.class);

                        Loggers.RAFT.info("received approve from peer: {}", JSON.toJSONString(peer));

                        peers.decideLeader(peer);

                        return 0;
                    }
                });
            } catch (Exception e) {
                Loggers.RAFT.warn("error while sending vote to server: {}", server);
            }
        }
    }
}

 

目前爲止,我還不知道,怎樣註冊持久化的服務,走 raft 協議。url

相關文章
相關標籤/搜索