且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

试图让Spring/Consul/Vault一起工作

更新时间:2023-11-30 15:52:28

要从Vault获取配置,您需要将Spring Cloud Vault添加到pom.

To get configuration from Vault you need to add Spring Cloud Vault to your pom.

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-vault-config</artifactId>
</dependency>

使用Spring Cloud Dalston.SR4(在pom中使用),您将收到Spring Cloud Vault 1.0.2.因此请注意,此版本的资料少于较新的资料(例如,您不能使用发现功能).如果要使用它,请考虑至少使用Spring Cloud Vault 1.1.1.RELEASE随附的Edgware.SR4.您可以随时在此处查看版本信息: https://github.com/spring-projects/spring-cloud/wiki#release-notes .无论如何,它仍然可以工作,但是您需要对其进行配置. 您的bootstrap.yml应该看起来像这样.

With Spring Cloud Dalston.SR4 (which you used in your pom) you will receive Spring Cloud Vault 1.0.2. So be aware that this version has less stuff than newer one (e.g. you cannot use discovery feature). If you want to use it, consider using at least Edgware.SR4 which comes with Spring Cloud Vault 1.1.1.RELEASE. You can always check version information here: https://github.com/spring-projects/spring-cloud/wiki#release-notes. Anyway, it still will work, but you need to configure it. Your bootstrap.yml should look like this.

spring:
  application:
    name: test-consul
  profiles:
    active: dev
  vault:
    fail-fast: true
    host: localhost
    port: 8200
    scheme: http
    authentication: TOKEN
    config:
      order: -10 # not sure that you need this, didn't investigate
    token: <your_token>

Spring Cloud Vault会使用Vault HTTP API根据您的应用程序名称和配置文件在Vault中搜索配置.因此,您需要根据下一个模式存储信息. (请参阅 Spring Cloud Vault 1.0.2 的文档)

Spring Cloud Vault will search configuration in Vault based on your application name and profile using Vault HTTP API. So you need to store information according to next patterns. (See documentation for Spring Cloud Vault 1.0.2)

/secret/{application}/{profile}
/secret/{application}
/secret/{default-context}/{profile}
/secret/{default-context}

示例:

vault write secret/test-consul/dev username=test_user

在这种情况下,在应用程序启动期间,Spring Cloud Vault将从secret/test-consul/dev获取属性并从中创建属性源.要获得此测试属性的值,您应该仅使用键.示例:

In this case during application start Spring Cloud Vault will get properties from secret/test-consul/dev and create Property Source from them. To get value of this test property you should use only key. Example:

@Value("${username}")
private String user;  // will be resolved to "test_user"

从Consul KV存储中获取属性是相似的. bootstrap.yml文件为:

Getting properties from Consul KV storage is similar. bootstrap.yml file is:

spring:
  profiles:
    active: dev
  application:
    name: test-consul
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true

Spring Cloud Consul将使用下一个模式(所有模式)来使用HTTP API从Consul获取键值.请参阅有关Spring Cloud Consul 1.2.1的文档(它随Dalston.SR4一起提供):

Spring Cloud Consul will use next patterns (all of them) for getting key-values from Consul using HTTP API. See documentation for Spring Cloud Consul 1.2.1 (it comes with Dalston.SR4):

config/testApp,dev/
config/testApp/
config/application,dev/
config/application/

因此,您需要相应地保存数据.我是通过Consul GUI通过在KV Storage中创建文件夹来实现的,使用控制台它看起来应该与此类似:

So you need to save data accordingly. I did it from Consul GUI by creating folders in KV Storage, using console it should look similarly to this:

consul kv put config/test-consul/test testvalue

通过使用HTTP API获取数据,Spring Cloud Consul将从存储在其中的属性创建属性源,您将可以使用

By getting data using HTTP API Spring Cloud Consul will create a property source from properties stored there and you will be able to get this using

@Value("${test}")
private String testString; // will be resolved to "testvalue"

您可以组合两种配置并同时使用Spring Cloud Consul和Vault.在此演示中,我完全没有使用application.yml.

You can combine 2 configurations and use Spring Cloud Consul and Vault simultaneously. I did't use application.yml at all for this demo.

希望这篇长篇文章对您有所帮助:)

Hope this long post will help you :)