gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:59:07 30 4
gpt4 key购买 nike

我正在尝试做一些我认为很简单的事情。我想用 Consul用于配置,Vault对于 secret 。

我正在寻找的是这样一个简单的应用程序,它允许我从 Consul 获取配置和服务,并从 Vault 获取 secret 。

在我的示例中,Consul 和 Vault 已连接,我可以在 Consul 服务和键值存储中看到 Vault。

在 Postman 中,我可以直接查询 Vault 并在 {{vault_url}}/v1/secret/interservice-bus/data-categorizer 查看我的数据

现在我在测试应用中看不到 Consul 或 Vault 值。

我的application.yml很简单

spring:
profiles:
active: dev
main:
banner-mode: "CONSOLE"
application:
name: test-consul

logging:
level:
org: INFO

bootstrap.yml 是我进行大部分配置的地方。其中一些是重复的,因为如果缺少不同的部分,Spring 会提示。我不确定为什么。

spring:
profiles:
active: vault, dev
cloud:
consul:
host: 127.0.0.1
port: 8500
config:
enabled: true
discovery:
prefer-ip-address: true
config:
discovery:
enabled: true
service-id: vault
server:
vault:
host: localhost
port: 8200
scheme: http
authentication: TOKEN
token: 00000000-0000-0000-0000-000000000000
vault:
host: localhost
port: 8200
scheme: http
connection-timeout: 5000
read-timeout: 15000
authentication: TOKEN
config:
order: -10
token: 00000000-0000-0000-0000-000000000000
discovery:
enabled: true
service-id: vault

token 在上面被遮盖了,但确实与保险库的根 token 匹配。

这是我的 POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>edu.dkist</groupId>
<artifactId>test-consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>test-consul</name>
<description>Small test to see if consul can work with vault</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-dependencies</artifactId>
<version>1.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

这是我尝试运行的简单应用

package com.example.demo;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@RefreshScope
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
@Autowired
org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient;

@Value("${test}")
private String testString;


@Value("${interservice-bus.data-categorizer.username}")
private String user;

@Value("${interservice-bus.data-categorizer.password}")
private String password;


@PostConstruct
public void setRabbitMQLocation() {
if (discoveryClient == null) {
System.out.println("******************************ERROR: Discovery Client is null******************************");
return;
}
System.out.println("****************************** Discovery Client is ACTIVE ******************************");

org.springframework.cloud.client.ServiceInstance serviceInstance =
discoveryClient.getInstances("interservice-bus")
.stream()
.findFirst()
.orElseThrow(() -> new RuntimeException("ERROR: " + "interservice-bus" + " not found"));
System.out.println("****************************** ISB is " + serviceInstance.getHost() + ":" + serviceInstance.getPort() + " ******************************");
}

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

它没有找到“测试”。我正在尝试从 Vault 中提取用户名和密码,并从 Consul 中进行测试。

我得到了

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-20 16:30:33.580 ERROR 92722 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.demoApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'test' in value "${test}"

我正在寻找的是这样一个简单的应用程序,它允许我从 Consul 获取配置和服务,并从 Vault 获取 secret 。

通过在网上搜索和查看示例,我不清楚我是否还需要引入 Vault 依赖项,或者我是否从 Consul 获取所有内容(因此不需要 Vault 依赖项)。

最佳答案

要从 Vault 获取配置,您需要将 Spring Cloud Vault 添加到您的 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 应该如下所示。

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 的文档)

/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 获取属性并从中创建 Property Source。要获得此测试属性的值,您应该只使用键。示例:

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

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

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

Spring Cloud Consul 将使用下一个模式(所有模式)使用 HTTP API 从 Consul 获取键值对。参见 documentation for Spring Cloud Consul 1.2.1 (it comes with Dalston.SR4) :

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

因此您需要相应地保存数据。我通过在 KV 存储中创建文件夹从 Consul GUI 做到了这一点,使用控制台它应该看起来类似于:

consul kv put config/test-consul/test testvalue

通过使用 HTTP API 获取数据,Spring Cloud Consul 将从存储在那里的属性创建一个属性源,您将能够使用它来获取它

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

您可以组合 2 个配置并同时使用 Spring Cloud Consul 和 Vault。对于这个演示,我根本没有使用 application.yml

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

关于spring - 试图让 Spring/Consul/Vault 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958207/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com