springcloud学习之config统一管理微服务配置

springcloud学习之config统一管理微服务配置

在微服务架构中一般对于配置管理一般有一下需求
1 集中管理配置
2 不同环境不同配置
3 运行期间可以动态调整。
4 配置修改后自动更新

综上所需要的特点,springcloud提供了spring cloud config 这个统一管理微服务配置的项目

当前版本是
springboot 2.1.6
springcloud Greenwich.SR1

1 什么是spring cloud config

Spring Cloud Config 为分布式系统外部化配置提供了服务器和客户端的支持。它包含了两个部分Config Server 和Config Client。
Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理引用程序各个环境下的配置,默认使用Git存储配置内容。
Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性。

2 搭建Config Server服务

2.1 pom

核心pom

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

整体pom

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloud</groupId>
    <artifactId>config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

2.2 启动类配置

这里使用的注解是
@EnableConfigServer //声明这是个Config Server 服务

在这里插入图片描述

2.3 yaml配置
server:
  port: 8081
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/imqw/config-application //配置文件存放的git仓库地址
          username:
          password:
      label: master          #git配置文件分支

可以自己在github上创建一个

git仓库地址

目录结构
在这里插入图片描述
可以看到 本仓库下有两个子文件(之后客户端做加载指定目录下的配置文件中用到)

application-dev.yaml

profile: dev-1.0

name: application-dev

application-test.yaml

profile: test-1.0  

name: application-test  #存放配置文件位置
2.4 测试

启动服务

输入 http://localhost:8081/application/dev
可以直观的看到应用名称,项目profile,等配置信息

在这里插入图片描述

或者输入 http://localhost:8081/application-dev.yaml
直接返回配置文件中的属性

在这里插入图片描述
到这里简单的config server搭建完成

3 搭建Config Client服务

3.1 pom

核心pom

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

整体pom

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.config</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

3.2 yaml配置

这里需要注意的是config client的服务

application.yaml 可以配置服务启动端口

server:
  port: 8082
name: qiuwei

bootstrap.yaml config client 必须要有的配置文件 必须叫这个名字 用于连接config server服务

spring:
  application:
    name: applicaiton # 对应config server 所获取的配置文件的{application}
  cloud:
    config:
      uri: http://localhost:8081/
      profile: test  # 对应config server 文件中的 profile
      label: master  # git 对应的分支
3.3 测试

编写controller

ClientController


package com.config.client;

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

@RestController
public class ClientController {

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

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

    @GetMapping("/profile")
    public String hello(){

        return this.profile +"   :    "+this.name;
    }

}

启动服务

输入 http://localhost:8082/profile
在这里插入图片描述

对比
在这里插入图片描述

到此config client 服务读取到了 git仓库的配置了

4 config client 读取指定目录下的配置文件

在实际开发中我们的配置文件可能会根据不同模块存放在不同的子目录下
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

如:我需要config client 加载cloulconf中的配置 cloudconf-dev.yaml

4.1 改造项目

首先当前的需求是在统一个数据库中 所以在config server 中需要修改yaml

application.yaml

server:
  port: 8081
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/imqw/config-application
          username:
          password:
          search-paths: '{application}' #根据输入的{application}自动匹配对应的配置文件--针对有子目录的库
      label: master          #git配置文件分支

search-paths 这个参数 可以配置当前库中对应的子目录 如果要根据 {application} 匹配不同的子目录 可以配置成 '{application}'

修改 config client

bootstrap.yaml

spring:
  application:
    name: cloudconf
  cloud:
    config:
      uri: http://localhost:8081/
      profile: dev
      label: master
4.2 测试

启动服务
输入 http://localhost:8082/profile

在这里插入图片描述

5 案例源码

demo案例

配置文件仓库

到此简单实现 spring cloud conf 功能 在下一章将会说明 动态更新配置功能

参考文档 : 《Spring cloud 与Docker微服务架构实战》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值