Closed l15k4 closed 7 years ago
ConfigFactory.load()
resolves the default configuration before it is merged with your overridden one. So all substitution are already replaced. What you want is to resolve the configuration after the merge occurred.
If you want to override the reference.conf
by a string in your code, you should merge with the unresolved default configuration. Something like:
private val config = ConfigFactory.parseString(
"""
akka {
persistence.journal.plugin = "akka-persistence-redis.journal"
}
akka-persistence-redis {
redis {
host = redis
}
}
""".stripMargin
).withFallback(ConfigFactory.parseResources("reference.conf")).resolve()
However I would recommend to use the standard mechanism described in the documentation
What? I don't thinks so, it is just a fallback for the primary :
private val config = ConfigFactory.parseString(
"""
akka {
persistence.journal.plugin = "akka-persistence-redis.journal"
}
akka-persistence-redis {
redis {
host = redis
}
}
""".stripMargin
).withFallback(ConfigFactory.load())
println(config.getString("akka-persistence-redis.redis.host"))
println(config.getString("akka-persistence-redis.redis.mode"))
Prints :
redis
simple
Yes, because the merge overrides the specific host key you are querying.
But the journal uses a substitution which default to to akka-persistence-redis.redis
which is resolved by the load
method before the merge occurs.
Try:
println(config.getConfig("akka-persistence-redis.journal.redis"))
Please try my solution and you will see the difference. That’s just the way the config library works.
Hmm, I still can't make it work : redis.conf :
akka {
extensions = ["com.romix.akka.serialization.kryo.KryoSerializationExtension$"]
persistence.journal.plugin = "akka-persistence-redis.journal"
}
akka-persistence-redis {
redis {
host = redis
}
}
val system = ActorSystem("example", ConfigFactory.load("redis").withFallback(ConfigFactory.parseResources("reference.conf")).resolve())
Still connecting to localhost :-/
Still the same problem. load
resolves, but the resolution mechanism does not allow to resolve substitution in reference
that are overridden in application
(or redis
in your case) (see the documentation)
For your example to work, either provide your overloading in a reference.conf
file and simply call Configfactory.load
or try the solution I gave you, where we only parse (hence without resolving) all the configuration, then merge them, and finally resolve the result. Remember that load
method do resolve things.
ConfigFactory.load("redis")
parses, merges, and resolve all the reference.conf
files in your classpath, then parses redis.conf
, merges it with the already merged and resolved references.conf
, and resolves the result. What you want is to merge before any resolution is performed and that’s exactly what my first solution does.
Another way to do it is to have a redis.conf
file that looks like this:
akka {
persistence.journal.plugin = "akka-persistence-redis.journal"
}
akka-persistence-redis {
redis {
host = redis
}
journal.redis = ${akka-persistence-redis.redis}
}
and then just ConfigFactory.load("redis")
I see, :man_facepalming: I grasp the problem now, thank you for your help and patience @satabin
This is very strange :
I tried to expose even
redis.host = redis
to the global scope, but I just cannot make it to connect to hostnameredis
. Any idea?