Search in sources :

Example 1 with RpcClient

use of com.alibaba.nacos.common.remote.client.RpcClient in project nacos by alibaba.

the class NamingGrpcClientProxyTest method testServerListChanged.

@Test
public void testServerListChanged() throws Exception {
    RpcClient rpc = new RpcClient("testServerListHasChanged", factory) {

        @Override
        public ConnectionType getConnectionType() {
            return ConnectionType.GRPC;
        }

        @Override
        public int rpcPortOffset() {
            return 0;
        }

        @Override
        public Connection connectToServer(ServerInfo serverInfo) throws Exception {
            return new Connection(serverInfo) {

                @Override
                public Response request(Request request, long timeoutMills) throws NacosException {
                    Response response = new Response() {
                    };
                    response.setRequestId(request.getRequestId());
                    return response;
                }

                @Override
                public RequestFuture requestFuture(Request request) throws NacosException {
                    return new DefaultRequestFuture("test", request.getRequestId());
                }

                @Override
                public void asyncRequest(Request request, RequestCallBack requestCallBack) throws NacosException {
                }

                @Override
                public void close() {
                }
            };
        }
    };
    Field rpcClient = NamingGrpcClientProxy.class.getDeclaredField("rpcClient");
    rpcClient.setAccessible(true);
    rpcClient.set(client, rpc);
    rpc.serverListFactory(factory);
    rpc.registerServerRequestHandler(new NamingPushRequestHandler(holder));
    Field listenerField = NamingGrpcClientProxy.class.getDeclaredField("redoService");
    listenerField.setAccessible(true);
    NamingGrpcRedoService listener = (NamingGrpcRedoService) listenerField.get(client);
    rpc.registerConnectionListener(listener);
    rpc.start();
    int retry = 10;
    while (!rpc.isRunning()) {
        TimeUnit.SECONDS.sleep(1);
        if (--retry < 0) {
            Assert.fail("rpc is not running");
        }
    }
    Assert.assertEquals(ORIGIN_SERVER, rpc.getCurrentServer().getServerIp());
    String newServer = "www.aliyun.com";
    when(factory.genNextServer()).thenReturn(newServer);
    when(factory.getServerList()).thenReturn(Stream.of(newServer, "anotherServer").collect(Collectors.toList()));
    NotifyCenter.publishEvent(new ServerListChangedEvent());
    retry = 10;
    while (ORIGIN_SERVER.equals(rpc.getCurrentServer().getServerIp())) {
        TimeUnit.SECONDS.sleep(1);
        if (--retry < 0) {
            Assert.fail("failed to auth switch server");
        }
    }
    Assert.assertEquals(newServer, rpc.getCurrentServer().getServerIp());
}
Also used : ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) DefaultRequestFuture(com.alibaba.nacos.api.remote.DefaultRequestFuture) NamingGrpcRedoService(com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService) RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) Connection(com.alibaba.nacos.common.remote.client.Connection) Request(com.alibaba.nacos.api.remote.request.Request) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) QueryServiceResponse(com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse) Response(com.alibaba.nacos.api.remote.response.Response) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) ServiceListResponse(com.alibaba.nacos.api.naming.remote.response.ServiceListResponse) InstanceResponse(com.alibaba.nacos.api.naming.remote.response.InstanceResponse) Field(java.lang.reflect.Field) RpcClient(com.alibaba.nacos.common.remote.client.RpcClient) Test(org.junit.Test)

Example 2 with RpcClient

use of com.alibaba.nacos.common.remote.client.RpcClient in project nacos by alibaba.

the class ClusterRpcClientProxy method refresh.

/**
 * init cluster rpc clients.
 *
 * @param members cluster server list member list.
 */
private void refresh(List<Member> members) throws NacosException {
    // ensure to create client of new members
    for (Member member : members) {
        if (MemberUtil.isSupportedLongCon(member)) {
            createRpcClientAndStart(member, ConnectionType.GRPC);
        }
    }
    // shutdown and remove old members.
    Set<Map.Entry<String, RpcClient>> allClientEntrys = RpcClientFactory.getAllClientEntries();
    Iterator<Map.Entry<String, RpcClient>> iterator = allClientEntrys.iterator();
    List<String> newMemberKeys = members.stream().filter(MemberUtil::isSupportedLongCon).map(this::memberClientKey).collect(Collectors.toList());
    while (iterator.hasNext()) {
        Map.Entry<String, RpcClient> next1 = iterator.next();
        if (next1.getKey().startsWith("Cluster-") && !newMemberKeys.contains(next1.getKey())) {
            Loggers.CLUSTER.info("member leave,destroy client of member - > : {}", next1.getKey());
            RpcClientFactory.getClient(next1.getKey()).shutdown();
            iterator.remove();
        }
    }
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) MemberUtil(com.alibaba.nacos.core.cluster.MemberUtil) HashMap(java.util.HashMap) Map(java.util.Map) RpcClient(com.alibaba.nacos.common.remote.client.RpcClient)

Example 3 with RpcClient

use of com.alibaba.nacos.common.remote.client.RpcClient in project nacos by alibaba.

the class ClusterRpcClientProxy method createRpcClientAndStart.

private void createRpcClientAndStart(Member member, ConnectionType type) throws NacosException {
    Map<String, String> labels = new HashMap<String, String>(2);
    labels.put(RemoteConstants.LABEL_SOURCE, RemoteConstants.LABEL_SOURCE_CLUSTER);
    String memberClientKey = memberClientKey(member);
    RpcClient client = buildRpcClient(type, labels, memberClientKey);
    if (!client.getConnectionType().equals(type)) {
        Loggers.CLUSTER.info(",connection type changed,destroy client of member - > : {}", member);
        RpcClientFactory.destroyClient(memberClientKey);
        client = buildRpcClient(type, labels, memberClientKey);
    }
    if (client.isWaitInitiated()) {
        Loggers.CLUSTER.info("start a new rpc client to member - > : {}", member);
        // one fixed server
        client.serverListFactory(new ServerListFactory() {

            @Override
            public String genNextServer() {
                return member.getAddress();
            }

            @Override
            public String getCurrentServer() {
                return member.getAddress();
            }

            @Override
            public List<String> getServerList() {
                return CollectionUtils.list(member.getAddress());
            }
        });
        client.start();
    }
}
Also used : ServerListFactory(com.alibaba.nacos.common.remote.client.ServerListFactory) HashMap(java.util.HashMap) List(java.util.List) RpcClient(com.alibaba.nacos.common.remote.client.RpcClient)

Aggregations

RpcClient (com.alibaba.nacos.common.remote.client.RpcClient)3 HashMap (java.util.HashMap)2 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)1 SubscribeServiceRequest (com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest)1 InstanceResponse (com.alibaba.nacos.api.naming.remote.response.InstanceResponse)1 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)1 ServiceListResponse (com.alibaba.nacos.api.naming.remote.response.ServiceListResponse)1 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)1 DefaultRequestFuture (com.alibaba.nacos.api.remote.DefaultRequestFuture)1 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)1 Request (com.alibaba.nacos.api.remote.request.Request)1 Response (com.alibaba.nacos.api.remote.response.Response)1 ServerListChangedEvent (com.alibaba.nacos.client.naming.event.ServerListChangedEvent)1 NamingGrpcRedoService (com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService)1 Connection (com.alibaba.nacos.common.remote.client.Connection)1 ServerListFactory (com.alibaba.nacos.common.remote.client.ServerListFactory)1 Member (com.alibaba.nacos.core.cluster.Member)1 MemberUtil (com.alibaba.nacos.core.cluster.MemberUtil)1 Field (java.lang.reflect.Field)1 List (java.util.List)1