Search in sources :

Example 11 with ServiceInstance

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

the class HttpRpcProtocol method addHostHeader.

public void addHostHeader(Request request) {
    ServiceInstance serviceInstance = request.getCommunicationClient().getServiceInstance();
    String hostPort = serviceInstance.getIp() + ":" + serviceInstance.getPort();
    HttpRequest httpRequest = (HttpRequest) request;
    // some http server decide what to do by the 'host' param in request header
    // TODO: check if it must need domain name when using DnsNamingService
    httpRequest.headers().set(HttpHeaderNames.HOST, hostPort);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(com.baidu.brpc.protocol.HttpRequest) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance)

Example 12 with ServiceInstance

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

the class ConsulNamingService method convert.

public List<ServiceInstance> convert(Response<List<HealthService>> consulServices) {
    if (consulServices == null || consulServices.getValue() == null || consulServices.getValue().isEmpty()) {
        return new ArrayList<ServiceInstance>();
    } else {
        List<ServiceInstance> serviceInstances = new ArrayList<ServiceInstance>();
        for (HealthService consulService : consulServices.getValue()) {
            ServiceInstance serviceInstance = new ServiceInstance();
            serviceInstance.setIp(consulService.getService().getAddress());
            serviceInstance.setPort(consulService.getService().getPort());
            serviceInstance.setServiceName(consulService.getService().getService());
            serviceInstances.add(serviceInstance);
        }
        return serviceInstances;
    }
}
Also used : HealthService(com.ecwid.consul.v1.health.model.HealthService) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance)

Example 13 with ServiceInstance

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

the class ConsulNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    try {
        Response<List<HealthService>> consulServices = lookupHealthService(subscribeInfo.getServiceId(), -1);
        List<ServiceInstance> instances = convert(consulServices);
        log.info("lookup {} instances from consul", instances.size());
        return instances;
    } catch (Exception ex) {
        log.warn("lookup endpoint list failed from {}, msg={}", url, ex.getMessage());
        if (!subscribeInfo.isIgnoreFailOfNamingService()) {
            throw new RpcException("lookup endpoint list failed from consul failed", ex);
        } else {
            return new ArrayList<ServiceInstance>();
        }
    }
}
Also used : RpcException(com.baidu.brpc.exceptions.RpcException) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) ArrayList(java.util.ArrayList) List(java.util.List) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 14 with ServiceInstance

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

the class ConsulNamingServiceTest method testRegisterAndSubscribe.

@Test
public void testRegisterAndSubscribe() throws InterruptedException {
    RegisterInfo registerInfo = createRegisterInfo("127.0.0.1", 8015);
    RegisterInfo anotherRegisterInfo = createRegisterInfo("127.0.0.1", 8016);
    consulNamingService.register(registerInfo);
    consulNamingService.register(anotherRegisterInfo);
    Thread.sleep(3 * 1000);
    SubscribeInfo subscribeInfo = createSubscribeInfo(false);
    final List<ServiceInstance> adds = consulNamingService.lookup(subscribeInfo);
    Assert.assertTrue(adds.size() == 2);
    adds.clear();
    final List<ServiceInstance> deletes = new ArrayList<ServiceInstance>();
    consulNamingService.subscribe(subscribeInfo, new NotifyListener() {

        @Override
        public void notify(Collection<ServiceInstance> addList, Collection<ServiceInstance> deleteList) {
            adds.addAll(addList);
            deletes.addAll(deleteList);
            log.info("notify: {}, {}", addList, deleteList);
        }
    });
    consulNamingService.unregister(registerInfo);
    Thread.sleep(3 * 1000);
    Assert.assertTrue(adds.size() == 0);
    Assert.assertTrue(deletes.size() == 1);
    Assert.assertTrue(deletes.get(0).equals(new ServiceInstance("127.0.0.1", 8015)));
    adds.clear();
    deletes.clear();
    consulNamingService.register(registerInfo);
    Thread.sleep(3 * 1000);
    Assert.assertTrue(adds.size() == 1);
    Assert.assertTrue(deletes.size() == 0);
    Assert.assertTrue(adds.get(0).equals(new ServiceInstance("127.0.0.1", 8015)));
}
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)

Example 15 with ServiceInstance

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

the class ConsulNamingServiceTest method testLookup.

@Test
public void testLookup() throws InterruptedException {
    SubscribeInfo subscribeInfo = createSubscribeInfo(true);
    List<ServiceInstance> instances = consulNamingService.lookup(subscribeInfo);
    Assert.assertTrue(instances.size() == 0);
    RegisterInfo registerInfo = createRegisterInfo("127.0.0.1", 8012);
    consulNamingService.register(registerInfo);
    Thread.sleep(3 * 1000);
    instances = consulNamingService.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);
    consulNamingService.unregister(registerInfo);
}
Also used : SubscribeInfo(com.baidu.brpc.protocol.SubscribeInfo) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RegisterInfo(com.baidu.brpc.naming.RegisterInfo)

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