spring boot 无法读取点分隔的环境变量

问题描述

项目使用docker打包,在k8s 环境变量或者docker-compose.yaml指定的点分隔环境变量无法被spring识别。
例如:
如果在程序的application.yml里面如下设置

server: 
  prot: 80

在docker-compose.yaml里设置

server.port=79

预期服务端口为79,但实际测试发现为80。

原因

操作系统不支持点分隔的环境变量

spring文档中有提到:

If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (for example, SPRING_CONFIG_NAME instead of spring.config.name). See Binding from Environment Variables for details.

解决

  1. 使用大写字母+下划线 替代 小写字母+点 的方式。
    例如,改为:
SERVER_PORT=79
  1. 更换镜像的系统
    我原来的是ubuntu的,改为如下
FROM openjdk:17-jdk-alpine3.14

如果以上不能解决,还有一种说法是alpine中使用sh -c启动jar会忽略点号分隔的环境变量,详见
https://cloud.tencent.com/developer/article/1965723

你可能感兴趣的:(错误集合,spring,boot,后端,java)