今天在搭建完成zmq的服务和调用端之后,为了区分测试、生产环境配置,拉出database.properties中参数时遇到了点问题。这边要随时在一个rpc通信框架和spring内部调用之间切换,而且rpc是独立于spring优先启动的。在这种spring没有完全启动的情况下,通过@value就读不到database.properties中参数,必须手动read InputStream。
期间因为rpc和spring不属于同一个容器,通过classpath也会有读不到的情况,为了在外层用户调用时保证透明,内部读取配置只能恶心一点判断。
Properties p=new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("/database.properties");
if (in == null) {
in = new FileInputStream("conf/database.properties");
}
p.load(in);
in.close();
} catch (IOException e) {
LOGGER.error("ZmqServerTemplate read database.properties error");
e.printStackTrace();
}
HuskarConfig.ServiceHostUrl = p.getProperty("Huskar.ServiceHostUrl");
HuskarConfig.ServiceHostUrlCluster = p.getProperty("Huskar.ServiceHostUrlCluster");
HuskarConfig.TokenKey = p.getProperty("Huskar.TokenKey");
HuskarConfig.TokenValue = p.getProperty("Huskar.TokenValue");