SpringCloud的Config分布式配置中心

概述

分布式系统面临的–配置文件问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。spring cloud提供了configServer来解决这个问题,我们每一个微服务自己带着一个application.yml,那上百个的配置文件修改起来,令人头疼!

SpringCloud config分布式配置中心

什么是SpringCloud config分布式配置中心?

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring EnvironmentPropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

image-20210816220908504

spring cloud config 为微服务架构中的微服务提供集中化的外部支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置

spring cloud config 分为服务端客户端两部分。

服务端也称为 分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密,解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理。并且可用通过git客户端工具来方便的管理和访问配置内容。

spring cloud config 分布式配置中心能干嘛?

  • 集中式管理配置文件
  • 不同环境,不同配置,动态化的配置更新,分环境部署,比如 /dev /test /prod /beta /release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启,即可感知到配置的变化,并应用新的配置
  • 将配置信息以REST接口的形式暴露

spring cloud config 分布式配置中心与GitHub整合

由于spring cloud config 默认使用git来存储配置文件 (也有其他方式,比如自持SVN 和本地文件),但是最推荐的还是git ,而且使用的是 http / https 访问的形式。

入门案例

搭建Git环境

首先安装Git客户端,在码云上注册自己的一个账号并进入,并在码云上创建一个项目springcloud-config

image-20210816221338059

然后本地随便一个目录打开git命令界面输入:ssh-keygen -t rsa -C "xxxxx@xxxxx.com" (自己的邮箱)

这样在C:\Users\想办法爱上我.ssh目录下会生成一个私钥一个公钥

image-20210816221616719

然后把公钥(.pub结尾文件里的内容)丢到码云上去:

image-20210816221830774

然后再本地某个目录把码云上的代码通过SSH链接clone下来:

image-20210816222404054

然后我们编写一个application.yml文件并将其提交到码云上:

spring:
  profiles:
  active: dev


---
spring:
  profiles: dev
  application:
   name: springcloud-config-dev
 

---
spring:
  profiles: test
  application:
   name: springcloud-config-test
在项目根目录下打开git bask here 
git add .
git commit -m "first commit"(这个会提示找不到名字和邮箱,使用下面的命令即可)
git config --global user.name "自己的用户名随便"
git config --global user.email "自己的邮箱"
然后再输入:git commit -m "first commit"
git push

image-20210816223459205

springcloud config服务端配置

导入依赖

新建springcloud-config-server-3344模块导入pom.xml依赖

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

修改配置文件

resource下创建application.yml配置文件,Spring Cloud Config服务器从git存储库(必须提供)为远程客户端提供配置:

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server-3344
    # 连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mo18/springcloud-config.git

# 通过config-server,我们可以连接到码云上,访问其中的资源以及配置

主启动类

@SpringBootApplication
@EnableConfigServer //开启配置中心功能
public class Config_Server_3344 {

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

测试

然后我们通过访问http://localhost:3344/application-test.yml这样的方式访问到码云上面的yaml配置信息:

image-20210817092128854

我们还可以通过以下几种方式来访问,其中label指的是码云上的分支:

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

springcloud config客户端配置

GIT上传码云上的配置文件

在本地git仓库springcloud-config文件夹下新建一个config-client.yml(注意编码必须是UTF-8,不然会有意想不到的错误)并提交到码云仓库:

spring:
  profiles:
  active: dev

---

server:
  port: 8201

#springboot的配置

spring:
  profiles: dev
  application:
    name: springcloud-provider-dept


#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      #单机:defaultZone: http://localhost:7001/eureka/
      #集群的话,要注册到集群的每个节点
      defaultZone: http://eureka7001.com:7001/eureka/



---

server:
  port: 8202

#springboot的配置

spring:
  profiles: test
  application:
    name: springcloud-provider-dept


#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      #单机:defaultZone: http://localhost:7001/eureka/
      #集群的话,要注册到集群的每个节点
      defaultZone: http://eureka7001.com:7001/eureka/

image-20210817112745054

新建springcloud-config-client-3355模块

新建一个springcloud-config-client-3355模块,并导入依赖

<dependencies>

    <!--config client依赖-->
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>



    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--Web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

创建配置文件并修改

resources下创建application.yml和bootstrap.yml配置文件,他两有以下的一些特点:

bootstrap.yml主要用于从额外的资源来加载配置信息;

bootstrap.yml要比application.yml先加载;

bootstrap.yml里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。
application.yml是应用级的配置,而bootstrap.yml是系统级别的一些参数配置

bootstrap.yml 是系统级别的配置

# 系统级别的配置

spring:
  cloud:
    config:
      uri: http://localhost:3344
      name: config-client # 需要从git上读取的资源名称,不需要后缀
      profile: dev
      label: master

application.yml 是用户级别的配置

# 用户级别的配置
spring:
  application:
    name: springcloud-config-client-3355

创建controller

创建controller包下的ConfigClientController.java 用于测试

@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/config")
    public String getConfig(){
        return "applicationName:"+applicationName + " eurekaServer:"+ eurekaServer + " port:"+port;
    }

}

主启动类

@SpringBootApplication
public class ConfigClient_3355 {

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

测试

启动服务端Config_server_3344 再启动客户端Config_client_3355

此时我们可以直接访问8201端口,因为启动Config_client_3355客户端的时候它已经通过Config_server_3344服务端在码云上拿到配置的端口信息了!

访问:http://localhost:8201/config/

image-20210817115057879

远程配置实战测试小案例

GIT上传配置文件

本地新建config-dept.yml和config-eureka.yml并提交到码云仓库

image-20210817184921597

image-20210817185003405

config-dept.yml

spring:
  profiles:
    active: dev 



---

server:
  port: 8001

#mybatis配置
mybatis:
  type-aliases-package: com.kuang.springcloud.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml
  config-location: classpath:mybatis/mybatis-config.xml


#springboot的配置

spring:
  profiles: dev 
  application:
    name: springcloud-config-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db03?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456


#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      #单机:defaultZone: http://localhost:7001/eureka/
      #集群的话,要注册到集群的每个节点
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 #修改eureka监控界面对应此服务的实列id
    prefer-ip-address: true #监控界面的status显示ip地址

#info配置:就是用来配置eureka界面对应服务的详细监控信息的:就界面status那里每个服务有个链接点进去  http://xxxx:xxx/actuator/info
info:
  app.name: kuangshen-springcloud
  company.name: blog.kuangstudy.com


---

server:
  port: 8001

#mybatis配置
mybatis:
  type-aliases-package: com.kuang.springcloud.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml
  config-location: classpath:mybatis/mybatis-config.xml


#springboot的配置

spring:
  profiles: test 
  application:
    name: springcloud-config-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db02?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456


#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      #单机:defaultZone: http://localhost:7001/eureka/
      #集群的话,要注册到集群的每个节点
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 #修改eureka监控界面对应此服务的实列id
    prefer-ip-address: true #监控界面的status显示ip地址

#info配置:就是用来配置eureka界面对应服务的详细监控信息的:就界面status那里每个服务有个链接点进去  http://xxxx:xxx/actuator/info
info:
  app.name: kuangshen-springcloud
  company.name: blog.kuangstudy.com

config-eureka.yml

spring:
  profiles:
    active: dev 


---
server:
  port: 7001


spring:
  profiles: dev
  application:
    name: springcloud-config-eureka


#Eureka配置
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registryr如果为false,则表示自己为注册中心,客户端的话为true
    service-url: #表示注册中心的地址,其中http://${eureka.instance.hostname}:${server.port}是一个监控页面
      #单机: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群(我们就只需要关联其他集群的节点):
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

---
server:
  port: 7001


spring:
  profiles: test
  application:
    name: springcloud-config-eureka


#Eureka配置
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registryr如果为false,则表示自己为注册中心,客户端的话为true
    service-url: #表示注册中心的地址,其中http://${eureka.instance.hostname}:${server.port}是一个监控页面
      #单机: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群(我们就只需要关联其他集群的节点):
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

新建springcloud-config-eureka-7001模块

新建springcloud-config-eureka-7001模块,并将原来的springcloud-eureka-7001模块下的内容拷贝的该模块。

1.清空该模块的application.yml配置,并新建bootstrap.yml连接远程配置

spring:
  cloud:
    config:
      name: config-eureka
      label: master
      profile: dev
      uri: http://localhost:3344

2.在pom.xml中添加spring cloud config依赖

<!--config-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

3.主启动类

@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 表示是一个注册中心服务端:可以接收别人注册进来
public class EurekaServer_7001 {

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

4.测试

第一步:启动 Config_Server_3344,并访问 http://localhost:3344/master/config-eureka-dev.yml 测试

image-20210817185703922

第二步:启动ConfigEurekaServer_7001,访问 http://localhost:7001/ 测试

image-20210817185922968

显示上图则成功

新建springcloud-config-dept-8001模块

新建springcloud-config-dept-8001模块并拷贝springcloud-provider-dept-8001的内容

同理导入spring cloud config依赖、清空application.yml 、新建bootstrap.yml配置文件并配置

spring:
  cloud:
    config:
      name: config-dept
      label: master
      profile: dev
      uri: http://localhost:3344

其他内容基本都是复制springcloud-provider-dept-8001的内容不变就行了

测试

我们去访问http://localhost:8001/dept/get/1,就能访问到码云上配置的数据库db03的内容了

image-20210817190137869

image-20210817190458503

总结:以上案例,我们只需要修改码云上的配置,但是需要重启客户端服务才能接收最新的配置,因此体验不是太好,最近接触一款配置中心携程的阿波罗(appolo)感觉很不错,远程改了配置,不用重启客户端就能生效,虽然听说nacos也比较好~。

代码在码云地址:https://gitee.com/mo18/springcloud.git

posted @ 2021-08-17 19:51  一万年太久只争朝夕  阅读(177)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css