1

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

我正在使用配置文件 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 获取配置映射

在此处输入图像描述

部署文件:

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"

引导程序:

spring:
  application:
    name: personservice
4

1 回答 1

1

你把事情弄糊涂了。您的 configmap 已命名personservice-dev,而您的应用程序的 namepersonservice不是personservice-dev,默认情况下 Spring Cloud K8S 会查找名称等于spring.application.name和 not的 configmap 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
于 2019-11-05T19:13:07.370 回答