基于Git的Spring Cloud Config极速搭建

基于Git的Spring Cloud Config极速搭建

0.阅读完本文你将会学到

  • 如何基于Git搭建一个Spring Cloud Config服务器

1.概述

Spring Cloud Config是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,Server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,Client通过接口获取数据、并依据此数据初始化自己的应用。

这种配置管理方案在Git版本控制的加持下可以在应用运行时进行修改。虽然它非常适用于Spring应用程序,使用了所有支持的配置文件格式以及EnvironmentPropertySource@Value等,但它可以用于运行任何编程语言的任何环境。

在这篇文章中,我们将着重介绍一个例子,说明如何设置一个由Git支持的配置服务器,如何在一个简单的REST应用服务器中使用它。

2.项目设置和依赖性

首先我们先创建两个新的Maven项目。Server项目依赖如下:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Client项目依赖如下:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3.配置服务器的实现

应用程序的主要部分是一个配置类–更确切地说,就是@SpringBootApplication–它通过自动配置注解@EnableConfigServer引入所有需要的设置。

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
    
    public static void main(String[] arguments) {
        SpringApplication.run(ConfigServer.class, arguments)。
    }
}

现在我们需要配置服务器监听的端口和提供版本控制的配置内容的Git-url。后者可以使用http、ssh等协议或本地文件系统上的一个简单文件。

提示:如果你打算使用多个配置服务器实例指向同一个配置仓库,你可以配置服务器将你的版本克隆到本地临时文件夹。但要注意有双重验证的私有存储库,它们很难处理。在这种情况下,把它们克隆到你的本地文件系统上,然后用这个副本工作会更容易。

我们还需要在application.properties中为Basic-Authentication设置一个用户名和一个密码,以避免每次重启应用程序时自动生成密码。

server.port=8888
spring.cloud.config.server.git.uri=ssh://localhost/config-repo
spring.cloud.config.server.git.clone-on-start=true
spring.security.user.name=jayxu
spring.security.user.password=s3cr3t

4.作为配置存储的 Git 仓库

为了完成我们的服务器,我们必须在配置好的URL下初始化一个Git仓库,创建一些新的属性文件。

配置文件的名称像普通的application.properties一样组成,但是用配置的名称代替application,例如客户端的属性spring.application.name的值,后面加一个破折号和活动配置文件。

例如:

$> git init
$> echo 'user.role=Developer' > config-client-development.properties
$> echo 'user.role=User'      > config-client-production.properties
$> git add .
$> git commit -m 'Initial config-client properties'

提示:如果你遇到与ssh相关的认证问题,请仔细检查你的ssh服务器上的~/.ssh/known_hosts和~/.ssh/authorized_keys!

5.查询配置

现在我们能够启动我们的服务器了。我们的服务器所提供的基于Git的配置API可以通过以下路径进行查询。

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中{label}占位符指的是一个Git分支,{application}指的是客户端的应用程序名称,{profile}指的是客户端当前的活动应用程序配置文件。

最后,我们可以通过下面的命令在master分支中的开发配置文件中为客户端获取配置。

$> curl http://jayxu:s3cr3t@localhost:8888/config-client/development/master

6.客户端实现

接下来,让我们来处理一下客户端的问题。这将是一个非常简单的客户端应用程序。

为了获取我们的服务器,配置必须放在application.properties文件中。Spring Boot 2.4引入了一种新的方式,使用spring.config.import属性来加载配置数据,现在这是绑定到配置服务器的默认方式。

@SpringBootApplication
@RestController
public class ConfigClient {
    
    @Value("${user.role}")
    private String role;

    public static void main(String[] args) {
        SpringApplication.run(ConfigClient.class, args);
    }

    @GetMapping(
      value = "/test/{username}",  
      produces = MediaType.TEXT_PLAIN_VALUE)
    public String test(@PathVariable("username") String username) {
        return String.format("Hello!
          You're %s and you'll become a(n) %s...n"
, username, role);
    }
}

除了应用程序名称,我们还在application.properties中加入了活动配置文件和连接细节。

spring.application.name=config-client
spring.profiles.active=development
spring.config.import=optional:configserver:http://jayxu:s3cr3t@localhost:8888

这将连接到配置服务器http://localhost:8888,在启动连接时也将使用HTTP。我们还可以分别使用spring.cloud.config.usernamespring.cloud.config.password属性设置用户名和密码。

在某些情况下,如果一个服务无法连接到配置服务器,我们可能想让它的启动失败。我们可以去掉optional:前缀,使客户端停止运行。

为了测试,如果配置从我们的服务器正确接收,并且role被注入到我们的Controller方法中,我们只需在启动客户端后使用以下命令:

$> curl http://localhost:8080/test/JayXu

如果响应如下,我们的Spring Cloud配置服务器及其客户端目前工作正常。

Hello! You're JayXu and you'll become a(n) Developer...

8.结语

现在我们就可以创建一个配置服务器,从 Git 仓库向客户端应用程序提供一组配置文件。


原文始发于微信公众号(下班的思想家):基于Git的Spring Cloud Config极速搭建

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/20082.html

(0)

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!