Search in sources :

Example 1 with RpcURL

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;
}
Also used : ConsulResponse(com.orbitz.consul.model.ConsulResponse) ServiceHealth(com.orbitz.consul.model.health.ServiceHealth) ImmutableServiceHealth(com.orbitz.consul.model.health.ImmutableServiceHealth) HealthClient(com.orbitz.consul.HealthClient) Consul(com.orbitz.consul.Consul) ImmutableServiceHealth(com.orbitz.consul.model.health.ImmutableServiceHealth) RpcURL(com.jim.framework.rpc.common.RpcURL) ServiceHealthCache(com.orbitz.consul.cache.ServiceHealthCache) ServiceHealthKey(com.orbitz.consul.cache.ServiceHealthKey) ConsulCache(com.orbitz.consul.cache.ConsulCache) List(java.util.List)

Example 2 with RpcURL

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();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ConsulRegistryService(com.jim.framework.rpc.registry.ConsulRegistryService) RpcException(com.jim.framework.rpc.exception.RpcException) RegistryService(com.jim.framework.rpc.registry.RegistryService) ConsulRegistryService(com.jim.framework.rpc.registry.ConsulRegistryService) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) RpcURL(com.jim.framework.rpc.common.RpcURL)

Aggregations

RpcURL (com.jim.framework.rpc.common.RpcURL)2 RpcException (com.jim.framework.rpc.exception.RpcException)1 ConsulRegistryService (com.jim.framework.rpc.registry.ConsulRegistryService)1 RegistryService (com.jim.framework.rpc.registry.RegistryService)1 Consul (com.orbitz.consul.Consul)1 HealthClient (com.orbitz.consul.HealthClient)1 ConsulCache (com.orbitz.consul.cache.ConsulCache)1 ServiceHealthCache (com.orbitz.consul.cache.ServiceHealthCache)1 ServiceHealthKey (com.orbitz.consul.cache.ServiceHealthKey)1 ConsulResponse (com.orbitz.consul.model.ConsulResponse)1 ImmutableServiceHealth (com.orbitz.consul.model.health.ImmutableServiceHealth)1 ServiceHealth (com.orbitz.consul.model.health.ServiceHealth)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 List (java.util.List)1