Search in sources :

Example 1 with HostPort

use of rpc.turbo.config.HostPort in project turbo-rpc by hank-whu.

the class AppConfig method parse.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static AppConfig parse(Config config) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
    String group = getStringOrElse(config, "group", TurboService.DEFAULT_GROUP);
    String app = getStringOrElse(config, "app", TurboService.DEFAULT_GROUP);
    int globalTimeout = getIntOrElse(config, "globalTimeout", 0);
    int maxRequestWait = getIntOrElse(config, "maxRequestWait", 10000);
    int connectPerServer = getIntOrElse(config, "connectPerServer", 1);
    int serverErrorThreshold = getIntOrElse(config, "serverErrorThreshold", 16);
    int connectErrorThreshold = getIntOrElse(config, "connectErrorThreshold", 2 * serverErrorThreshold / connectPerServer);
    String serializerClass = getStringOrElse(config, "serializer.class", ProtostuffSerializer.class.getName());
    Serializer serializer = (Serializer) // 
    Class.forName(// 
    serializerClass).getDeclaredConstructor().newInstance();
    String loadBalanceFactoryClass = getStringOrElse(config, "loadBalanceFactory.class", RoundRobinLoadBalanceFactory.class.getName());
    LoadBalanceFactory loadBalanceFactory = (LoadBalanceFactory) // 
    Class.forName(// 
    loadBalanceFactoryClass).getDeclaredConstructor().newInstance();
    String discoverClass = getStringOrElse(config, "discover.class", DirectConnectDiscover.class.getName());
    List<String> discoverAddressList = config.getStringList("discover.address");
    Discover discover = (Discover) // 
    Class.forName(// 
    discoverClass).getDeclaredConstructor().newInstance();
    List<HostPort> hostPorts = // 
    discoverAddressList.stream().map(// 
    str -> new HostPort(str)).collect(Collectors.toList());
    discover.init(hostPorts);
    AppConfig appConfig = new AppConfig();
    appConfig.setGroup(group);
    appConfig.setApp(app);
    appConfig.setSerializer(serializer);
    appConfig.setGlobalTimeout(globalTimeout);
    appConfig.setMaxRequestWait(maxRequestWait);
    appConfig.setConnectPerServer(connectPerServer);
    appConfig.setServerErrorThreshold(serverErrorThreshold);
    appConfig.setConnectErrorThreshold(connectErrorThreshold);
    appConfig.setLoadBalanceFactory(loadBalanceFactory);
    appConfig.setDiscover(discover);
    return appConfig;
}
Also used : Discover(rpc.turbo.discover.Discover) Serializer(rpc.turbo.serialization.Serializer) Config(com.typesafe.config.Config) TurboService(rpc.turbo.annotation.TurboService) ConfigUtils.getStringOrElse(rpc.turbo.config.ConfigUtils.getStringOrElse) DirectConnectDiscover(rpc.turbo.discover.DirectConnectDiscover) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer) ConfigException(rpc.turbo.config.ConfigException) Collectors(java.util.stream.Collectors) LoadBalanceFactory(rpc.turbo.loadbalance.LoadBalanceFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) HostPort(rpc.turbo.config.HostPort) Weightable(rpc.turbo.loadbalance.Weightable) ConfigUtils.getIntOrElse(rpc.turbo.config.ConfigUtils.getIntOrElse) RoundRobinLoadBalanceFactory(rpc.turbo.loadbalance.RoundRobinLoadBalanceFactory) HostPort(rpc.turbo.config.HostPort) LoadBalanceFactory(rpc.turbo.loadbalance.LoadBalanceFactory) RoundRobinLoadBalanceFactory(rpc.turbo.loadbalance.RoundRobinLoadBalanceFactory) Discover(rpc.turbo.discover.Discover) DirectConnectDiscover(rpc.turbo.discover.DirectConnectDiscover) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer) DirectConnectDiscover(rpc.turbo.discover.DirectConnectDiscover) RoundRobinLoadBalanceFactory(rpc.turbo.loadbalance.RoundRobinLoadBalanceFactory) Serializer(rpc.turbo.serialization.Serializer) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer)

Example 2 with HostPort

use of rpc.turbo.config.HostPort in project turbo-rpc by hank-whu.

the class ZooKeeperRegister method init.

@Override
public void init(List<HostPort> hostPorts) {
    watcherMap = new ConcurrentHashMap<>();
    String connectString = hostPorts.stream().map(i -> i.toString()).collect(Collectors.joining(","));
    RetryPolicy retryPolicy = new ForeverRetryPolicy(1000, 60 * 1000);
    client = CuratorFrameworkFactory.newClient(connectString, 1000 * 10, 1000 * 3, retryPolicy);
    client.start();
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) Register(rpc.turbo.registry.Register) IOException(java.io.IOException) Protocol(rpc.turbo.config.server.Protocol) Collectors(java.util.stream.Collectors) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) AddressWithWeight(rpc.turbo.config.AddressWithWeight) StandardCharsets(java.nio.charset.StandardCharsets) StartMode(org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode) ForeverRetryPolicy(rpc.turbo.zk.common.ForeverRetryPolicy) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) HostPort(rpc.turbo.config.HostPort) RetryPolicy(org.apache.curator.RetryPolicy) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) ConcurrentHashMap(org.jboss.netty.util.internal.ConcurrentHashMap) HexUtils(rpc.turbo.util.HexUtils) ForeverRetryPolicy(rpc.turbo.zk.common.ForeverRetryPolicy) ForeverRetryPolicy(rpc.turbo.zk.common.ForeverRetryPolicy) RetryPolicy(org.apache.curator.RetryPolicy)

Example 3 with HostPort

use of rpc.turbo.config.HostPort in project turbo-rpc by hank-whu.

the class RegisterConfig method parse.

static RegisterConfig parse(Config config) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
    String serverAddressStr = config.getString("server.address");
    HostPort serverAddress = new HostPort(serverAddressStr);
    int serverWeight = config.getInt("server.weight");
    String protocolStr = getStringOrElse(config, "server.protocol", Protocol.RPC.name());
    Protocol protocol = Protocol.valueOf(protocolStr);
    String registerClass = getStringOrElse(config, "register.class", DirectConnectRegister.class.getName());
    List<String> registerAddressList = config.getStringList("register.address");
    Register register = (Register) // 
    Class.forName(// 
    registerClass).getDeclaredConstructor().newInstance();
    List<HostPort> hostPorts = // 
    registerAddressList.stream().map(// 
    str -> new HostPort(str)).collect(Collectors.toList());
    register.init(hostPorts);
    RegisterConfig registerConfig = new RegisterConfig();
    registerConfig.setRegister(register);
    registerConfig.setProtocol(protocol);
    registerConfig.setServerAddress(serverAddress);
    registerConfig.setServerWeight(serverWeight);
    return registerConfig;
}
Also used : List(java.util.List) HostPort(rpc.turbo.config.HostPort) Register(rpc.turbo.registry.Register) Config(com.typesafe.config.Config) DirectConnectRegister(rpc.turbo.registry.DirectConnectRegister) ConfigUtils.getStringOrElse(rpc.turbo.config.ConfigUtils.getStringOrElse) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Register(rpc.turbo.registry.Register) DirectConnectRegister(rpc.turbo.registry.DirectConnectRegister) HostPort(rpc.turbo.config.HostPort) DirectConnectRegister(rpc.turbo.registry.DirectConnectRegister)

Example 4 with HostPort

use of rpc.turbo.config.HostPort in project turbo-rpc by hank-whu.

the class ServerConfig method parse.

private static ServerConfig parse(Config config) {
    String group = getStringOrElse(config, "group", TurboService.DEFAULT_GROUP);
    String app = getStringOrElse(config, "app", TurboService.DEFAULT_GROUP);
    String ownerName = getStringOrElse(config, "owner.name", "");
    String ownerPhone = getStringOrElse(config, "owner.phone", "");
    String serializerClass = getStringOrElse(config, "serializer.class", ProtostuffSerializer.class.getName());
    Serializer serializer;
    try {
        serializer = (Serializer) // 
        Class.forName(// 
        serializerClass).getDeclaredConstructor().newInstance();
    } catch (Throwable t) {
        throw new ConfigException(t);
    }
    String jsonMapperClass = getStringOrElse(config, "jsonMapper.class", JacksonMapper.class.getName());
    JsonMapper jsonMapper;
    try {
        jsonMapper = (JsonMapper) // 
        Class.forName(// 
        jsonMapperClass).getDeclaredConstructor().newInstance();
    } catch (Throwable t) {
        throw new ConfigException(t);
    }
    List<? extends Config> registerConfigList = config.getConfigList("registers");
    List<RegisterConfig> registers = // 
    registerConfigList.stream().map(registerConfig -> {
        try {
            return RegisterConfig.parse(registerConfig);
        } catch (Throwable e) {
            throw new ConfigException(e);
        }
    }).collect(Collectors.toList());
    Map<Integer, RegisterConfig> portMap = new HashMap<>();
    for (RegisterConfig registerConfig : registers) {
        HostPort serverAddress = registerConfig.getServerAddress();
        RegisterConfig existed = portMap.get(serverAddress.port);
        if (existed == null) {
            portMap.put(serverAddress.port, registerConfig);
            continue;
        }
        if (existed.getProtocol() == registerConfig.getProtocol() && existed.getServerAddress().equals(serverAddress)) {
            continue;
        }
        throw new ConfigException("存在端口冲突: " + registerConfig + " vs " + existed);
    }
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setGroup(group);
    serverConfig.setApp(app);
    serverConfig.setOwnerName(ownerName);
    serverConfig.setOwnerPhone(ownerPhone);
    serverConfig.setSerializer(serializer);
    serverConfig.setJsonMapper(jsonMapper);
    serverConfig.setRegisters(registers);
    return serverConfig;
}
Also used : Serializer(rpc.turbo.serialization.Serializer) Config(com.typesafe.config.Config) TurboService(rpc.turbo.annotation.TurboService) ConfigUtils.getStringOrElse(rpc.turbo.config.ConfigUtils.getStringOrElse) HashMap(java.util.HashMap) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer) ConfigException(rpc.turbo.config.ConfigException) Collectors(java.util.stream.Collectors) JacksonMapper(rpc.turbo.serialization.jackson.JacksonMapper) List(java.util.List) HostPort(rpc.turbo.config.HostPort) JsonMapper(rpc.turbo.serialization.JsonMapper) Map(java.util.Map) ConfigFactory(com.typesafe.config.ConfigFactory) JacksonMapper(rpc.turbo.serialization.jackson.JacksonMapper) HashMap(java.util.HashMap) HostPort(rpc.turbo.config.HostPort) ConfigException(rpc.turbo.config.ConfigException) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer) JsonMapper(rpc.turbo.serialization.JsonMapper) Serializer(rpc.turbo.serialization.Serializer) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer)

Example 5 with HostPort

use of rpc.turbo.config.HostPort in project turbo-rpc by hank-whu.

the class App method rescue.

private void rescue() {
    if (isCloseing) {
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(group + "#" + app + " start rescue zombies");
    }
    if (zombieMap.isEmpty()) {
        if (logger.isDebugEnabled()) {
            logger.debug(group + "#" + app + ", there is no zombie");
        }
        return;
    }
    // 
    zombieMap.entrySet().stream().parallel().forEach(kv -> {
        if (isCloseing) {
            return;
        }
        HostPort serverAddress = kv.getKey();
        ConnectorContext context = kv.getValue();
        try {
            context.connect();
            List<String> list = loadClass(context);
            for (String clazz : list) {
                supportClassMap.put(clazz, Boolean.TRUE);
            }
            if (logger.isInfoEnabled()) {
                logger.info("rescue: " + group + "#" + app + " " + context.serverAddress + " support services: " + list);
            }
            Map<String, Integer> methodStringToServiceIdMap = loadServiceId(context);
            context.clear();
            context.setServiceMethodNameToServiceIdMap(methodStringToServiceIdMap);
            zombieMap.remove(serverAddress);
            activeMap.put(serverAddress, context);
            if (logger.isInfoEnabled()) {
                logger.info(group + "#" + app + " " + context.serverAddress + " zombie is rescued");
            }
        } catch (Exception e) {
            if (logger.isWarnEnabled()) {
                logger.warn("rescue: " + group + "#" + app + " " + context.serverAddress + " zombie is also zombie", e);
            }
        }
    });
}
Also used : HostPort(rpc.turbo.config.HostPort) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException) InvokeException(rpc.turbo.invoke.InvokeException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

HostPort (rpc.turbo.config.HostPort)14 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 IOException (java.io.IOException)5 InetSocketAddress (java.net.InetSocketAddress)4 Config (com.typesafe.config.Config)3 Log (org.apache.commons.logging.Log)3 LogFactory (org.apache.commons.logging.LogFactory)3 TurboService (rpc.turbo.annotation.TurboService)3 ConfigUtils.getStringOrElse (rpc.turbo.config.ConfigUtils.getStringOrElse)3 Protocol (rpc.turbo.config.server.Protocol)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Map (java.util.Map)2 Objects (java.util.Objects)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 RetryPolicy (org.apache.curator.RetryPolicy)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)2 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)2