11package com .github .wenweihu86 .raft .admin ;
22
3- import com .github .wenweihu86 .raft .proto .RaftMessage ;
3+ import com .baidu .brpc .client .BrpcProxy ;
4+ import com .baidu .brpc .client .RpcClient ;
5+ import com .baidu .brpc .client .RpcClientOptions ;
6+ import com .baidu .brpc .client .instance .Endpoint ;
7+ import com .github .wenweihu86 .raft .proto .RaftProto ;
48import com .github .wenweihu86 .raft .service .RaftClientService ;
5- import com .github .wenweihu86 .rpc .client .EndPoint ;
6- import com .github .wenweihu86 .rpc .client .RPCClient ;
7- import com .github .wenweihu86 .rpc .client .RPCClientOptions ;
8- import com .github .wenweihu86 .rpc .client .RPCProxy ;
9- import com .google .protobuf .util .JsonFormat ;
9+ import com .googlecode .protobuf .format .JsonFormat ;
1010import org .slf4j .Logger ;
1111import org .slf4j .LoggerFactory ;
1212
1818 */
1919public class RaftClientServiceProxy implements RaftClientService {
2020 private static final Logger LOG = LoggerFactory .getLogger (RaftClientServiceProxy .class );
21- private static final JsonFormat . Printer PRINTER = JsonFormat . printer (). omittingInsignificantWhitespace ();
21+ private static final JsonFormat jsonFormat = new JsonFormat ();
2222
23- private List <RaftMessage .Server > cluster ;
24- private RPCClient clusterRPCClient ;
23+ private List <RaftProto .Server > cluster ;
24+ private RpcClient clusterRPCClient ;
2525 private RaftClientService clusterRaftClientService ;
2626
27- private RaftMessage .Server leader ;
28- private RPCClient leaderRPCClient ;
27+ private RaftProto .Server leader ;
28+ private RpcClient leaderRPCClient ;
2929 private RaftClientService leaderRaftClientService ;
3030
31- private RPCClientOptions rpcClientOptions = new RPCClientOptions ();
31+ private RpcClientOptions rpcClientOptions = new RpcClientOptions ();
3232
3333 // servers format is 10.1.1.1:8888,10.2.2.2:9999
3434 public RaftClientServiceProxy (String ipPorts ) {
3535 rpcClientOptions .setConnectTimeoutMillis (1000 ); // 1s
3636 rpcClientOptions .setReadTimeoutMillis (3600000 ); // 1hour
3737 rpcClientOptions .setWriteTimeoutMillis (1000 ); // 1s
38- clusterRPCClient = new RPCClient (ipPorts , rpcClientOptions );
39- clusterRaftClientService = RPCProxy .getProxy (clusterRPCClient , RaftClientService .class );
38+ clusterRPCClient = new RpcClient (ipPorts , rpcClientOptions );
39+ clusterRaftClientService = BrpcProxy .getProxy (clusterRPCClient , RaftClientService .class );
4040 updateConfiguration ();
4141 }
4242
4343 @ Override
44- public RaftMessage .GetLeaderResponse getLeader (RaftMessage .GetLeaderRequest request ) {
44+ public RaftProto .GetLeaderResponse getLeader (RaftProto .GetLeaderRequest request ) {
4545 return clusterRaftClientService .getLeader (request );
4646 }
4747
4848 @ Override
49- public RaftMessage .GetConfigurationResponse getConfiguration (RaftMessage .GetConfigurationRequest request ) {
49+ public RaftProto .GetConfigurationResponse getConfiguration (RaftProto .GetConfigurationRequest request ) {
5050 return clusterRaftClientService .getConfiguration (request );
5151 }
5252
5353 @ Override
54- public RaftMessage .AddPeersResponse addPeers (RaftMessage .AddPeersRequest request ) {
55- RaftMessage .AddPeersResponse response = leaderRaftClientService .addPeers (request );
56- if (response != null && response .getResCode () == RaftMessage .ResCode .RES_CODE_NOT_LEADER ) {
54+ public RaftProto .AddPeersResponse addPeers (RaftProto .AddPeersRequest request ) {
55+ RaftProto .AddPeersResponse response = leaderRaftClientService .addPeers (request );
56+ if (response != null && response .getResCode () == RaftProto .ResCode .RES_CODE_NOT_LEADER ) {
5757 updateConfiguration ();
5858 response = leaderRaftClientService .addPeers (request );
5959 }
6060 return response ;
6161 }
6262
6363 @ Override
64- public RaftMessage .RemovePeersResponse removePeers (RaftMessage .RemovePeersRequest request ) {
65- RaftMessage .RemovePeersResponse response = leaderRaftClientService .removePeers (request );
66- if (response != null && response .getResCode () == RaftMessage .ResCode .RES_CODE_NOT_LEADER ) {
64+ public RaftProto .RemovePeersResponse removePeers (RaftProto .RemovePeersRequest request ) {
65+ RaftProto .RemovePeersResponse response = leaderRaftClientService .removePeers (request );
66+ if (response != null && response .getResCode () == RaftProto .ResCode .RES_CODE_NOT_LEADER ) {
6767 updateConfiguration ();
6868 response = leaderRaftClientService .removePeers (request );
6969 }
@@ -80,22 +80,22 @@ public void stop() {
8080 }
8181
8282 private boolean updateConfiguration () {
83- RaftMessage .GetConfigurationRequest request = RaftMessage .GetConfigurationRequest .newBuilder ().build ();
84- RaftMessage .GetConfigurationResponse response = clusterRaftClientService .getConfiguration (request );
85- if (response != null && response .getResCode () == RaftMessage .ResCode .RES_CODE_SUCCESS ) {
83+ RaftProto .GetConfigurationRequest request = RaftProto .GetConfigurationRequest .newBuilder ().build ();
84+ RaftProto .GetConfigurationResponse response = clusterRaftClientService .getConfiguration (request );
85+ if (response != null && response .getResCode () == RaftProto .ResCode .RES_CODE_SUCCESS ) {
8686 if (leaderRPCClient != null ) {
8787 leaderRPCClient .stop ();
8888 }
8989 leader = response .getLeader ();
90- leaderRPCClient = new RPCClient (convertEndPoint (leader .getEndPoint ()), rpcClientOptions );
91- leaderRaftClientService = RPCProxy .getProxy (leaderRPCClient , RaftClientService .class );
90+ leaderRPCClient = new RpcClient (convertEndPoint (leader .getEndpoint ()), rpcClientOptions );
91+ leaderRaftClientService = BrpcProxy .getProxy (leaderRPCClient , RaftClientService .class );
9292 return true ;
9393 }
9494 return false ;
9595 }
9696
97- private EndPoint convertEndPoint (RaftMessage . EndPoint endPoint ) {
98- return new EndPoint (endPoint .getHost (), endPoint .getPort ());
97+ private Endpoint convertEndPoint (RaftProto . Endpoint endPoint ) {
98+ return new Endpoint (endPoint .getHost (), endPoint .getPort ());
9999 }
100100
101101}
0 commit comments