<NME> ExampleServiceImpl.java
<BEF> package com.github.wenweihu86.raft.example.server.service.impl;

import com.baidu.brpc.client.BrpcProxy;
import com.baidu.brpc.client.RpcClient;
import com.github.wenweihu86.raft.example.server.ExampleStateMachine;
import com.github.wenweihu86.raft.example.server.service.ExampleProto;
import com.github.wenweihu86.raft.example.server.service.ExampleService;
import com.github.wenweihu86.raft.RaftNode;
import com.github.wenweihu86.raft.proto.RaftProto;
import com.googlecode.protobuf.format.JsonFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wenweihu86 on 2017/5/9.
 */
public class ExampleServiceImpl implements ExampleService {

    private static final Logger LOG = LoggerFactory.getLogger(ExampleServiceImpl.class);
    private static JsonFormat jsonFormat = new JsonFormat();

    private RaftNode raftNode;
    private ExampleStateMachine stateMachine;

    public ExampleServiceImpl(RaftNode raftNode, ExampleStateMachine stateMachine) {
        this.raftNode = raftNode;
        this.stateMachine = stateMachine;
    }

    @Override
        ExampleProto.SetResponse.Builder responseBuilder = ExampleProto.SetResponse.newBuilder();
        // 如果自己不是leader，将写请求转发给leader
        if (raftNode.getLeaderId() <= 0) {
        if (raftNode.getLeaderId() <= 0) {
            responseBuilder.setSuccess(false);
        } else if (raftNode.getLeaderId() != raftNode.getLocalServer().getServerId()) {
            RpcClient rpcClient = raftNode.getPeerMap().get(raftNode.getLeaderId()).getRpcClient();
            ExampleService exampleService = BrpcProxy.getProxy(rpcClient, ExampleService.class);
            ExampleProto.SetResponse responseFromLeader = exampleService.set(request);
            responseBuilder.mergeFrom(responseFromLeader);
        } else {
            byte[] data = request.toByteArray();
            boolean success = raftNode.replicate(data, RaftProto.EntryType.ENTRY_TYPE_DATA);
            responseBuilder.setSuccess(success);
        }

        ExampleProto.SetResponse response = responseBuilder.build();
        LOG.info("set request, request={}, response={}", jsonFormat.printToString(request),
                jsonFormat.printToString(response));
        return response;
    }

    @Override
    public ExampleProto.GetResponse get(ExampleProto.GetRequest request) {
        ExampleProto.GetResponse response = stateMachine.get(request);
        LOG.info("get request, request={}, response={}", jsonFormat.printToString(request),
                jsonFormat.printToString(response));
        return response;
    }

}

<MSG> bugfix: RpcException: serviceInterface must not be set repeatedly

<DFF> @@ -2,6 +2,9 @@ package com.github.wenweihu86.raft.example.server.service.impl;
 
 import com.baidu.brpc.client.BrpcProxy;
 import com.baidu.brpc.client.RpcClient;
+import com.baidu.brpc.client.RpcClientOptions;
+import com.baidu.brpc.client.instance.Endpoint;
+import com.github.wenweihu86.raft.Peer;
 import com.github.wenweihu86.raft.example.server.ExampleStateMachine;
 import com.github.wenweihu86.raft.example.server.service.ExampleProto;
 import com.github.wenweihu86.raft.example.server.service.ExampleService;
@@ -11,6 +14,9 @@ import com.googlecode.protobuf.format.JsonFormat;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
 /**
  * Created by wenweihu86 on 2017/5/9.
  */
@@ -21,10 +27,35 @@ public class ExampleServiceImpl implements ExampleService {
 
     private RaftNode raftNode;
     private ExampleStateMachine stateMachine;
+    private int leaderId = -1;
+    private RpcClient leaderRpcClient = null;
+    private Lock leaderLock = new ReentrantLock();
 
     public ExampleServiceImpl(RaftNode raftNode, ExampleStateMachine stateMachine) {
         this.raftNode = raftNode;
         this.stateMachine = stateMachine;
+        onLeaderChangeEvent();
+    }
+
+    private void onLeaderChangeEvent() {
+        if (raftNode.getLeaderId() != -1
+                && raftNode.getLeaderId() != raftNode.getLocalServer().getServerId()
+                && leaderId != raftNode.getLeaderId()) {
+            leaderLock.lock();
+            if (leaderId != -1 && leaderRpcClient != null) {
+                leaderRpcClient.stop();
+                leaderRpcClient = null;
+                leaderId = -1;
+            }
+            leaderId = raftNode.getLeaderId();
+            Peer peer = raftNode.getPeerMap().get(leaderId);
+            Endpoint endpoint = new Endpoint(peer.getServer().getEndpoint().getHost(),
+                    peer.getServer().getEndpoint().getPort());
+            RpcClientOptions rpcClientOptions = new RpcClientOptions();
+            rpcClientOptions.setGlobalThreadPoolSharing(true);
+            leaderRpcClient = new RpcClient(endpoint, rpcClientOptions);
+            leaderLock.unlock();
+        }
     }
 
     @Override
@@ -34,8 +65,8 @@ public class ExampleServiceImpl implements ExampleService {
         if (raftNode.getLeaderId() <= 0) {
             responseBuilder.setSuccess(false);
         } else if (raftNode.getLeaderId() != raftNode.getLocalServer().getServerId()) {
-            RpcClient rpcClient = raftNode.getPeerMap().get(raftNode.getLeaderId()).getRpcClient();
-            ExampleService exampleService = BrpcProxy.getProxy(rpcClient, ExampleService.class);
+            onLeaderChangeEvent();
+            ExampleService exampleService = BrpcProxy.getProxy(leaderRpcClient, ExampleService.class);
             ExampleProto.SetResponse responseFromLeader = exampleService.set(request);
             responseBuilder.mergeFrom(responseFromLeader);
         } else {

bugfix: RpcException: serviceInterface must not be set repeatedly