Search in sources :

Example 1 with ServerListChangedEvent

use of com.alibaba.nacos.client.naming.event.ServerListChangedEvent 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 ServerListChangedEvent

use of com.alibaba.nacos.client.naming.event.ServerListChangedEvent in project nacos by alibaba.

the class ServerListManager method refreshServerListIfNeed.

private void refreshServerListIfNeed() {
    try {
        if (!CollectionUtils.isEmpty(serverList)) {
            NAMING_LOGGER.debug("server list provided by user: " + serverList);
            return;
        }
        if (System.currentTimeMillis() - lastServerListRefreshTime < refreshServerListInternal) {
            return;
        }
        List<String> list = getServerListFromEndpoint();
        if (CollectionUtils.isEmpty(list)) {
            throw new Exception("Can not acquire Nacos list");
        }
        if (null == serversFromEndpoint || !CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
            NAMING_LOGGER.info("[SERVER-LIST] server list is updated: " + list);
            serversFromEndpoint = list;
            lastServerListRefreshTime = System.currentTimeMillis();
            NotifyCenter.publishEvent(new ServerListChangedEvent());
        }
    } catch (Throwable e) {
        NAMING_LOGGER.warn("failed to update server list", e);
    }
}
Also used : ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) NacosLoadException(com.alibaba.nacos.api.exception.runtime.NacosLoadException) NacosException(com.alibaba.nacos.api.exception.NacosException) IOException(java.io.IOException)

Example 3 with ServerListChangedEvent

use of com.alibaba.nacos.client.naming.event.ServerListChangedEvent in project nacos by alibaba.

the class AbstractNamingClientProxyTest method testGetSecurityHeaders.

@Test
public void testGetSecurityHeaders() {
    SecurityProxy sc = Mockito.mock(SecurityProxy.class);
    Properties props = new Properties();
    AbstractNamingClientProxy proxy = new AbstractNamingClientProxy(sc, props) {

        @Override
        public void onEvent(ServerListChangedEvent event) {
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return ServerListChangedEvent.class;
        }

        @Override
        public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        }

        @Override
        public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
        }

        @Override
        public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        }

        @Override
        public ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort, boolean healthyOnly) throws NacosException {
            return null;
        }

        @Override
        public Service queryService(String serviceName, String groupName) throws NacosException {
            return null;
        }

        @Override
        public void createService(Service service, AbstractSelector selector) throws NacosException {
        }

        @Override
        public boolean deleteService(String serviceName, String groupName) throws NacosException {
            return false;
        }

        @Override
        public void updateService(Service service, AbstractSelector selector) throws NacosException {
        }

        @Override
        public ListView<String> getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException {
            return null;
        }

        @Override
        public ServiceInfo subscribe(String serviceName, String groupName, String clusters) throws NacosException {
            return null;
        }

        @Override
        public void unsubscribe(String serviceName, String groupName, String clusters) throws NacosException {
        }

        @Override
        public boolean isSubscribed(String serviceName, String groupName, String clusters) throws NacosException {
            return false;
        }

        @Override
        public void updateBeatInfo(Set<Instance> modifiedInstances) {
        }

        @Override
        public boolean serverHealthy() {
            return false;
        }

        @Override
        public void shutdown() throws NacosException {
        }
    };
    String token = "aa";
    Mockito.when(sc.getAccessToken()).thenReturn(token);
    Map<String, String> securityHeaders = proxy.getSecurityHeaders();
    Assert.assertEquals(1, securityHeaders.size());
    Assert.assertEquals(token, securityHeaders.get(Constants.ACCESS_TOKEN));
}
Also used : ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) Set(java.util.Set) AbstractSelector(com.alibaba.nacos.api.selector.AbstractSelector) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Service(com.alibaba.nacos.api.naming.pojo.Service) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with ServerListChangedEvent

use of com.alibaba.nacos.client.naming.event.ServerListChangedEvent in project nacos by alibaba.

the class AbstractNamingClientProxyTest method testGetSpasHeaders.

@Test
public void testGetSpasHeaders() throws Exception {
    SecurityProxy sc = Mockito.mock(SecurityProxy.class);
    Properties props = new Properties();
    String ak = "aa";
    String sk = "bb";
    props.put(PropertyKeyConst.ACCESS_KEY, ak);
    props.put(PropertyKeyConst.SECRET_KEY, sk);
    AbstractNamingClientProxy proxy = new AbstractNamingClientProxy(sc, props) {

        @Override
        public void onEvent(ServerListChangedEvent event) {
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return ServerListChangedEvent.class;
        }

        @Override
        public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        }

        @Override
        public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
        }

        @Override
        public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        }

        @Override
        public ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort, boolean healthyOnly) throws NacosException {
            return null;
        }

        @Override
        public Service queryService(String serviceName, String groupName) throws NacosException {
            return null;
        }

        @Override
        public void createService(Service service, AbstractSelector selector) throws NacosException {
        }

        @Override
        public boolean deleteService(String serviceName, String groupName) throws NacosException {
            return false;
        }

        @Override
        public void updateService(Service service, AbstractSelector selector) throws NacosException {
        }

        @Override
        public ListView<String> getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException {
            return null;
        }

        @Override
        public ServiceInfo subscribe(String serviceName, String groupName, String clusters) throws NacosException {
            return null;
        }

        @Override
        public void unsubscribe(String serviceName, String groupName, String clusters) throws NacosException {
        }

        @Override
        public boolean isSubscribed(String serviceName, String groupName, String clusters) throws NacosException {
            return false;
        }

        @Override
        public void updateBeatInfo(Set<Instance> modifiedInstances) {
        }

        @Override
        public boolean serverHealthy() {
            return false;
        }

        @Override
        public void shutdown() throws NacosException {
        }
    };
    String serviceName = "aaa";
    Map<String, String> spasHeaders = proxy.getSpasHeaders(serviceName);
    Assert.assertEquals(4, spasHeaders.size());
    Assert.assertEquals(AppNameUtils.getAppName(), spasHeaders.get("app"));
    Assert.assertEquals(ak, spasHeaders.get("ak"));
    Assert.assertTrue(spasHeaders.get("data").endsWith("@@" + serviceName));
    String expectSign = SignUtil.sign(spasHeaders.get("data"), sk);
    Assert.assertEquals(expectSign, spasHeaders.get("signature"));
}
Also used : ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) Set(java.util.Set) AbstractSelector(com.alibaba.nacos.api.selector.AbstractSelector) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Service(com.alibaba.nacos.api.naming.pojo.Service) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ServerListChangedEvent (com.alibaba.nacos.client.naming.event.ServerListChangedEvent)4 Test (org.junit.Test)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 Service (com.alibaba.nacos.api.naming.pojo.Service)2 AbstractSelector (com.alibaba.nacos.api.selector.AbstractSelector)2 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)2 Properties (java.util.Properties)2 Set (java.util.Set)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 NacosLoadException (com.alibaba.nacos.api.exception.runtime.NacosLoadException)1 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