读取配置文件

back_recharge
gaojie 2019-01-09 09:57:46 +08:00
parent 32155b1435
commit dae7fbe1a7
8 changed files with 164 additions and 1 deletions

View File

@ -0,0 +1,3 @@
{
"version":"1"
}

View File

@ -28,6 +28,7 @@ dependencies {
compile("com.google.protobuf:protobuf-java:2.5.0") compile("com.google.protobuf:protobuf-java:2.5.0")
compile("com.fasterxml.jackson.core:jackson-core:2.3.1") compile("com.fasterxml.jackson.core:jackson-core:2.3.1")
compile("com.fasterxml.jackson.core:jackson-databind:2.3.3") compile("com.fasterxml.jackson.core:jackson-databind:2.3.3")
compile files("${System.properties['java.home']}/../lib/tools.jar")
} }
jar { jar {

View File

@ -1,5 +1,8 @@
package com.ljsd; package com.ljsd;
import com.ljsd.jieling.config.json.ConfMessage;
import com.ljsd.jieling.config.json.ServerConfiguration;
import com.ljsd.jieling.config.json.ServerProperties;
import com.ljsd.jieling.handler.BaseHandler; import com.ljsd.jieling.handler.BaseHandler;
import com.ljsd.jieling.logic.STableManager; import com.ljsd.jieling.logic.STableManager;
import com.ljsd.jieling.netty.server.NettyGameServer; import com.ljsd.jieling.netty.server.NettyGameServer;
@ -28,7 +31,7 @@ public class GameApplication {
private static SpringApplication app = null; private static SpringApplication app = null;
private static ConfigurableApplicationContext configurableApplicationContext = null; private static ConfigurableApplicationContext configurableApplicationContext = null;
private static final Logger LOGGER = LoggerFactory.getLogger(GameApplication.class); private static final Logger LOGGER = LoggerFactory.getLogger(GameApplication.class);
public static ServerProperties serverProperties;
private static NettyGameServer nettyServer = null; private static NettyGameServer nettyServer = null;
public static int serverId = 0; public static int serverId = 0;
// public static ServerProperties serverProperties; // public static ServerProperties serverProperties;
@ -42,7 +45,11 @@ public class GameApplication {
app.setWebEnvironment(false); app.setWebEnvironment(false);
configurableApplicationContext = app.run(args); configurableApplicationContext = app.run(args);
ServerConfiguration serverConfiguration = configurableApplicationContext.getBean(ServerConfiguration.class);
serverProperties = serverConfiguration.getServerProperties();
//加载配置开关
ConfMessage.doWork();
//注册消息处理方法 //注册消息处理方法
ProtocolsManager protocolsManager = ProtocolsManager.getInstance(); ProtocolsManager protocolsManager = ProtocolsManager.getInstance();

View File

@ -0,0 +1,44 @@
package com.ljsd.jieling.config.json;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ConfMessage {
private static String currVerion;
private static final Logger LOGGER = LoggerFactory.getLogger(ConfMessage.class);
//加载配置开关
public static void doWork() throws IOException {
File file = new File("conf/plat-configure.json");
// File file = new File("C://ljsd/gameserver_new/conf/plat-configure.json");
if (!file.exists()) {
LOGGER.error("the file plat-configure.json is not exists");
return;
}
InputStream in = new FileInputStream(file);
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
PlatConfigure platConfigure = mapper.readValue(in, PlatConfigure.class);
in.close();
String version = platConfigure.getVersion();
if (version.equalsIgnoreCase(currVerion)) {
return;
}
GlobalPlatConfigure.platConfigure = platConfigure;
currVerion = version;
LOGGER.info("PlatConfigure ->{}", GlobalPlatConfigure.platConfigure.toString());
LOGGER.debug("doWork update PlatConfigure->currVerion={},version={}", currVerion, version);
}
}

View File

@ -0,0 +1,5 @@
package com.ljsd.jieling.config.json;
public class GlobalPlatConfigure {
public static PlatConfigure platConfigure = new PlatConfigure();
}

View File

@ -0,0 +1,22 @@
package com.ljsd.jieling.config.json;
public class PlatConfigure {
public String version = "1";
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "PlatConfigure{" +
"version='" + version + '\'' +
'}';
}
}

View File

@ -0,0 +1,21 @@
package com.ljsd.jieling.config.json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Created by a on 2017/7/5.
*/
@Configuration(value = "serverConfiguration")
@EnableConfigurationProperties(ServerProperties.class)
public class ServerConfiguration {
@Autowired
private ServerProperties serverProperties;
public ServerProperties getServerProperties() {
return serverProperties;
}
}

View File

@ -0,0 +1,60 @@
package com.ljsd.jieling.config.json;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by a on 2017/7/5.
*/
@ConfigurationProperties(prefix = "server")
public class ServerProperties {
private int id;
private String plat;
private String channel;
private int isLAN;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlat() {
return plat;
}
public void setPlat(String plat) {
this.plat = plat;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public int getIsLAN() {
return isLAN;
}
public void setIsLAN(int isLAN) {
this.isLAN = isLAN;
}
@Override
public String toString() {
return "ServerProperties{" +
"id=" + id +
", plat='" + plat + '\'' +
", channel='" + channel + '\'' +
", isLAN=" + isLAN +
'}';
}
}