Raft 协议重新设置 Ip

raft 节点在机器ip变动之后,可能出现选主不成功的问题。

解决方法

下面是 nacos 的 JRaft 解决方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  @Test
  public void test() throws IOException {
    String[] groupIds = {"naming_instance_metadata", "naming_persistent_service_v2", "naming_service_metadata"};
    String logPath = "/Users/ooooo/nacos/data/protocol/raft/%s/log";

    // 遍历 groupId
    for (String groupId : groupIds) {
      // logStorage
      LogStorage logStorage = new RocksDBLogStorage(String.format(logPath, groupId), new RaftOptions());
      // logStorageOptions
      LogStorageOptions logStorageOptions = new LogStorageOptions();
      logStorageOptions.setConfigurationManager(new ConfigurationManager());
      logStorageOptions.setLogEntryCodecFactory(LogEntryV2CodecFactory.getInstance());
      logStorageOptions.setGroupId(groupId);
      // 初始化
      boolean init = logStorage.init(logStorageOptions);
      if (init) {
        // 获取最后一个 index
        long lastLogIndex = logStorage.getLastLogIndex();
        System.out.println(lastLogIndex);
        // 新增配置
        LogEntry logEntry = new LogEntry();
        logEntry.setType(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
        logEntry.setPeers(Collections.singletonList(PeerId.parsePeer("127.0.0.1:7848")));
        // 添加到日志中
        boolean b = logStorage.appendEntry(logEntry);
        System.out.println(b);
      }
    }
  }
0%