springCloud(六)分布式配置中心

本文介绍了Spring Cloud Config在分布式系统中的应用,它提供server和client支持外部资源配置,用Config Server管理所有环境的外部属性文件,默认存储后端是git。还阐述了Server和Client的特点,给出代码示例,指出存在的问题及注意事项,如版本不匹配异常等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一个分布式系统中,springcloud config 提供了server和client,用以支持外部资源配置,用一个配置中心config Server管理程序所有环境的外部属性文件。server 和client 结合的这种思想和 Spring enviroment 与 propertySource 完全相同,因此他们非常适合spring 程序。当一个应用程序从开发环境移动到测试环境,再到正式环境,你可以通过配置中心管理这些配置文件,以提供应用程序在所有环境中需要用到的资源。配置中心server 默认的存储后端是git,   因此能够很简单的支持不同版本的配置环境。

特点:

Spring Cloud Config Server:

1、加密和解密的属性值(对称或非对称)。

2、使用@EnableConfigServer就能够嵌入到Spring Boot 程序中。

3、通过http请求传递外部资源。

Config Client

1、绑定Config Server,通过远程资源初始化Spring 环境。

2、加密和解密的属性值(对称或非对称)。

上面是从官网翻译的:https://spring.io/projects/spring-cloud-config#overview

一、代码:git:https://github.com/aeolusway/aeolusway.git

1、Spring Cloud Config Server

(1)包结构

(2)ConfigServer

package com.aeolusway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


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

(3)application.properties

spring.application.name=config-server
server.port=8889
# git 仓库地址
spring.cloud.config.server.git.uri=https://github.com/aeolusway/aeolusway.git
# 仓库路径===匹配查询的路径名
spring.cloud.config.server.git.searchPaths=git
# 配置仓库的分支
spring.cloud.config.label=master
# git 用户名和密码,这里是公开的,所以为空
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=

(4)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.aeolusway</groupId>
    <artifactId>SpringCloud-config-server</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2、Config Client

(1)包结构

(2)ConfigClient

package com.aeolusway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RestController;


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

}

(3)ConfigClientController

存在问题:

1、读取属性文件,中文存在乱码问题

2、Spring Cloud 到底默认给封装了一些啥东西?(程序中username的由来,因为属性文件中并没有配置这个属性)

package com.aeolusway.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ConfigClientController {
    // 读取git上的config-client-uat.properties 文件中的description属性
    @Value("${description}")
    private String description;
    @RequestMapping("/getDesc")
    public String getDesc(){
        return description;
    }
    // 很奇怪的是,config-client-uat.properties 中并没有 username这个属性,
    //但是访问这个返回的是我计算机的adminstrator的用户名,springcloud 默认给我封装了一些属性?
    @Value("${username}")
    private String username;
    @RequestMapping("/getUser")
    public String getUser(){
        return username;
    }

     // 读取git上的config-client-uat.properties 文件中的question属性,question是中文,存在乱码问题
    @Value("${question}")
    private String question;
    @RequestMapping("/getQue")
    public String getQue(){
        return question;
    }
}

(4)bootstrap.properties

这个文件我试了试使用名为application.properties 去配置,会报错:Could not resolve placeholder 'description' in value "${description}"

bootstrap 和application 程序启动后都会被加载,而且bootstrap先于application,但是使用application.properties  去配置配置中心,程序都无法启动,这就非常有意思了——>说明:程序会首先加载名为bootstrap的属性文件(.properties或.yml)去启动程序,程序启动之后再加载application的属性文件。

spring.application.name=config-client
server.port=8888
# 配置仓库的分支
spring.cloud.config.label=master
# spring.application.name + - + spring.cloud.config.profile + .properties为服务中心的属性文件名
# 在这里,配置中心的文件名即为 config-client-uat.properties
spring.cloud.config.profile=uat
# 指明配置服务中心的网址
spring.cloud.config.uri=http://localhost:8889/

(5)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.aeolusway</groupId>
    <artifactId>SpringCloud-config-client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

二、注意事项

1、异常:springcloud java.lang.AbstractMethodError: null,原因:Spring Boot 和Spring Cloud 的版本不匹配。

如果 spring-cloud-config-client 使用的是2.1.1.RELEASE版本就会抛这个异常

2、git上属性文件的命名根据 client 中bootstrap属性文件中配置的属性命名:

spring.application.name=config-client
spring.cloud.config.profile=uat

=====git 上的文件命名即位:config-client-uat.properties

3、默认优先加载配置中心的配置文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值