Micronaut 云配置

2023-03-08 16:52 更新

為云構(gòu)建的應(yīng)用程序通常需要適應(yīng)在云環(huán)境中運行,以分布式方式讀取和共享配置,并在必要時將配置外部化到環(huán)境中。

Micronaut 的環(huán)境概念默認是云平臺感知的,并盡最大努力檢測底層的活動環(huán)境。

然后,您可以使用 Requires 注釋有條件地加載 bean 定義。

下表總結(jié)了 Environment 接口中的常量并提供了一個示例:

表 1. Micronaut 環(huán)境檢測
常量 描述 需要示例 環(huán)境名稱

ANDROID

該應(yīng)用程序作為 Android 應(yīng)用程序運行

@Requires(env = Environment.ANDROID)

android

TEST

應(yīng)用程序在 JUnit 或 Spock 測試中運行

@Requires(env = Environment.TEST)

test

CLOUD

該應(yīng)用程序在云環(huán)境中運行(適用于所有其他云平臺類型)

@Requires(env = Environment.CLOUD)

cloud

AMAZON_EC2

在亞馬遜 EC2 上運行

@Requires(env = Environment.AMAZON_EC2)

ec2

GOOGLE_COMPUTE

在谷歌 Compute 上運行

@Requires(env = Environment.GOOGLE_COMPUTE)

gcp

KUBERNETES

在 Kubernetes 上運行

@Requires(env = Environment.KUBERNETES)

k8s

HEROKU

在 Heroku 上運行

@Requires(env = Environment.HEROKU)

heroku

CLOUD_FOUNDRY

在 Cloud Foundry 上運行

@Requires(env = Environment.CLOUD_FOUNDRY)

pcf

AZURE

在微軟 Azure 上運行

@Requires(env = Environment.AZURE)

azure

IBM

在 IBM Cloud 上運行

@Requires(env = Environment.IBM)

ibm

DIGITAL_OCEAN

在 Digital Ocean 上運行

@Requires(env = Environment.DIGITAL_OCEAN)

digitalocean

ORACLE_CLOUD

在 Oracle Cloud 上運行

@Requires(env = Environment.ORACLE_CLOUD)

oraclecloud

請注意,您可以激活多個環(huán)境,例如在 AWS 上的 Kubernetes 中運行時。

此外,使用上表中定義的常量值,您可以創(chuàng)建特定于環(huán)境的配置文件。例如,如果您創(chuàng)建一個 src/main/resources/application-gcp.yml 文件,它只會在 Google Compute 上運行時加載。

Environment 中的任何配置屬性也可以通過環(huán)境變量進行設(shè)置。例如,設(shè)置 CONSUL_CLIENT_HOST 環(huán)境變量會覆蓋 ConsulConfiguration 中的主機屬性。

使用云實例元數(shù)據(jù)

當 Micronaut 檢測到它正在受支持的云平臺上運行時,它會在啟動時填充接口 ComputeInstanceMetadata。

從 Micronaut 2.1.x 開始,此邏輯取決于是否存在適用于 Oracle Cloud、AWS 或 GCP 的適當核心云模塊。

所有這些數(shù)據(jù)都合并到正在運行的 ServiceInstance 的元數(shù)據(jù)屬性中。

要訪問應(yīng)用程序?qū)嵗脑獢?shù)據(jù),您可以使用 EmbeddedServerInstance 接口,并調(diào)用返回元數(shù)據(jù)映射的 getMetadata()。

如果您通過客戶端遠程連接,一旦您從 LoadBalancer 或 DiscoveryClient API 檢索到 ServiceInstance,就可以引用實例元數(shù)據(jù)。

Netflix Ribbon 客戶端負載均衡器可以配置為使用元數(shù)據(jù)進行區(qū)域感知客戶端負載均衡。

要通過服務(wù)發(fā)現(xiàn)獲取服務(wù)的元數(shù)據(jù),請使用 LoadBalancerResolver 接口來解析 LoadBalancer 并通過標識符獲取對服務(wù)的引用:

獲取服務(wù)實例的元數(shù)據(jù)

LoadBalancer loadBalancer = loadBalancerResolver.resolve("some-service");
Flux.from(
    loadBalancer.select()
).subscribe((instance) ->
    ConvertibleValues<String> metaData = instance.getMetadata();
    ...
);

EmbeddedServerInstance 可通過偵聽 ServiceReadyEvent 的事件偵聽器獲得。 @EventListener 注釋使監(jiān)聽 bean 中的事件變得容易。

要獲取本地運行的服務(wù)器的元數(shù)據(jù),請使用 ServiceReadyEvent 的 EventListener:

獲取本地服務(wù)器的元數(shù)據(jù)

@EventListener
void onServiceStarted(ServiceReadyEvent event) {
    ServiceInstance serviceInstance = event.getSource();
    ConvertibleValues<String> metadata = serviceInstance.getMetadata();
}

分布式配置

如您所見,Micronaut 具有一個健壯的系統(tǒng),用于外部化和調(diào)整配置以適應(yīng)受 Grails 和 Spring Boot 中類似方法啟發(fā)的環(huán)境。

但是,如果你想讓多個微服務(wù)共享配置怎么辦? Micronaut 包括用于分布式配置的 API。

ConfigurationClient 接口有一個 getPropertySources 方法,可以實現(xiàn)該方法以從分布式源讀取和解析配置。

getPropertySources 返回發(fā)出零個或多個 PropertySource 實例的 Publisher。

默認實現(xiàn)是 DefaultCompositeConfigurationClient,它將所有已注冊的 ConfigurationClient bean 合并到一個 bean 中。

您可以實現(xiàn)自己的 ConfigurationClient 或使用 Micronaut 提供的實現(xiàn)。以下部分涵蓋了這些內(nèi)容。

HashiCorp 領(lǐng)事支持

Consul 是由 HashiCorp 提供的流行的服務(wù)發(fā)現(xiàn)和分布式配置服務(wù)器。 Micronaut 有一個原生的 ConsulClient,它使用 Micronaut 對聲明式 HTTP 客戶端的支持。

啟動領(lǐng)事

開始使用 Consul 的最快方法是通過 Docker:

  1. 使用 Docker 啟動 Consul

docker run -p 8500:8500 consul

或者,您可以安裝并運行本地 Consul 實例。

使用 Consul 啟用分布式配置

使用 CLI

如果您使用 Micronaut CLI 創(chuàng)建項目,請?zhí)峁?nbsp;config-consul 功能以在您的項目中啟用 Consul 的分布式配置:

$ mn create-app my-app --features config-consul

要啟用分布式配置,請確保啟用 [bootstrap] 并使用以下配置創(chuàng)建 src/main/resources/bootstrap.[yml/toml/properties] 文件:

 Properties Yaml  Toml  Groovy  Hocon  JSON 
micronaut.application.name=hello-world
micronaut.config-client.enabled=true
consul.client.defaultZone=${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}
micronaut:
  application:
    name: hello-world
  config-client:
    enabled: true
consul:
  client:
    defaultZone: "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"
[micronaut]
  [micronaut.application]
    name="hello-world"
  [micronaut.config-client]
    enabled=true
[consul]
  [consul.client]
    defaultZone="${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"
micronaut {
  application {
    name = "hello-world"
  }
  configClient {
    enabled = true
  }
}
consul {
  client {
    defaultZone = "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"
  }
}
{
  micronaut {
    application {
      name = "hello-world"
    }
    config-client {
      enabled = true
    }
  }
  consul {
    client {
      defaultZone = "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"
    }
  }
}
{
  "micronaut": {
    "application": {
      "name": "hello-world"
    },
    "config-client": {
      "enabled": true
    }
  },
  "consul": {
    "client": {
      "defaultZone": "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"
    }
  }
}

啟用分布式配置后,將要共享的配置存儲在 Consul 的鍵/值存儲中。有很多方法可以做到這一點。

將配置存儲為鍵/值對

一種方法是將鍵和值直接存儲在 Consul 中。在這種情況下,Micronaut 默認在 Consul /config 目錄中查找配置。

您可以通過設(shè)置 consul.client.config.path 來更改搜索的路徑

在 /config 目錄中,Micronaut 按優(yōu)先順序在以下目錄中搜索值:

表 1. 配置解析優(yōu)先級
目錄 描述

/config/application

所有應(yīng)用程序共享的配置

/config/application,prod

prod 環(huán)境的所有應(yīng)用程序共享的配置

/config/[APPLICATION_NAME]

特定于應(yīng)用程序的配置,示例 /config/hello-world

/config/[APPLICATION_NAME],prod

活動環(huán)境的特定于應(yīng)用程序的配置

APPLICATION_NAME 的值是您在引導(dǎo)程序配置文件中配置的任何 micronaut.application.name。

要查看實際效果,請使用以下 cURL 命令在目錄 /config/application 中存儲名為 foo.bar 且值為 myvalue 的屬性。

使用 cURL 寫入值

curl -X PUT -d @- localhost:8500/v1/kv/config/application/foo.bar <<< myvalue

如果您現(xiàn)在定義一個 @Value("${foo.bar}") 或調(diào)用 environment.getProperty(..) 值 myvalue 將從 Consul 解析。

在 YAML、JSON 等中存儲配置

一些 Consul 用戶更喜歡將配置存儲在特定格式的 blob 中,例如 YAML。 Micronaut 支持這種模式,并支持以 YAML、JSON 或 Java 屬性格式存儲配置。

ConfigDiscoveryConfiguration 有許多配置選項,用于配置發(fā)現(xiàn)分布式配置的方式。

您可以設(shè)置 consul.client.config.format 選項來配置讀取屬性的格式。

例如,要配置 JSON:

 Properties Yaml  Toml  Groovy  Hocon  JSON 
consul.client.config.format=JSON
consul:
  client:
    config:
      format: JSON
[consul]
  [consul.client]
    [consul.client.config]
      format="JSON"
consul {
  client {
    config {
      format = "JSON"
    }
  }
}
{
  consul {
    client {
      config {
        format = "JSON"
      }
    }
  }
}
{
  "consul": {
    "client": {
      "config": {
        "format": "JSON"
      }
    }
  }
}

現(xiàn)在將您的配置以 JSON 格式寫入 Consul:

使用 cURL 編寫 JSON

curl -X PUT  localhost:8500/v1/kv/config/application \
-d @- << EOF
{ "foo": {  "bar": "myvalue" } }
EOF

將配置存儲為文件引用

另一個流行的選項是 git2consul,它將 Git 存儲庫的內(nèi)容鏡像到 Consul 的鍵/值存儲。

您可以設(shè)置一個包含 application.yml、hello-world-test.json 等文件的 Git 存儲庫,這些文件的內(nèi)容將被克隆到 Consul。

在這種情況下,Consul 中的每個鍵都代表一個帶有擴展名的文件,例如 /config/application.yml,您必須配置 FILE 格式:

 Properties Yaml  Toml  Groovy  Hocon  JSON 
consul.client.config.format=FILE
consul:
  client:
    config:
      format: FILE
[consul]
  [consul.client]
    [consul.client.config]
      format="FILE"
consul {
  client {
    config {
      format = "FILE"
    }
  }
}
{
  consul {
    client {
      config {
        format = "FILE"
      }
    }
  }
}
{
  "consul": {
    "client": {
      "config": {
        "format": "FILE"
      }
    }
  }
}

HashiCorp 金庫支持

Micronaut 與 HashiCorp Vault 集成,作為分布式配置源。

要啟用分布式配置,請確保啟用 [bootstrap] 并創(chuàng)建一個 src/main/resources/bootstrap.[yml/toml/properties] 文件,其中包含以下配置:

與 HashiCorp Vault 集成

 Properties Yaml  Toml  Groovy  Hocon  JSON 
micronaut.application.name=hello-world
micronaut.config-client.enabled=true
vault.client.config.enabled=true
micronaut:
  application:
    name: hello-world
  config-client:
    enabled: true

vault:
  client:
    config:
      enabled: true
[micronaut]
  [micronaut.application]
    name="hello-world"
  [micronaut.config-client]
    enabled=true
[vault]
  [vault.client]
    [vault.client.config]
      enabled=true
micronaut {
  application {
    name = "hello-world"
  }
  configClient {
    enabled = true
  }
}
vault {
  client {
    config {
      enabled = true
    }
  }
}
{
  micronaut {
    application {
      name = "hello-world"
    }
    config-client {
      enabled = true
    }
  }
  vault {
    client {
      config {
        enabled = true
      }
    }
  }
}
{
  "micronaut": {
    "application": {
      "name": "hello-world"
    },
    "config-client": {
      "enabled": true
    }
  },
  "vault": {
    "client": {
      "config": {
        "enabled": true
      }
    }
  }
}

查看所有配置選項的配置參考。

Micronaut 使用配置的 micronaut.application.name 從 Vault 中查找應(yīng)用程序的屬性源。

表 1. 配置解析優(yōu)先級
目錄 描述

/application

所有應(yīng)用程序共享的配置

/[APPLICATION_NAME]

特定于應(yīng)用程序的配置

/application/[ENV_NAME]

一個活動環(huán)境名稱的所有應(yīng)用程序共享的配置

/[APPLICATION_NAME]/[ENV_NAME]

活動環(huán)境名稱的特定于應(yīng)用程序的配置

有關(guān)如何設(shè)置服務(wù)器的更多信息,請參閱 HashiCorp Vault 的文檔。

Spring Cloud 配置支持

從 1.1 開始,Micronaut 為那些沒有切換到專用的更完整的解決方案(如 Consul)的人提供了原生的 Spring Cloud 配置。

要啟用分布式配置,請確保啟用 [bootstrap] 并使用以下配置創(chuàng)建 src/main/resources/bootstrap.[yml/toml/properties] 文件:

與 Spring Cloud 配置集成

 Properties Yaml  Toml  Groovy  Hocon  JSON
micronaut.application.name=hello-world
micronaut.config-client.enabled=true
spring.cloud.config.enabled=true
spring.cloud.config.uri=http://localhost:8888/
spring.cloud.config.retry-attempts=4
spring.cloud.config.retry-delay=2s
micronaut:
  application:
    name: hello-world
  config-client:
    enabled: true
spring:
  cloud:
    config:
      enabled: true
      uri: http://localhost:8888/
      retry-attempts: 4
      retry-delay: 2s
[micronaut]
  [micronaut.application]
    name="hello-world"
  [micronaut.config-client]
    enabled=true
[spring]
  [spring.cloud]
    [spring.cloud.config]
      enabled=true
      uri="http://localhost:8888/"
      retry-attempts=4
      retry-delay="2s"
micronaut {
  application {
    name = "hello-world"
  }
  configClient {
    enabled = true
  }
}
spring {
  cloud {
    config {
      enabled = true
      uri = "http://localhost:8888/"
      retryAttempts = 4
      retryDelay = "2s"
    }
  }
}
{
  micronaut {
    application {
      name = "hello-world"
    }
    config-client {
      enabled = true
    }
  }
  spring {
    cloud {
      config {
        enabled = true
        uri = "http://localhost:8888/"
        retry-attempts = 4
        retry-delay = "2s"
      }
    }
  }
}
{
  "micronaut": {
    "application": {
      "name": "hello-world"
    },
    "config-client": {
      "enabled": true
    }
  },
  "spring": {
    "cloud": {
      "config": {
        "enabled": true,
        "uri": "http://localhost:8888/",
        "retry-attempts": 4,
        "retry-delay": "2s"
      }
    }
  }
}
  • retry-attempts 可選,指定重試次數(shù)

  • retry-delay 是可選的,指定重試之間的延遲

Micronaut 使用配置的 micronaut.application.name 從通過 spring.cloud.config.uri 配置的 Spring Cloud 配置服務(wù)器中查找應(yīng)用程序的屬性源。

有關(guān)如何設(shè)置服務(wù)器的更多信息,請參閱 Spring Cloud Config Server 文檔。

AWS 參數(shù)存儲支持

Micronaut 支持通過 AWS System Manager Parameter Store 共享配置。您需要配置以下依賴項:

 Gradle Maven 
implementation("io.micronaut.aws:micronaut-aws-parameter-store")
<dependency>
    <groupId>io.micronaut.aws</groupId>
    <artifactId>micronaut-aws-parameter-store</artifactId>
</dependency>

要啟用分布式配置,請確保已啟用引導(dǎo)程序并使用以下配置創(chuàng)建一個 src/main/resources/bootstrap.yml 文件:

 Properties Yaml  Toml  Groovy  Hocon  JSON 
micronaut.application.name=hello-world
micronaut.config-client.enabled=true
aws.client.system-manager.parameterstore.enabled=true
micronaut:
  application:
    name: hello-world
  config-client:
    enabled: true
aws:
  client:
    system-manager:
      parameterstore:
        enabled: true
[micronaut]
  [micronaut.application]
    name="hello-world"
  [micronaut.config-client]
    enabled=true
[aws]
  [aws.client]
    [aws.client.system-manager]
      [aws.client.system-manager.parameterstore]
        enabled=true
micronaut {
  application {
    name = "hello-world"
  }
  configClient {
    enabled = true
  }
}
aws {
  client {
    systemManager {
      parameterstore {
        enabled = true
      }
    }
  }
}
{
  micronaut {
    application {
      name = "hello-world"
    }
    config-client {
      enabled = true
    }
  }
  aws {
    client {
      system-manager {
        parameterstore {
          enabled = true
        }
      }
    }
  }
}
{
  "micronaut": {
    "application": {
      "name": "hello-world"
    },
    "config-client": {
      "enabled": true
    }
  },
  "aws": {
    "client": {
      "system-manager": {
        "parameterstore": {
          "enabled": true
        }
      }
    }
  }
}

查看所有配置選項的配置參考。

您可以從 AWS 控制臺 → 系統(tǒng)管理器 → Parameter Store 配置共享屬性。

Micronaut 使用層次結(jié)構(gòu)來讀取配置值,并支持 String、StringList 和 SecureString 類型。

您也可以通過在下劃線 _ 后包含環(huán)境名稱來創(chuàng)建特定于環(huán)境的配置。例如,如果 micronaut.application.name 設(shè)置為 helloworld,則在 helloworld_test 下指定的配置值將僅應(yīng)用于測試環(huán)境。

表 1. 配置解析優(yōu)先級
目錄 描述

/config/application

所有應(yīng)用程序共享的配置

/config/[APPLICATION_NAME]

特定于應(yīng)用程序的配置,示例 /config/hello-world

/config/application_prod

prod 環(huán)境的所有應(yīng)用程序共享的配置

/config/[APPLICATION_NAME]_prod

活動環(huán)境的特定于應(yīng)用程序的配置

例如,如果在 AWS Parameter Store 中配置了配置名稱 /config/application_test/server.url,則連接到該參數(shù)存儲的任何應(yīng)用程序都可以使用 server.url 檢索該值。如果應(yīng)用程序?qū)?nbsp;micronaut.application.name 配置為 myapp,則名稱為 /config/myapp_test/server.url 的值會覆蓋該應(yīng)用程序的值。

樹的每一層都可以由鍵=值對組成。對于多個鍵/值對,將類型設(shè)置為 StringList。

對于特殊的安全信息,例如密鑰或密碼,請使用 SecureString 類型。當您添加和檢索值時,KMS 將被自動調(diào)用,并將使用您帳戶的默認密鑰庫對其進行解密。如果您將配置設(shè)置為不使用安全字符串,它們將以加密方式返回給您,您必須手動解密它們。

Oracle Cloud Vault 支持

請參閱使用 Oracle Cloud Vault 文檔的安全分布式配置。

谷歌云發(fā)布/訂閱支持

請參閱 Micronaut GCP Pub/Sub 文檔。

Kubernetes 支持

請參閱 Kubernetes 配置客戶端文檔。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號