[dev] no longer use random akka port

This commit is contained in:
tjq 2020-05-18 16:36:32 +08:00
parent f06ae8be8f
commit 4981af0e26
2 changed files with 43 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import com.github.kfcfans.oms.common.RemoteConstant;
import com.github.kfcfans.oms.common.utils.NetUtils;
import com.github.kfcfans.oms.server.akka.actors.FriendActor;
import com.github.kfcfans.oms.server.akka.actors.ServerActor;
import com.github.kfcfans.oms.server.common.utils.PropertyUtils;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Maps;
import com.typesafe.config.Config;
@ -15,6 +16,7 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.Properties;
/**
* 服务端 ActorSystem 启动器
@ -35,10 +37,15 @@ public class OhMyServer {
Stopwatch stopwatch = Stopwatch.createStarted();
log.info("[OhMyServer] OhMyServer's akka system start to bootstrap...");
// 1. 解析配置文件
PropertyUtils.init();
Properties properties = PropertyUtils.getProperties();
int port = Integer.parseInt(properties.getProperty("oms.akka.port", "10086"));
// 1. 启动 ActorSystem
Map<String, Object> overrideConfig = Maps.newHashMap();
String localIP = NetUtils.getLocalHost();
int port = NetUtils.getAvailablePort();
overrideConfig.put("akka.remote.artery.canonical.hostname", localIP);
overrideConfig.put("akka.remote.artery.canonical.port", port);
actorSystemAddress = localIP + ":" + port;

View File

@ -0,0 +1,35 @@
package com.github.kfcfans.oms.server.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;
import java.util.Properties;
/**
* 加载配置文件
*
* @author tjq
* @since 2020/5/18
*/
@Slf4j
public class PropertyUtils {
private static final Properties PROPERTIES = new Properties();
public static Properties getProperties() {
return PROPERTIES;
}
public static void init() {
URL propertiesURL =PropertyUtils.class.getClassLoader().getResource("application.properties");
Objects.requireNonNull(propertiesURL);
try (InputStream is = propertiesURL.openStream()) {
PROPERTIES.load(is);
}catch (Exception e) {
ExceptionUtils.rethrow(e);
}
}
}