Spring Cloud 客戶端使用

2024-01-03 17:08 更新

要在應(yīng)用程序中使用這些功能,只需將其構(gòu)建為依賴于spring-cloud-vault-config的Spring Boot應(yīng)用程序即可(例如,查看測(cè)試用例)。Maven示例配置:

示例101.1 pom.xml

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

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-vault-config</artifactId>
        <version>{project-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

<!-- repositories also needed for snapshots and milestones -->

然后,您可以創(chuàng)建一個(gè)標(biāo)準(zhǔn)的Spring Boot應(yīng)用程序,例如以下簡(jiǎn)單的HTTP服務(wù)器:

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

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

運(yùn)行時(shí),它將從端口8200上的默認(rèn)本地Vault服務(wù)器獲取外部配置(如果正在運(yùn)行)。要修改啟動(dòng)行為,您可以使用bootstrap.properties來(lái)更改Vault服務(wù)器的位置(例如application.properties,但用于應(yīng)用程序上下文的引導(dǎo)階段),例如

示例101.2 bootstrap.yml

spring.cloud.vault:
    host: localhost
    port: 8200
    scheme: https
    uri: https://localhost:8200
    connection-timeout: 5000
    read-timeout: 15000
    config:
        order: -10

  • host設(shè)置Vault主機(jī)的主機(jī)名。主機(jī)名將用于SSL證書(shū)驗(yàn)證
  • port設(shè)置Vault端口
  • scheme將方案設(shè)置為http將使用純HTTP。支持的方案是httphttps。
  • uri使用URI配置Vault端點(diǎn)。優(yōu)先于主機(jī)/端口/方案配置
  • connection-timeout設(shè)置連接超時(shí)(以毫秒為單位)
  • read-timeout設(shè)置讀取超時(shí)(以毫秒為單位)
  • config.order設(shè)置屬性來(lái)源的順序

啟用進(jìn)一步的集成需要附加的依賴關(guān)系和配置。根據(jù)您設(shè)置Vault的方式,可能需要其他配置,例如 SSL身份驗(yàn)證

如果應(yīng)用程序?qū)肓?code class="literal" i="4551">spring-boot-starter-actuator項(xiàng)目,則可以通過(guò)/health端點(diǎn)獲得保管庫(kù)服務(wù)器的狀態(tài)。

可以通過(guò)屬性management.health.vault.enabled(默認(rèn)值為true)啟用或禁用Vault運(yùn)行狀況指示器。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)