Spring Cloud Kubernetes未加载配置映射

我已经创建了名为personpersonservice和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获取配置图

Spring Cloud Kubernetes未加载配置映射

部署文件:

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
zcshou 回答:Spring Cloud Kubernetes未加载配置映射

您感到困惑。您的配置映射名为personservice-dev,而应用程序的名称为personservice而不是personservice-dev,默认情况下,Spring Cloud K8S会查找名称等于spring.application.name而不是spring.application.name-{profile}的configmap

您有2种解决问题的方法:

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
本文链接:https://www.f2er.com/3157868.html

大家都在问