springCloud - 第7篇 - 配置文件管理中心 ( SpringCloud Config )

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

一、简介

分布式的系统往往有多个服务,会有不同的配置文件。

不同环境配置文件内容也各不相同: 开发环境、测试环境、生产环境。

为了方便管理数目众多的配置文件,springcloud 有对应的配置文件管理:spring cloud config 。

在此组件中主要有 2 种 角色:config server  配置文件管理中心 、

                                              config client  从 config server 中读取真正需要的配置信息 。

配置文件支持放在本地,也支持放在远程 Git 仓库中 。

二、重用此系列工程的父 pom 工程 springcloud-base。

在其 pom 中加上依赖:

        <!--配置文件管理-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

新建文件夹 config , 并设置相应配置内容:

config-client-pro.properties :

version = pro -1.1.1.11- FZW -1.1.1

config-client-test.properties :

version =  test-2.2.2.22

config-client-dev.properties :

version = dev-0.0.0.DEV-0.0.0

三、 新建 config-server 工程

1. file - new - module 

2. spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。

3. 填写工程相关信息:包名、工程名等,next。

4. spring cloud config- 勾选 config server,next。

5. 工程名,代码存放位置等,finish 。

6. 工程结构如下:

7. 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.config</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>配置文件管理 server</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

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

</project>

8. 在工程启动类上加注解:@EnableConfigServer  ,开启配置文件管理服务功能 。

package com.config.configserver;


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

@SpringBootApplication

// 配置文件管理
@EnableConfigServer
public class ConfigServerApplication {

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

}

9. 配置文件中设置:

读取的是 springcloud-base工程中新增的 config 文件夹下的配置文件信息。

# 工程名
spring.application.name=config-server

# 端口
server.port=3333

# git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/FJ_WoMenDeShiJie/springcloud-base.git

# 仓库路径
spring.cloud.config.server.git.searchPaths=config

# 仓库分支
spring.cloud.config.label=master

# git仓库用户名 ( 公开库-可不填 )
spring.cloud.config.server.git.username=

# git仓库密码 ( 公开库-可不填 )
spring.cloud.config.server.git.password=

10. 启动工程访问:http://localhost:3333/这里随便写/dev

表示访问远程仓库中的配置文件成功 。访问路径最后的  dev  在此测试时也可任意写。

四、 新建 config-client 工程

1. file - new - module 

2. spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。

3. 填写工程相关信息:包名、工程名等,next。

4. spring cloud config- 勾选 config client,next。

5. 工程名,代码存放位置等,finish 。

6. 工程结构如下:

7. 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.config</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>配置文件管理 client</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

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

</project>

8. 在工程启动类上加注解:@RestController , 书写 getVersion 方法,提供对外访问。

package com.config.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${version}")
    String version;

    @RequestMapping(value = "/getVersion")
    public String getVersion() {
        return version;
    }

    /**
     * 增加此方法用以解决报错:Could not resolve placeholder 'version' in value "${version}"
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
        c.setIgnoreUnresolvablePlaceholders(true);
        return c;
    }

}

9. 配置文件设置:  注意此配置文件的名字是: bootstrap.properties 。

叫这个名字的原因:官方指定优先读取此文件,bootstrap 在 application 之前加载 。

# 项目名
spring.application.name=config-client

# 端口
server.port=4444

# 仓库分支
spring.cloud.config.label=master

# 读取文件:dev开发环境、test测试、pro生产
spring.cloud.config.profile=test

# 配置文件管理服务 config-server 地址
spring.cloud.config.uri=http://localhost:3333/

此时设置为读取 springcloud-base中的 配置文件 :config-client-test.properties

10. 启动工程访问:http://localhost:4444/getVersion

修改配置文件中访问的文件:

此时设置为读取 springcloud-base中的 配置文件 :config-client-dev.properties

重启工程访问:http://localhost:4444/getVersion

至此,config-server 从远程仓库读取配置文件成功。 config-client 通过 config-server 读取配置文件中的具体信息值也成功了。

在 config-client 配置文件中可通过修改 spring.cloud.config.profile 选项来设定读取不同的配置文件 。

-------------------------------------------------------------

下一篇:springCloud - 第8篇 - 配置文件管理中心 - 集群模式(负载匀衡)使用

源码见:https://gitee.com/FJ_WoMenDeShiJie/springcloud-base

https://gitee.com/FJ_WoMenDeShiJie/springcloud-config-server

https://gitee.com/FJ_WoMenDeShiJie/springcloud-config-client

-------------------------------------------------------------

PS:这个系列不定时更新,只是个人的学习分享,

内容全程参考书目:

《Spring Cloud 与 Docker 微服务架构空实战 》、

《Spring Cloud 微服务实战》及此书作者博客:http://blog.didispace.com/spring-cloud-learning/

《深入理解 Spring Cloud 与微服务构建》及此书作者博客:https://blog.csdn.net/forezp/article/details/70148833
--------------------------------------------------------------


以下出自:https://blog.csdn.net/forezp/article/details/81041028

config-server 启动成功后,访问路径映射方式有以下几种:

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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值