Spring-Cloud-Config学习笔记(一):使用本地存储

简介

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用上。
随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。
服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。

Spring Cloud Config服务端特性

  • HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
  • 属性值的加密和解密(对称加密和非对称加密)
  • 通过使用@EnableConfigServer在Spring boot应用中非常简单的嵌入。

Config客户端的特性(特指Spring应用)

  • 绑定Config服务端,并使用远程的属性源初始化Spring环境。
  • 属性值的加密和解密(对称加密和非对称加密)

说明

由于网上一般都是关于git存储的默认式教程,如Spring Cloud Config 分布式配置中心使用教程
但并不是所有公司都使用git,比如我们的生产环境,每个服务都部署在不同的服务器,且无法访问git,所以本文主要记录文件系统存储辅加Eureka服务发现功能的方式。

Server端

依赖

pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yan</groupId>
    <artifactId>spring.cloud.config-client-a</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

</project>

启动类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ClientAApplication {

    @Value("${profile}")
    private String profile;

    @GetMapping("/")
    public String home() {
        return profile;
    }

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

}

配置

bootstrap.yml

server:
  port: 8888
spring:
  application:
    name: spring-cloud-config-server

测试

启动服务,打开网址http://localhost:8888/svcA/pro,返回值:

{
  "name": "svcA",
  "profiles": [
    "pro"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/svcA-pro.yml",
      "source": {
        "server.port": 8082,
        "profile": "pro"
      }
    },
    {
      "name": "classpath:/svcA.yml",
      "source": {
        "server.port": 8081,
        "profile": "default"
      }
    }
  ]
}

如果有返回值,说明我们的配置生效了,从所示URL中可以看出一些端倪,比如,当输入http://localhost:8888/svcA/default,那么应返回:

{
  "name": "svcA",
  "profiles": [
    "default"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/svcA.yml",
      "source": {
        "server.port": 8081,
        "profile": "default"
      }
    }
  ]
}

官方文档列举了几种内置访问规则:

/{application}/{profile}[/{label}]  <--- 我所采用的方式
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

由于我们没有使用git,因此没有label也就是分支的概念,所以我们采用不带label的方式访问。
当采用其他方式访问时,结果类似读取配置文件并展示相应内容,如,http://localhost:8888/svcA-pro.yml返回:

profile: pro
server.port: 8082

猜你喜欢

转载自www.cnblogs.com/yw0219/p/10421160.html