【问题标题】:is spring cloud kubernetes support spring cloud loadbalancer?Spring Cloud Kubernetes 支持 Spring Cloud 负载均衡器吗?
【发布时间】:2020-10-06 23:01:38
【问题描述】:
【问题讨论】:
标签:
spring-cloud-kubernetes
【解决方案1】:
是的,确实如此。
一个完整的例子可以在这里找到 - https://github.com/dhananjay12/spring-microservices-using-spring-kubernetes
基本上,跟随依赖就可以了
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-archaius</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
<exclusion>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-eureka</artifactId>
</exclusion>
</exclusions>
</dependency>
【解决方案2】:
我迟到了,但我想分享对 Spring Cloud LoadBalancer 的支持已添加到 Spring Cloud Kubernetes last year 中,它就像一个魅力。从 Spring Cloud 2020.0 开始。 (又名 Ilford),它是默认的客户端负载均衡器,因此 您不再需要排除功能区。
我建议在您的项目中包含 org.springframework.cloud:spring-cloud-starter-kubernetes-client-all 依赖项,以利用 Spring Cloud Kubernetes 启用的所有细节。
在 Kubernetes 上使用 Spring Cloud LoadBalancer 的最典型方式是使用服务发现。如果您的类路径中有任何 DiscoveryClient,则默认的 Spring Cloud LoadBalancer 配置使用它来检查服务实例。因此,它只会从已启动并正在运行的实例中进行选择。所需要做的就是使用 @EnableDiscoveryClient 注释您的 Spring Boot 应用程序以启用 K8s-native 服务发现。
您可以在 Spring Cloud Kubernetes documentation 上阅读有关它的更多信息。