如何检索库属性以在application.yml中配置MongoDB属性?

时间:2019-07-29 07:58:15

标签: spring-boot spring-data spring-vault

我想从库中检索一些秘密(数据库用户名/密码),并使用spring.data.mongodb配置MongoDB。

我在spring-boot-starter-data-mongodb应用程序中完成了保管库集成。

硬编码的值工作正常,尝试使用${vault.application-name.property_name}

来获取保管库属性。

这是我在application.yml中提供的配置:

spring:
  data:
    mongodb: 
      host: 10.xx.xxx.xxx
      port: 27017
      database: ${vault.application-name.database-name}
      username: ${vault.application-name.database-username}
      password: ${vault.application-name.database-password}
      authentication-database: ${vault.application-name.authentication-database}

考虑以上配置是否正确,我应该从Vault接收这些属性,但是当前,应用程序在执行时抛出以下异常:

2019-07-29 11:28:20,683 [iption='null'}-localhost:27017] org.mongodb.driver.cluster INFO Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.3.jar:?]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.3.jar:?]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) [mongodb-driver-core-3.4.3.jar:?]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_181]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_181]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_181]
    at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_181]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.3.jar:?]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.3.jar:?]
    ... 3 more

1 个答案:

答案 0 :(得分:0)

Spring Cloud Vault集成时,理想情况下唯一要提供的是role名称。 除了使用Spring Boot的Spring Cloud Vault之外,我不建议使用其他任何集成方法,因为您需要在Spring Boot初始化MongoDB配置之前获取属性,这可能会非常棘手。

有关更多参考,请参见docs,这是指向MySQL with Spring Cloud Vault示例的链接。