Search in sources :

Example 1 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class StargateZookeeperNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    String path = buildParentNodePath(resolveGroup(subscribeInfo), subscribeInfo.getInterfaceName(), resolveVersion(subscribeInfo));
    List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
    try {
        List<String> childList = client.getChildren().forPath(path);
        for (String child : childList) {
            // 跨过所有客户端节点
            if (StargateConstants.ZK_CONSUMER_DIR.equals(child)) {
                continue;
            }
            String childPath = path + "/" + child;
            try {
                String childData = new String(client.getData().forPath(childPath));
                StargateURI uri = new StargateURI.Builder(childData).build();
                ServiceInstance instance = new ServiceInstance(uri.getHost(), uri.getPort());
                if (subscribeInfo != null && StringUtils.isNoneBlank(subscribeInfo.getServiceId())) {
                    instance.setServiceName(subscribeInfo.getServiceId());
                }
                instances.add(instance);
            } catch (Exception getDataFailedException) {
                log.warn("get child data failed, path:{}, ex:", childPath, getDataFailedException);
            }
        }
        log.info("lookup {} instances from {}", instances.size(), url);
    } catch (Exception ex) {
        log.warn("lookup service instance list failed from {}, msg={}", url, ex.getMessage());
        if (!subscribeInfo.isIgnoreFailOfNamingService()) {
            throw new RpcException("lookup end point list failed from zookeeper failed", ex);
        }
    }
    return instances;
}
Also used : StargateURI(com.baidu.brpc.protocol.stargate.StargateURI) RpcException(com.baidu.brpc.exceptions.RpcException) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 2 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class ZookeeperNamingServiceTest method testLookup.

@Test
public void testLookup() throws Exception {
    setUp();
    SubscribeInfo subscribeInfo = createSubscribeInfo(true);
    List<ServiceInstance> instances = namingService.lookup(subscribeInfo);
    Assert.assertTrue(instances.size() == 0);
    RegisterInfo registerInfo = createRegisterInfo("127.0.0.1", 8012);
    namingService.register(registerInfo);
    instances = namingService.lookup(subscribeInfo);
    Assert.assertTrue(instances.size() == 1);
    Assert.assertTrue(instances.get(0).getIp().equals("127.0.0.1"));
    Assert.assertTrue(instances.get(0).getPort() == 8012);
    namingService.unregister(registerInfo);
    tearDown();
}
Also used : SubscribeInfo(com.baidu.brpc.protocol.SubscribeInfo) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RegisterInfo(com.baidu.brpc.naming.RegisterInfo) Test(org.junit.Test)

Example 3 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class ZookeeperNamingServiceTest method testSubscribe.

@Test
public void testSubscribe() throws Exception {
    setUp();
    final List<ServiceInstance> adds = new ArrayList<ServiceInstance>();
    final List<ServiceInstance> deletes = new ArrayList<ServiceInstance>();
    SubscribeInfo subscribeInfo = createSubscribeInfo(false);
    namingService.subscribe(subscribeInfo, new NotifyListener() {

        @Override
        public void notify(Collection<ServiceInstance> addList, Collection<ServiceInstance> deleteList) {
            System.out.println("receive new subscribe info time:" + System.currentTimeMillis());
            System.out.println("add size:" + addList.size());
            for (ServiceInstance instance : addList) {
                System.out.println(instance);
            }
            adds.addAll(addList);
            System.out.println("delete size:" + deleteList.size());
            for (ServiceInstance instance : deleteList) {
                System.out.println(instance);
            }
            deletes.addAll(deleteList);
        }
    });
    RegisterInfo registerInfo = createRegisterInfo("127.0.0.1", 8013);
    namingService.register(registerInfo);
    System.out.println("register time=" + System.currentTimeMillis());
    Thread.sleep(1000);
    Assert.assertTrue(adds.size() == 1);
    Assert.assertTrue(deletes.size() == 0);
    Assert.assertTrue(adds.get(0).getIp().equals("127.0.0.1"));
    Assert.assertTrue(adds.get(0).getPort() == 8013);
    adds.clear();
    deletes.clear();
    namingService.unregister(registerInfo);
    System.out.println("unregister time=" + System.currentTimeMillis());
    Thread.sleep(1000);
    Assert.assertTrue(adds.size() == 0);
    Assert.assertTrue(deletes.size() == 1);
    Assert.assertTrue(deletes.get(0).getIp().equals("127.0.0.1"));
    Assert.assertTrue(deletes.get(0).getPort() == 8013);
    namingService.unsubscribe(subscribeInfo);
    tearDown();
}
Also used : SubscribeInfo(com.baidu.brpc.protocol.SubscribeInfo) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RegisterInfo(com.baidu.brpc.naming.RegisterInfo) NotifyListener(com.baidu.brpc.naming.NotifyListener) Test(org.junit.Test)

Example 4 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class ZookeeperNamingServiceTest method testSubscribeWhenZookeeperDownAndUp.

/**
 * This test must test under actual zookeeper server, Not the TestingServer of Curator
 */
@Test
@Ignore
public void testSubscribeWhenZookeeperDownAndUp() throws Exception {
    namingUrl = new BrpcURL("zookeeper://127.0.0.1:2181");
    namingService = new ZookeeperNamingService(namingUrl);
    final List<ServiceInstance> adds = new ArrayList<ServiceInstance>();
    final List<ServiceInstance> deletes = new ArrayList<ServiceInstance>();
    SubscribeInfo subscribeInfo = createSubscribeInfo(false);
    namingService.subscribe(subscribeInfo, new NotifyListener() {

        @Override
        public void notify(Collection<ServiceInstance> addList, Collection<ServiceInstance> deleteList) {
            System.out.println("receive new subscribe info time:" + System.currentTimeMillis());
            System.out.println("add size:" + addList.size());
            for (ServiceInstance instance : addList) {
                System.out.println(instance);
            }
            adds.addAll(addList);
            System.out.println("delete size:" + deleteList.size());
            for (ServiceInstance instance : deleteList) {
                System.out.println(instance);
            }
            deletes.addAll(deleteList);
        }
    });
    RegisterInfo registerInfo = createRegisterInfo("127.0.0.1", 8014);
    namingService.register(registerInfo);
    System.out.println("register time=" + System.currentTimeMillis());
    Thread.sleep(1000);
    Assert.assertTrue(adds.size() == 1);
    Assert.assertTrue(deletes.size() == 0);
    Assert.assertTrue(adds.get(0).getIp().equals("127.0.0.1"));
    Assert.assertTrue(adds.get(0).getPort() == 8014);
    adds.clear();
    deletes.clear();
    // sleep for restarting zookeeper
    Thread.sleep(30 * 1000);
    List<ServiceInstance> instances = namingService.lookup(subscribeInfo);
    Assert.assertTrue(instances.size() == 1);
    Assert.assertTrue(instances.get(0).getIp().equals("127.0.0.1"));
    Assert.assertTrue(instances.get(0).getPort() == 8014);
    namingService.unregister(registerInfo);
    System.out.println("unregister time=" + System.currentTimeMillis());
    Thread.sleep(1000);
    Assert.assertTrue(adds.size() == 0);
    Assert.assertTrue(deletes.size() == 1);
    Assert.assertTrue(deletes.get(0).getIp().equals("127.0.0.1"));
    Assert.assertTrue(deletes.get(0).getPort() == 8014);
    namingService.unsubscribe(subscribeInfo);
}
Also used : BrpcURL(com.baidu.brpc.naming.BrpcURL) SubscribeInfo(com.baidu.brpc.protocol.SubscribeInfo) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RegisterInfo(com.baidu.brpc.naming.RegisterInfo) NotifyListener(com.baidu.brpc.naming.NotifyListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class SpringCloudNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    List<org.springframework.cloud.client.ServiceInstance> discoveryInstances = discoveryClient.getInstances(subscribeInfo.getServiceId());
    List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
    for (org.springframework.cloud.client.ServiceInstance discoveryInstance : discoveryInstances) {
        String host = discoveryInstance.getHost();
        Integer port = Integer.valueOf(discoveryInstance.getMetadata().get(BrpcServiceRegistrationAutoConfiguration.META_DATA_PORT_KEY));
        ServiceInstance instance = new ServiceInstance(host, port);
        instances.add(instance);
    }
    return instances;
}
Also used : ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance)

Aggregations

ServiceInstance (com.baidu.brpc.client.channel.ServiceInstance)24 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)8 RpcException (com.baidu.brpc.exceptions.RpcException)7 RegisterInfo (com.baidu.brpc.naming.RegisterInfo)6 SubscribeInfo (com.baidu.brpc.protocol.SubscribeInfo)6 BrpcURL (com.baidu.brpc.naming.BrpcURL)4 NotifyListener (com.baidu.brpc.naming.NotifyListener)3 BaseMockitoTest (com.baidu.brpc.test.BaseMockitoTest)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)3 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)3 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)3 CommunicationClient (com.baidu.brpc.client.CommunicationClient)2 HttpRequest (com.baidu.brpc.protocol.HttpRequest)2 RpcServer (com.baidu.brpc.server.RpcServer)2 ByteBuf (io.netty.buffer.ByteBuf)2