小编alo*_*o42的帖子

Spring Cloud Kubernetes 应用程序无法检测到它何时在 Pod 中运行

预期行为

根据 Spring Cloud Kubernetes 文档,Spring 应用程序检测其何时在 Pod 中运行所需要做的就是将 Spring Cloud Kubernetes 及其spring-cloud-kubernetes-all依赖项添加到类路径中。

这会触发两个事件:

  1. 如果存在,则application-kubernetes.yml加载配置文件。
  2. Kubernetes 实现DiscoveryClient用于服务发现,利用 KubeDNS 而不是默认的 Eureka。

这种环境意识在这里描述: https://cloud.spring.io/spring-cloud-static/spring-cloud-kubernetes/1.1.0.RELEASE/reference/html/#kubernetes-profile-autoconfiguration

和这里:

https://cloud.spring.io/spring-cloud-static/spring-cloud-kubernetes/1.1.0.RELEASE/reference/html/#discoveryclient-for-kubernetes

实际行为

使用以下 yaml 启动 Spring Cloud Kubernetes 应用程序并http://minikube-ip:port/actuator/env通过 NodePort 访问其执行器端点后,我发现没有活动配置文件集,即使文件夹application-kubernetes.yml中存在resources

{
"activeProfiles": [],
...
Run Code Online (Sandbox Code Playgroud)

经过对http://minikube-ip:port/actuator/info端点的进一步调查,我发现 Spring 应用程序根本不知道它位于 pod 中,如下所示:

{
    "kubernetes": {
        "inside": false
    }
}
Run Code Online (Sandbox Code Playgroud)

我的代码

SpringCloud网关

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableDiscoveryClient …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot spring-cloud spring-cloud-kubernetes

5
推荐指数
1
解决办法
9312
查看次数