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);
}
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));
}
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;
}
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");
}
}
}
}
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());
}
Aggregations