use of com.jim.framework.rpc.common.RpcURL in project jim-framework by jiangmin168168.
the class ConsulDiscoveryService method getUrls.
@Override
public List<RpcURL> getUrls(String registryHost, int registryPort) {
List<RpcURL> urls = Lists.newArrayList();
Consul consul = this.buildConsul(registryHost, registryPort);
HealthClient client = consul.healthClient();
String name = CONSUL_NAME;
ConsulResponse object = client.getAllServiceInstances(name);
List<ImmutableServiceHealth> serviceHealths = (List<ImmutableServiceHealth>) object.getResponse();
for (ImmutableServiceHealth serviceHealth : serviceHealths) {
RpcURL url = new RpcURL();
url.setHost(serviceHealth.getService().getAddress());
url.setPort(serviceHealth.getService().getPort());
urls.add(url);
}
try {
ServiceHealthCache serviceHealthCache = ServiceHealthCache.newCache(client, name);
serviceHealthCache.addListener(new ConsulCache.Listener<ServiceHealthKey, ServiceHealth>() {
@Override
public void notify(Map<ServiceHealthKey, ServiceHealth> map) {
logger.info("serviceHealthCache.addListener notify");
RpcClientInvokerCache.clear();
}
});
serviceHealthCache.start();
} catch (Exception e) {
logger.info("serviceHealthCache.start error:", e);
}
return urls;
}
use of com.jim.framework.rpc.common.RpcURL in project jim-framework by jiangmin168168.
the class RpcServer method bind.
public void bind(ServiceConfig serviceConfig) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(this.rpcServerInitializer).childOption(ChannelOption.SO_KEEPALIVE, true);
try {
ChannelFuture channelFuture = bootstrap.bind(serviceConfig.getHost(), serviceConfig.getPort()).sync();
RpcURL url = new RpcURL();
url.setHost(serviceConfig.getHost());
url.setPort(serviceConfig.getPort());
url.setRegistryHost(serviceConfig.getRegistryHost());
url.setRegistryPort(serviceConfig.getRegistryPort());
RegistryService registryService = new ConsulRegistryService();
registryService.register(url);
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
throw new RpcException(e);
}
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
Aggregations