开发人员在使用分布式系统时面临的常见挑战之一是管理单个系统的多个配置文件的成本。 由于这些服务已部署并经常更新,因此它们的配置管理成为开发人员+操作人员的问题。

在本部分中,我们将重点介绍用于管理配置的正确设计原则,并研究其在微服务架构中的用例。

Principles

为了开发强大的配置管理解决方案,在KloudOne,我们的第一个尝试是将我们的配置依赖项完全分开。 并且,执行此操作的标准方法之一是将依赖项隔离到单独的配置层。

由于所有其他服务都将使用此集中式配置层,因此最好通过用于外部通信的API接口使其集中和公开。

在体系结构级别,我们保证此服务层具有:

  • 明确的依赖隔离集权冗余使用API接口进行适当的抽象以进行外部通信

Tools

由于开发人员的生产力是组织的主要考虑因素之一,因此有许多经过实践检验的工具可用于确保更快的开发。 以下是一些现在市场上可用的产品:

提到的所有工具都具有很高的采用率和出色的社区支持。 根据您的用例,可以使用上述任何工具。

在本部分中,我们将着眼于Spring Cloud Config,因为它易于设置和使用


Spring Cloud Config

Spring Cloud Config是基于Spring Boot构建的基于REST的服务器。 它使用可嵌入的Spring Boot服务器进行抽象。 Spring Cloud Config可以使用本地文件系统git进行配置管理。

Spring Cloud服务将在引导期间客户端将侦听的端口上运行。

Setting up Config .properties file



server.port=9999
spring.profiles.active=native
cloud.config.server.native.searchLocations: file:///tmp/config/

server.port=9999
spring.profiles.active=native
cloud.config.server.native.searchLocations: file:///tmp/config/



这里本机代表git的文件系统或应使用其相应属性占位符的任何其他文件系统。

Launching the Spring Cloud Server

Spring Boot应用程序需要引导类来启动服务。 我们的配置服务器应用程序的示例:



@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
 SpringApplication.run(ConfigServerApplication.class, args);
 }
}

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
 SpringApplication.run(ConfigServerApplication.class, args);
 }
}



@EnableConfigServer注释将服务启用为Spring Cloud Config Server


Integrating with Spring Cloud Server

任何服务都可以与Spring Cloud Config服务器集成。 可以通过设置服务的application.properties文件轻松地建立该文件。 虚拟服务的示例:



spring.application.name=dummyservice
spring.profile=dev
spring.cloud.config.uri=http://localhost:9999/

spring.application.name=dummyservice
spring.profile=dev
spring.cloud.config.uri=http://localhost:9999/



The service properties can be validated in the Spring Cloud Config by using the endpoint: http://cloud-server-ip:port/dummyservice/default

默认参数将返回在应用程序中设置的默认配置文件。

Reading the properties in the Application

所有属性都可以在spring应用程序中使用@Value批注直接注入。



@Value("${sample.property}")
private String sampleProperty;

@Value("${sample.property}")
private String sampleProperty;



Conclusion

这些文章是我们技术解决方案堆栈系列的一部分。 如有任何疑问或疑问,请告诉我。 签出我们解决方案堆栈中的其他文章。