首页 > 解决方案 > 如何连接我的 Spring Boot pod 以从 Kubernetes World 中的 Spring Cloud Config Server 获取配置

问题描述

顾名思义,我想让我的 Spring Boot pod 从 Spring Cloud Config Server pod 获取配置属性,所有这些都在 Kubernetes 的帮助下,但我被困在两件事上,我不知道该怎么做,我认为这与使用 Kubernetes Networking 错误有关:

我的 CONFIG 属性在我的部署清单中用作环境。多变的:

apiVersion: v1
kind: Service
metadata:  
  name: fotbalist-service

spec:
  selector:
    app: fotbalist
 ports:
   - protocol: 'TCP'
     port: 2000
     targetPort: 2000
     type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fotbalist

spec:
  replicas: 1
  
  selector:
    matchLabels:
      app: fotbalist
  
  template:
    metadata:
      labels: 
        app: fotbalist   
    spec:
      containers:
        - name: fotbalist-container
          image: cinevacineva/fotbalist
          imagePullPolicy: Always
          ports:
            - containerPort: 2000
          
          env:
            - name: CONFIG
              valueFrom:
                configMapKeyRef:
                  name: config-map
                  key: host 

和我的 Spring Cloud Config Server 的 ConfigMap:

#Config ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-map
 
data:
  host: CONFIG_SERVICE_HOST

当我尝试以这种方式使用它时得到下一个输出,并且没有连接到获取属性:

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://CONFIG_SERVICE_HOST
 c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url -http://CONFIG_SERVICE_HOST. Will be trying the next url if available

我的另一个应用程序尝试不同的方法,连接到 Config 的 app.properties 是相同的,但使用的 ENV 不同:

 env:
    - name: CONFIG
      value: CONFIG_SERVICE_HOST

输出:

c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: Invalid URL: http://CONFIG_SERVICE_HOST:tcp://10.101.163.39:3000
c.p.again.InfoEurekaServerApplication    : No active profile set, falling back to default profiles: default

我的 Spring Cloud Config 应用程序清单/服务:

#service Config
apiVersion: v1
kind: Service
metadata:
  name: config #DNS NAME
  labels:
    app: config

spec:
  selector:
    app: config
  ports:
   - protocol: 'TCP'
     port: 3000
     targetPort: 3000
  type: ClusterIP 
  
  
 
    
---
#Deployment Config
apiVersion: apps/v1
kind: Deployment
metadata:
  name: config

spec:
  selector:
    matchLabels:
      app: config
  replicas: 1
  template:
    metadata:
      labels:
        app: config
    spec:
      containers:
        - image: cinevacineva/config
          name: config
          imagePullPolicy: Always
          ports:
            - containerPort: 3000
              name: config

我做错了什么?

标签: javaspring-bootkubernetesspring-cloud-configspring-cloud-kubernetes

解决方案


推荐阅读