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