Spring Cloud kubernetes 未加载配置映射

标签 spring spring-boot kubernetes spring-cloud spring-cloud-kubernetes

我创建了 2 个名为 personservice 和 personservice-dev 的配置映射。

我正在使用配置文件 dev 运行 spring boot 应用程序,但它没有加载正确的配置映射。这是我在崩溃的 Pod 日志中看到的内容。

 2019-11-05 16:29:37.336  INFO [personservice,,,] 7 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='composite-configmap', propertySources=[ConfigMapPropertySource {name='configmap.personservice.default'}]}
2019-11-05 16:29:37.341  INFO [personservice,,,] 7 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: SecretsPropertySource {name='secrets.personservice.default'}
2019-11-05 16:29:37.445  INFO [personservice,,,] 7 --- [           main] c.person.PersonMicroServiceApplication   : The following profiles are active: kubernetes,dev

Kubectl 获取配置映射

enter image description here

部署文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: personservice
  labels:
    app: personservice
spec:
  replicas: 1
  selector:
    matchLabels:
      app: personservice
  template:
    metadata:
      labels:
        app: personservice
    spec:
      containers:
      - name: personservice
        image: microservice-k8s/personmicroservice-k8s:1.0
        ports:
        - containerPort: 8080
        env:
        - name: PROFILE
          value: "dev" 
        - name: SERVER_PORT
          value: "8080"
        - name: ZIPKIN_URI
          value: "http://172.19.27.145:9411"

Bootstrap :

spring:
  application:
    name: personservice

最佳答案

你把事情搞混了。您的 configmap 名为 personservice-dev 并且您的应用程序名称为 personservice 而不是 personservice-dev,默认情况下 Spring Cloud K8S 会查找名称等于的 configmap到 spring.application.name 而不是 spring.application.name-{profile}

您有两种方法可以解决您的问题:

1-删除 personservice-dev 并在您的 personservice 配置映射中:

kind: ConfigMap
apiVersion: v1
metadata:
  name: personservice
data:
  application.yml: |-
    p1:
      pa: blabla
    ---
    spring:
      profiles: dev
    p1:
      pa: blibli
    ---
    spring:
      profiles: prod
    p1:
      pa: blublu

2-保留 personservice-devpersonservice 并在 bootstrap.yml 中定义:

spring:
  cloud:
    kubernetes:
      config:
        name: ${spring.application.name} #This is optional
        sources:
          - name: ${spring.application.name}-${PROFILE} # Here you get your `personservice-dev` configmap

关于Spring Cloud kubernetes 未加载配置映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58715824/

相关文章:

html - 保留 <form :input type ="file"> with Spring MVC 的值

java - 使用 String Replace() 或 ReplaceAll() 配置动态电子邮件内容

java - 使用 @RefreshScope 刷新 Bean,无需更改配置

gradle - 在 Gradle processResources 期间扩展 application.yml 会产生 MissingPropertyException

java - org.springframework.batch.item.ItemStreamException : Failed to initialize the reader in JdbcPagingItemReader

java - Spring 安全: How to access authenticated user in asynchronous Jobs

java - 服务注释类在存储库调用时抛出空指针异常

kubernetes - 使用minikube的Kubernetes持久卷

kubernetes - 有没有办法获取 Kubernetes 集群的 UID?

kubernetes - 如何通过 kubectl 命令识别静态 Pod?