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