safety-data / akka-persistence-redis

Akka persistence plugin for Redis
Apache License 2.0
27 stars 13 forks source link

Plugin always connects to localhost #19

Closed l15k4 closed 7 years ago

l15k4 commented 7 years ago

This is very strange :

  private val config = ConfigFactory.parseString(
    """
        akka {
          persistence.journal.plugin = "akka-persistence-redis.journal"
        }
        akka-persistence-redis {
          redis {
            host = redis
          }
        }
        """.stripMargin
  ).withFallback(ConfigFactory.load())

  val system = ActorSystem("example", config)
 Connect to localhost/127.0.0.1:6379
[akka://example/user/RedisClient-$a] CommandFailed(Connect(localhost/127.0.0.1:6379,None,List(KeepAlive(true)),None,false)) because of Connection refused

I tried to expose even redis.host = redis to the global scope, but I just cannot make it to connect to hostname redis. Any idea?

satabin commented 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

l15k4 commented 7 years ago

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
satabin commented 7 years ago

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.

l15k4 commented 7 years ago

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 :-/

satabin commented 7 years ago

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")

l15k4 commented 7 years ago

I see, :man_facepalming: I grasp the problem now, thank you for your help and patience @satabin