【问题标题】:Unable to load configtree if we use spring-cloud-config-server如果我们使用 spring-cloud-config-server 则无法加载 configtree
【发布时间】:2023-01-05 08:06:44
【问题描述】:

不工作场景:

我正在尝试在 EKS 集群中部署 spring-boot 应用程序。

spring-config-server 帮助加载我的应用程序存储库中的(项目)属性

  • 应用程序-dev.yml(来自存储库)
spring: 
  config: 
    import: optional:configtree:/usr/src/apps/secrets/database/postgresql/ # not effective while loading into application by configserver
  datasource: 
    url: jdbc:postgresql://{xxxxxxxxxxxxxx}:{5444}/postgres
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    hikari: 
      schema: mydbschema

spring-config-server 已启动并运行! 当我尝试在 EKS 中使用 deployment.yaml 文件部署我的应用程序时,configtree:/usr/src/apps/secrets/database/postgresql/ 属性没有显示任何效果以设置 env 道具。

  • 部署.yaml
      volumes: 
      - name: tmpfs-1
        emptyDir: {}
      - name: secret-volume-postgresql
        secret:
          secretName: db_secrets
      containers:
      - name: {{ .Chart.Name }}
        securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - "--server.port=8080"
        - {{ default "dev" .Values.applicationActiveProfiles }}
        env:
        - name: ACTIVE_PROFILES
          value: {{ .Values.applicationActiveProfiles }}
        - name: DB_USERNAME
          value: {{ .Values.db.userName }}
        - name: NAME_SPACE
          value: {{ default "dev" .Values.selectedNamespace }}
        ports:
        - name: http
          containerPort: {{ .Values.containerPort }}
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /actuator/health/readiness
            port: http
            scheme: HTTP
          initialDelaySeconds: 15
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /actuator/health/liveness
            port: http
            scheme: HTTP
          initialDelaySeconds: 15
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        volumeMounts:
        - name: secret-volume-postgresql
          mountPath: /usr/src/apps/secrets/database/postgresql 
        - name: tmpfs-1
          mountPath: /tmp

  • 我的应用程序(项目)
    • 应用.yml
---
spring:
  application:
    name: myapplication
  config:
    import: optional:configserver:http://config-server.dev.svc:8080/
  cloud:
    config:
      label: develop
  profiles:
    active:
    - dev 

工作场景:

在 deployment.yaml 文件中进行一项额外更改,将 spring.config.import 设置为 arg。

      containers:
      - name: {{ .Chart.Name }}
        securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - "--server.port=8080"
        - {{ default "dev" .Values.applicationActiveProfiles }}
        # ******It's working after adding the below line******
        - "--spring.config.import=optional:configtree:/usr/src/apps/secrets/database/postgresql/"

题:为什么它不考虑我设置的属性myapplication-dev.yml文件 ?这里缺少什么?

【问题讨论】:

    标签: spring-boot kubernetes spring-cloud-config-server


    【解决方案1】:

    dev 配置文件处于活动状态时,对于 Spring Boot 检测到的配置文件特定配置文件,它应该命名为 application-dev.yml 而不是 myapplication-dev.yml,默认情况下它应该位于与 application.yml 相同的目录中。

    来自https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.profile-specific

    除了应用程序属性文件,Spring Boot 还将尝试使用命名约定加载特定于配置文件的文件应用程序-{profile}.例如,如果您的应用程序激活一个名为产品并使用 YAML 文件,然后两者应用.yml应用程序prod.yml会被考虑。

    配置文件特定的属性从与标准 application.properties 相同的位置加载,特定于配置文件的文件始终覆盖非特定文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-28
      • 2020-09-05
      • 1970-01-01
      • 2015-03-26
      • 2017-10-20
      • 2019-01-12
      • 2017-06-08
      • 2015-06-18
      相关资源
      最近更新 更多