浅谈Spring Cloud 系列 (二)之 小试 Spring Cloud Config(分布式/版本化配置)

序言

相信大家一定很好奇,分布式/版本化配置 这到底是个什么东西,其实呢,它就是市面上的全局配置中心,用来统一管理分布式的系统外部化配置。Okay,废话不多,下面开始介绍今天的重点,Spring Cloud Config。

简介

Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器映射的概念与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入。

本节采用的Srping Cloud Config版本为1.3.0.RELEASE

服务搭建

下面开始搭建Spring Cloud Conifg 的Server

首先 下载 Spring Cloud Config Server 源码

git clone https://github.com/spring-cloud/spring-cloud-config.git

cd spring-cloud-config/spring-cloud-config-server


../mvnw spring-boot:run

curl  http://localhost:8888/foo/development

{
    "name": "foo",
    "profiles": [
        "development"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/spring-cloud-samples/config-repo/foo-development.properties",
            "source": {
                "bar": "spam",
                "foo": "from foo development"
            }
        },
        {
            "name": "https://github.com/spring-cloud-samples/config-repo/foo.properties",
            "source": {
                "democonfigclient.message": "hello spring io",
                "foo": "from foo props"
            }
        },
        {
            "name": "https://github.com/spring-cloud-samples/config-repo/application.yml",
            "source": {
                "info.description": "Spring Cloud Samples",
                "info.url": "https://github.com/spring-cloud-samples",
                "eureka.client.serviceUrl.defaultZone": "http://localhost:8761/eureka/",
                "foo": "baz"
            }
        }
    ]
}

这样就可以启动一个简单的Srping Cloud Config Server ,当然我们也自己搭建一个Spring Cloud Conifg Server。(这个在下章节会详细讲解)

Spring Cloud Conifg Server 从git存储库中提取远程客户端的配置(必须提供):

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo

常见的HTTP服务具有的资源格式如下:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

如何来理解上面的占位符呢?对应的关系如下:

application:代表的是应用或服务的名称,在实际应用场景中,可以是应用服务(例如:会员服务,member),也可以代表数据 库连接(例如:贷款mysql数据库,mysql-loan),在客户端调用中,对应的是spring.config.name属性。

profile:代表的是区分不同环境的配置,在实际的应用场景中,可以用来区分 开发环境(dev),测试环境(test),生产环境(pro)...

label:label是可选的git标签(默认为“master”),当然 也可以是svn 等版本管理工具。

客户端调用

客户端调用的方式非常简单,首先穿件一个maven 项目,pom文件如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!-- repositories also needed for snapshots and milestones -->

接下来创建一个主程序:

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

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

}

接下来创建配置文件:bootstrap.properties (默认值: http://localhost:8888)

#Spring Cloud Config Server 地址
spring.cloud.config.uri: http://localhost:8888

Ps:由于bootstrap.properties 默认值为本地8888端口,也可以忽略此步。

接下来,启动主程序,访问/env ,会按照优先级展示属性内容。

curl http://localhost:8080/env

{
  "profiles":[],
  "configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
  "servletContextInitParams":{},
  "systemProperties":{...},
  ...
}

如上图中,configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},为优先级最高的属性。

至此,客户端就可以尽情的引用 Spring Cloud Config Server 中配置的属性了,下章节会详细讲解引用的细节。

跋文

本章节简单讲解了Spring Cloud Config Server 的搭建和 client 的简单调用,下章节会详细讲解 Srping Cloud Conifg Server 和Srping Cloud Config Client 的 资源库,安全,加密,快速失败,重试等,希望能跟大家在探讨中共同进步。

参考资料:

Spring Cloud 官方 :http://cloud.spring.io/spring-cloud-static/Dalston.RELEASE/

Spring Cloud 中文网 :https://springcloud.cc/

转载于:https://my.oschina.net/huangege/blog/896789

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值