- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试做一些我认为很简单的事情。我想用 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/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!