Search in sources :

Example 6 with ServiceInstance

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

the class RandomStrategyTest method before.

@Before
public void before() {
    List<Interceptor> interceptors = new ArrayList<Interceptor>();
    interceptors.add(new LoadBalanceTest.TestInterceptor(1));
    rpcServer1 = new RpcServer(8000, RpcOptionsUtils.getRpcServerOptions(), interceptors);
    rpcServer1.registerService(new LoadBalanceTest.TestEchoService(100));
    rpcServer1.start();
    interceptors = new ArrayList<Interceptor>();
    interceptors.add(new LoadBalanceTest.TestInterceptor(2));
    rpcServer2 = new RpcServer(8001, RpcOptionsUtils.getRpcServerOptions(), interceptors);
    rpcServer2.registerService(new LoadBalanceTest.TestEchoService(200));
    rpcServer2.start();
    interceptors = new ArrayList<Interceptor>();
    interceptors.add(new LoadBalanceTest.TestInterceptor(3));
    rpcServer3 = new RpcServer(8002, RpcOptionsUtils.getRpcServerOptions(), interceptors);
    rpcServer3.registerService(new LoadBalanceTest.TestEchoService(300));
    rpcServer3.start();
    ServiceInstance serviceInstance1 = new ServiceInstance("127.0.0.1", 8000);
    serviceInstance1.setServiceName("EchoService");
    instance1 = new CommunicationClient(serviceInstance1, RpcOptionsUtils.getCommunicationOptions(), null);
    ServiceInstance serviceInstance2 = new ServiceInstance("127.0.0.1", 8001);
    serviceInstance2.setServiceName("EchoService");
    instance2 = new CommunicationClient(serviceInstance2, RpcOptionsUtils.getCommunicationOptions(), null);
    ServiceInstance serviceInstance3 = new ServiceInstance("127.0.0.1", 8002);
    serviceInstance3.setServiceName("EchoService");
    instance3 = new CommunicationClient(serviceInstance3, RpcOptionsUtils.getCommunicationOptions(), null);
}
Also used : CommunicationClient(com.baidu.brpc.client.CommunicationClient) RpcServer(com.baidu.brpc.server.RpcServer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) Interceptor(com.baidu.brpc.interceptor.Interceptor)

Example 7 with ServiceInstance

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

the class HttpJsonProtocolTest method testEncodeHttpRequest.

@Test
public void testEncodeHttpRequest() throws Exception {
    Request request = new HttpRequest();
    request.setTargetMethod(HelloWorldService.class.getMethods()[0]);
    request.setArgs(new Object[] { "hello" });
    request.setLogId(1L);
    Mockito.when(communicationClient.getServiceInstance()).thenReturn(new ServiceInstance("127.0.0.1", 8002));
    request.setCommunicationClient(communicationClient);
    ByteBuf buf = protocol.encodeRequest(request);
    Assert.assertTrue(buf.readableBytes() > 0);
    System.out.println(buf.readableBytes());
    System.out.println(ByteBufUtils.byteBufToString(buf));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(com.baidu.brpc.protocol.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Request(com.baidu.brpc.protocol.Request) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(com.baidu.brpc.protocol.HttpRequest) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) ByteBuf(io.netty.buffer.ByteBuf) BaseMockitoTest(com.baidu.brpc.test.BaseMockitoTest) Test(org.junit.Test)

Example 8 with ServiceInstance

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

the class DnsNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    InetAddress[] addresses;
    try {
        addresses = InetAddress.getAllByName(host);
    } catch (UnknownHostException ex) {
        throw new IllegalArgumentException("unknown http host");
    }
    List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
    for (InetAddress address : addresses) {
        ServiceInstance instance = new ServiceInstance(address.getHostAddress(), port);
        if (subscribeInfo != null && StringUtils.isNoneBlank(subscribeInfo.getServiceId())) {
            instance.setServiceName(subscribeInfo.getServiceId());
        }
        instances.add(instance);
    }
    return instances;
}
Also used : UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) InetAddress(java.net.InetAddress)

Example 9 with ServiceInstance

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

the class FileNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    List<ServiceInstance> list = new ArrayList<ServiceInstance>();
    int lineNum = 0;
    BufferedReader reader = null;
    try {
        File file = new File(filePath);
        long lastModified = file.lastModified();
        reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            lineNum++;
            line = line.trim();
            String[] ipPort = line.split(":");
            if (ipPort.length != 2) {
                LOG.warn("Invalid address format: " + line);
                continue;
            }
            ServiceInstance instance = new ServiceInstance(ipPort[0].trim(), Integer.valueOf(ipPort[1].trim()));
            if (subscribeInfo != null && StringUtils.isNoneBlank(subscribeInfo.getServiceId())) {
                instance.setServiceName(subscribeInfo.getServiceId());
            }
            list.add(instance);
        }
        LOG.debug("Got " + list.size() + " servers (out of " + lineNum + ')' + " from " + filePath);
        this.lastModified = lastModified;
        return list;
    } catch (IOException ex) {
        LOG.warn("read file error, fileName={}", filePath);
        throw new RuntimeException("read naming file error");
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex2) {
                LOG.warn("close failed");
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 10 with ServiceInstance

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

the class BrpcChannelTest method before.

@Before
public void before() {
    rpcServer = new RpcServer(8000, RpcOptionsUtils.getRpcServerOptions());
    rpcServer.start();
    options = RpcOptionsUtils.getRpcClientOptions();
    options.setLatencyWindowSizeOfFairLoadBalance(2);
    rpcClient = new RpcClient("list://127.0.0.1:8000", options);
    ServiceInstance serviceInstance = new ServiceInstance("127.0.0.1", 8000);
    serviceInstance.setServiceName("EchoService2");
    channelGroup = new BrpcPooledChannel(serviceInstance, RpcOptionsUtils.getCommunicationOptions());
}
Also used : RpcServer(com.baidu.brpc.server.RpcServer) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) BrpcPooledChannel(com.baidu.brpc.client.channel.BrpcPooledChannel) Before(org.junit.Before)

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