08.SpringCloud-Config配置中心

一、为什么需要使用配置中心

1.服务配置的现状

在微服务系统中,每个微服务不仅仅只有代码,他还需要连接其他资源,例如数据库的配置或功能性的开关等等,但是随着微服务系统的不断迭代,整个微服务系统可能会成为一个网状结构,这个时候就要考虑整个微服务系统的扩展性、伸缩性、耦合性等等。其中一个很重要的环节就是配置管理的问题。

常见的配置类型

服务配置各类开关业务配置
数据库配置功能开关模块A
MQ队列配置业务开关模块B
redis缓存配置服务开关模块C

2.常用的配置管理解决方案的缺点

  1. 硬编码【缺点:需要修改代码、繁琐、风险大】
  2. 写在properties里面【缺点:在集群环境下,需要替换和重启】
  3. 写在xml配置文件中一般和应用一起打包【缺点:需要重新打包和重启】

3.使用spring cloud config配置中心的原因

由于常用的配置管理有很大的缺点,故spring cloudconfig采用了集中式的配置中心来管理每个服务的配置信息。 spring cloud config配置中心,在微服务分布式系统中,采用服务端和客户端来提供可扩展的配置服务。配置中心负责管理所有的服务的各种环境配置文件。配置服务中心默认采用Git的方式存储配置文件,因此我们很容易部署修改,有助于对环境配置进行版本管理

4.SpringCloud Config配置中心解决了什么问题?

Spring Cloud Config它解决了微服务配置的中心化、版本控制、 平台独立、 语言独立等问题
其特性如下

  1. 提供服务端和客户端支持(spring cloud config server spring cloud config client)
  2. 集中式管理分布式环境下的应用配置
  3. 基于Spring环境,无缝与Spring应用集成
  4. 可用于任何语言开发的程序
  5. 默认实现基于git仓库,可以进行版本管理

二、ConfigServer

1.创建项目

在这里插入图片描述
添加相关依赖

    <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.版本控制

gitee注册账号
在这里插入图片描述
创建仓库
在这里插入图片描述

3.修改属性文件

spring.application.name=shop-product-provider
server.port=9088

# 设置服务注册中心的地址
eureka.client.service-url.defaultZone=http://bb:1234@192.168.134.129:8761/eureka/,http://bb:1234@192.168.134.131:8761/eureka/

# Git配置
spring.cloud.config.server.git.uri=https://gitee.com/peibiaozhang/biao-config

4.配置文件

在这里插入图片描述

在项目中分别创建4个配置文件,每个文件中都有一个shop-tag,但是值不一样

5.把属性文件上传到创建的仓库

在这里插入图片描述

6.删除属性文件并修改启动类

package com.biao;

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

@EnableConfigServer
@SpringBootApplication
public class SpringcloudConfigServerApplication {

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

}

7.测试访问

http://localhost:9088/config-client/dev
在这里插入图片描述
http://localhost:9088/config-client/test
在这里插入图片描述
http://localhost:9088/config-client/prod
在这里插入图片描述
http://localhost:9088/config-client/default
在这里插入图片描述

8.命名规则

通过访问我们可以发现,配置文件的名称不是随便定义的,而是有一定的命名规则在这里

配置文件命名规则

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

application:表示目标服务的名称
profile:表示获取指定环境下配置,例如开发环境、测试环境、生产环境 默认值default,实际开发中可以使dev\test\prod等等
label:git标签,默认值master

举例

  • 主干:

https://gitee.com/peibiaozhang/biao-config
文件如下:
config-client.properties
config-client-dev.properties
config-client-test.properties
config-client-prod.properties
访问URL:
https://gitee.com/peibiaozhang/biao-config/dev
https://gitee.com/peibiaozhang/biao-config/text
https://gitee.com/peibiaozhang/biao-config/prod
https://gitee.com/peibiaozhang/biao-config/default

  • 分支
    https://gitee.com/peibiaozhang/biao-config/tree/branch1.0/
    文件如下:
    config-client.properties
    config-client-dev.properties
    config-client-test.properties
    config-client-prod.properties
    访问URL:
    https://gitee.com/peibiaozhang/biao-config/dev/branch1.0/
    https://gitee.com/peibiaozhang/biao-config/test/branch1.0/
    https://gitee.com/peibiaozhang/biao-config/prod/branch1.0/
    https://gitee.com/peibiaozhang/biao-config/default/branch1.0/

三、ConfigClient

1.创建项目

在这里插入图片描述
相关依赖

    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.修改配置文件

注意:在配置中心的客户端服务中,配置文件的名称必须是bootstrap.properties或者bootstrap.yml文件

官方解释:
Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解
密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。

spring.application.name=config-client
server.port=9087

# 设置服务注册中心的地址
eureka.client.service-url.defaultZone=http://bb:1234@192.168.134.129:8761/eureka/,http://bb:1234@192.168.134.131:8761/eureka/

# 开启配置中心
spring.cloud.config.discovery.enabled=true
# 对应的eureka 中的配置中心 serviceId 默认是 config-server
spring.cloud.config.discovery.service-id=config-server

# 指定环境
spring.cloud.config.profile=dev
# git 标签
spring.cloud.config.label=master

3.创建控制器

package com.biao.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 ShowController {

    @Value("${shop-tag}")
    private String msg;

    @RequestMapping("/show")
    public String showMsg(){
        return "-->"+msg;
    }
}

4.启动服务

在这里插入图片描述

四、动态刷新

1.原理结构

在这里插入图片描述
通过此图我们能看到配置服务其实是从本地的Git仓库中获取的信息,Git本地库通过pull命令同步远程库中的内容。当配置中心客户端重新启动的时候会显示的执行pull命令来拉取最新的配置信息,这种方式是需要重启客户端服务的,显然不是太友好,这时我们可以通过Actuator来实现。

2.动态刷新案例

2.1 添加依赖

在这里插入图片描述

2.2 修改属性文件

spring.application.name=config-client
server.port=9087

# 设置服务注册中心的地址
eureka.client.service-url.defaultZone=http://bb:1234@192.168.134.129:8761/eureka/,http://bb:1234@192.168.134.131:8761/eureka/

# 开启配置中心
spring.cloud.config.discovery.enabled=true
# 对应的eureka 中的配置中心 serviceId 默认是 config-server
spring.cloud.config.discovery.service-id=config-server

# 指定环境
spring.cloud.config.profile=dev
# git 标签
spring.cloud.config.label=master

# SpringBoot 默认开启了权限拦截,会导致 /refresh 出现401拒绝访问
management.security.enabled=false

# 在Actuator中默认只放开了 info 和health 如果要放开所有*
management.endpoints.web.exposure.include=*
# 放开shutdown接口
management.endpoints.enabled-by-default=true

2.3 修改Bean对象作用域

在这里插入图片描述

2.4 测试

因为refresh接口只支持post方式提交,我们通过postman来测试
在这里插入图片描述
然后修改码云中的数据
在这里插入图片描述
然后通过postman来发送post请求
在这里插入图片描述
重新访问请求,发现数据更新了
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值