Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Plugin always connects to localhost #19

Closed
l15k4 opened this issue Oct 4, 2017 · 6 comments
Closed

Plugin always connects to localhost #19

l15k4 opened this issue Oct 4, 2017 · 6 comments
Assignees
Labels

Comments

@l15k4
Copy link

l15k4 commented Oct 4, 2017

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 satabin self-assigned this Oct 5, 2017
@satabin satabin added the bug label Oct 5, 2017
@satabin
Copy link
Contributor

satabin commented Oct 5, 2017

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

@satabin satabin added question and removed bug labels Oct 5, 2017
@l15k4
Copy link
Author

l15k4 commented Oct 5, 2017

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
Copy link
Contributor

satabin commented Oct 5, 2017

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
Copy link
Author

l15k4 commented Oct 5, 2017

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
Copy link
Contributor

satabin commented Oct 5, 2017

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
Copy link
Author

l15k4 commented Oct 5, 2017

I see, 🤦‍♂️ I grasp the problem now, thank you for your help and patience @satabin

@l15k4 l15k4 closed this as completed Oct 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants