Spring Cloud部署时,如何将Kubernetes作为注册中心和配置中心一统江湖?

摘要:一、Spring Cloud 支持的常见注册中心和配置中心。 -> 关注清哥聊技术公众号,了解更多技术文章 Spring Cloud 自带的注册中心Eureka以及config配置中心 Nac
一、Spring Cloud 支持的常见注册中心和配置中心。->关注清哥聊技术公众号,了解更多技术文章 Spring Cloud 自带的注册中心Eureka以及config配置中心 Nacos,支持注册中心和配置中心等,可以参考:https://www.cnblogs.com/laoqing/p/17797759.html Zookeeper Consul Etcd Kubernetes ,当Spring Cloud 服务都是通过Kubernetes 部署时,可以使用Kubernetes 作为注册中心和配置中心。 二、Spring Cloud 部署时如何使用 Kubernetes 作为注册中心和配置中心 Spring Cloud Kubernetes提供了使用Kubernete本地服务的Spring Cloud通用接口实现。该存储库中提供的项目的主要目标是促进在Kubernetes中运行的Spring Cloud和Spring Boot应用程序的集成。 在Springboot中,Starters是一种方便的依赖描述符,可以包含在应用程序中。包括一个启动器来获取特性集的依赖项和Spring Boot自动配置。在使用Kubernetes 作为注册中心和配置中心时,需要集成如下的Starters 。 1、将服务名称解析为Kubernetes Services的发现客户端实现。 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes</artifactId> </dependency> 示例代码: @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } //Then you can inject the client in your code simply by autowiring it, as the following example shows: @Autowired private DiscoveryClient discoveryClient; 配置项: spring: cloud: kubernetes: enabled: true # discovery: # all-namespaces: true # enabled: true 2、从Kubernetes ConfigMaps和Secrets加载应用程序属性。ConfigMap或Secret更改时重新加载应用程序属性。 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes-config</artifactId> </dependency> 使用Kubernetes 的ConfigMap或Secret作为配置中心。 Kubernetes提供了一个名为ConfigMap的资源,以键值对或嵌入式application.properties或application.yaml文件的形式将要传递给应用程序的参数外部化。Spring Cloud Kubernetes Config项目使Kubernete ConfigMap实例在应用程序引导期间可用,并在观察到的ConfigMap实例上检测到更改时触发bean或Spring上下文的热重新加载。 默认行为是基于Kubernetes ConfigMap创建ConfigMapPropertySource,该ConfigMap的metadata.name值为Spring应用程序的名称(由其Spring.application.name属性定义)或bootstrap.properties文件中定义的自定义名称,位于以下键下:Spring.cloud.Kubernetes.config.name。 但是,可以使用多个ConfigMap实例进行更高级的配置。
阅读全文