Spring Cloud Kubernetes - Spring boot 不读取配置映射

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

我有一个 Spring Boot Web 应用程序,它只是打印在 Kubernetes 的 ConfigMap 中传递的属性。

这是我的主要类(class):

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {

    private MyConfig config;
    private DiscoveryClient discoveryClient;

    @Autowired
    public DemoApplication(MyConfig config, DiscoveryClient discoveryClient) {
        this.config = config;
        this.discoveryClient = discoveryClient;
    }

    @RequestMapping("/")
    public String info() {
        return config.getMessage();
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping("/services")
    public String services() {
        StringBuilder b = new StringBuilder();
        discoveryClient.getServices().forEach((s) -> b.append(s).append(" , "));
        return b.toString();
    }

}

MyConfig类是:
@Configuration
@ConfigurationProperties(prefix = "bean")
public class MyConfig {

    private String message = "a message that can be changed live";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

基本上,通过调用根资源,我总是得到:

a message that can be changed live



调用/services 我实际上得到了一个 Kubernetes 服务列表。

我正在使用 kubectl create -f configmap-demo.yml 创建 ConfigMap是内容:
apiVersion: v1
kind: ConfigMap
metadata:
  name: demo
data:
    bean.message: This is an info from k8

和部署 kubecetl create -f deploy-demo.yml内容是:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
  labels:
    app: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      # this service account was created according to
      # https://kubernetes.io/docs/reference/access-authn-authz/rbac/#service-account-permissions
      # point 5 - Grant super-user access to all service accounts cluster-wide (strongly discouraged)
      serviceAccountName: i18n-spring-k8
      containers:
      - name: demo
        image: aribeiro/sck-demo
        imagePullPolicy: Never
        env:
        - name: JAVA_OPTS
          value:
        ports:
        - containerPort: 8080
      volumes:
      - name: demo
        configMap:
          name: demo

问题是访问root资源时/我总是得到默认的硬编码值,而不是 Kubernetes 的 ConfigMap 中定义的值。

示例项目还带有 yaml 文件和 Docker 文件,可在 https://drive.google.com/open?id=107IcwnYIbVpmwVgdgi8Dhx4nHEFAVxV8 获得.

还检查了启动调试日志,我没有看到任何错误或线索为什么它不应该工作。

最佳答案

Spring Cloud Kubernetes documentation不完整。它缺少包含此依赖项以启用从 ConfigMap 加载应用程序属性的指令:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
</dependency>

关于Spring Cloud Kubernetes - Spring boot 不读取配置映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53960467/

相关文章:

kubernetes - 为什么从主机而不是gcloud交互式shell使用gcloud命令如此困难?

java - (UML) Spring-Statemachine 在 statemachine.stop() 之后仍在运行

java - Spring 建议不适用于某些类的某些方法

java - 为什么spring不使用ajax上传呢?

spring-boot - Spring Boot App 在 docker 上消耗过多内存

java - Spring Boot 将 JSON 和简单属性传递给相同的映射

java - 仅从表中选择某些列

kubernetes - 无法使用 minikube 设置 Istio

amazon-web-services - 在 s3 和 kubernetes 容器之间复制文件

java - <aws-messaging :annotation-driven-queue-listener/>? 的基于 Java 的配置版本是什么