Spring Cloud Config Server Starter Eureka是Spring Cloud生态系统中的一个重要组件,它结合了Spring Cloud Config和Eureka的功能,为微服务架构提供了强大的配置管理和服务发现能力。以下是关于它的详细介绍:
-
核心功能
- 配置管理:Spring Cloud Config提供了一个独立的微服务应用,作为配置中心,用于集中管理所有微服务的配置文件。这些配置文件可以存储在本地文件系统或远程仓库(如Git)中,并且支持版本控制。通过将配置信息集中管理,可以方便地在不同环境(如开发、测试、生产)之间切换配置文件,确保每个环境使用正确的配置。
- 服务注册与发现:Eureka是一个服务注册与发现的组件,帮助微服务之间互相发现和通信。当一个微服务实例启动时,它会向Eureka服务器注册自己的信息,包括服务ID、主机名、端口号等。其他微服务可以通过Eureka服务器来发现并获取这些已注册的服务实例的信息,从而实现服务之间的调用。
-
工作原理
- Config Server从指定的配置存储后端(如Git仓库)中加载配置文件,并将其提供给各个Config Client。当Config Client需要获取配置信息时,它会向Config Server发送请求,Config Server根据请求返回相应的配置信息给Config Client。如果配置信息发生了变更,Config Server会通知Config Client重新获取最新的配置信息。
- Config Server和Config Client都可以注册到Eureka服务器上,以便相互发现。当Config Client启动时,它会从Eureka服务器获取Config Server的地址,并与之建立连接获取配置信息。这样,即使Config Server的地址发生了变化,Config Client也能够通过Eureka服务器及时获取到新的地址。
-
优势
- 集中化配置管理:将所有的配置信息集中管理在一个位置,方便进行统一维护和管理,降低了配置管理的复杂性。同时,也便于对配置进行版本控制和回滚操作。
- 动态配置更新:当配置信息发生变更时,Config Client可以自动感知到这些变化,并重新获取最新的配置信息,无需重启服务。这使得应用程序能够在不停机的情况下动态地调整配置,提高了系统的可用性和灵活性。
- 高可用性:通过与Eureka集成,Config Server和Config Client可以实现高可用性。如果一个Config Server实例出现故障,其他的Config Server实例可以继续提供服务,不会影响整个系统的正常运行。同样,如果一个Config Client无法连接到某个Config Server实例,它可以从Eureka服务器获取其他可用的Config Server实例的地址,继续获取配置信息。
-
应用场景
- 微服务架构:在微服务架构中,不同的微服务可能需要共享一些公共的配置信息,如数据库连接信息、缓存服务器地址等。Spring Cloud Config Server Starter Eureka可以将这些公共的配置信息集中管理,并通过Eureka实现微服务之间的服务发现和通信,从而提高微服务架构的可维护性和扩展性。
- 多环境部署:对于需要在多个环境中部署的应用程序,如开发、测试、生产环境等,Spring Cloud Config Server Starter Eureka可以根据不同的环境加载相应的配置文件,确保应用程序在每个环境中都能使用正确的配置信息。
- 蓝绿部署和灰度发布:在进行蓝绿部署或灰度发布时,可以通过修改配置文件的方式来控制流量的切换和新功能的上线。Spring Cloud Config Server Starter Eureka可以方便地实现这种动态配置的更新,无需重启服务即可使新的配置生效。
综上所述,Spring Cloud Config Server Starter Eureka在微服务架构中扮演着至关重要的角色。它不仅简化了配置管理的复杂性,还提高了系统的灵活性和可维护性。无论是在微服务架构的搭建、多环境部署还是蓝绿部署和灰度发布等场景下,它都展现出了强大的功能和优势。
spring-cloud-config-server-starter-eureka
- 同步:每个 Eureka Server 同时也是 Eureka Client(逻辑上的)多个 Eureka Server 之间通过复制的方式完成服务注册表的同步,形成 Eureka 的高可用。
- 识别:Eureka Client 会缓存 Eureka Server 中的信息即使所有 Eureka Server 节点都宕掉,服务消费者仍可使用缓存中的信息找到服务提供者(笔者已亲测)。
- 续约:微服务会周期性(默认30s)地向 Eureka Server 发送心跳以Renew(续约)信息(类似于heartbeat)。
- 续期:Eureka Server会定期(默认60s)执行一次失效服务检测功能它会检查超过一定时间(默认90s)没有Renew的微服务,发现则会注销该微服务节点。
package com.programb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//开启配置服务器的支持
@EnableConfigServer
//开启Eureka server 作为客户端的支持
@EnableEurekaClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config #配置其他所需的配置文件的路径
server:
port: 8888
spring:
application:
name: config #在Eureka server 注册的服务名为config
profiles:
active: native # 配置服务器使用本地配置(默认为git 配置)
eureka:
instance:
non-secure-port: ${server.port:8888} #非ssl 端口,若环境变量中server.port有值,则使用环境变量的值,没有则使用8080
metadata-map:
instanceId: ${spring.application.name}:${random.value} #配置在Eureka server 的实例id
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ #Eureka 客户端设置的 Eureka server 地址
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springCloud-programb</artifactId>
<groupId>com.programb</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>