SpringCloud→SpringCloud Config 分布式配置文件中心(四)

SpringCloud Config 分布式配置文件中心笔记

1. 分布式配置中心产生的背景

在微服务如果使用传统的方式管理配置文件,配置文件管理起来会非常负责如果生产环境配置文件,可能需要发生改变的时候,传统方式会重新打war包,重新读取配置信息在JVM内存

2. 什么是分布式配置中心

在微服务当中使用同一个服务器管理所有服务配置文件信息,能够实现后台可管理,当服务器正在运行的时侯,
如果配置文件需要发生改变,可以实现不需要重启服务器实时更改配置文件信息

3. SpringCloud怎么理解

SpringCloud把微服务所有的解决都做了整合(全家桶)

  • 分布式配置中心
  • 分布式锁
  • 分布式任务
  • 调度平台
  • 分布式事务
  • 分布式日志收集

4. 分布式配置中心框架有哪些

阿波罗 → 携程写的分布式配置中心有图形界面可管理配置文件信息配置文件信息存放在数据库里面
SpringCloud Config → 没有后合可管理分布式配置中心配置文件信息存放在版本控制器里面(git[svn])
使用Zookeeper→ 实现分布式配置中心 持久节点信息+事件通知

热部署其实底层还是会重启服务器不适合于生产环境只适合于本地开发测试

5. 分布式配置中心需要哪些组件

  1. web管理系统-——后台可以使用图形界面管理配置文件SpringCloud Config没有图形化管理配置文件
  2. 存放分布式配置文件服务器(持久存储服务器)—使用版本控制器存放配置文件信息使用git环境
  3. ConfigServer缓存配置文件服务器(临时缓存存放)
  4. ConfigClient读取ConfigServer配置文件信息

6. SpringCloud Config分布式配置中心原理

[外链图片转存失败(img-6PM37flp-1568775721508)(images/1568708489155.png)]
在这里插入图片描述

7. 搭建git环境的目的

持久化存储配置文件信息 采用码云

8. git环境上的文件夹要以什么区分

以大分类 项目进行区分

9. 公司项目中环境是如何区分的

  • dev — 开发环境

  • sit — 测试环境

  • pre — 预生产环境

  • prd — 准生产环境

10. 在git环境上创建配置文件命名规范

服务名称-环境.文件类型

如: member-dev.properties

11. SpringCloud Config分布式配置中心搭建步骤

1. git上相关操作

  1. 在gitee(码云)上创建项目 我的为mayi_config

  2. 码云上新建文件夹 mayiconfig 注意该文件夹取名正常该为项目的名称

  3. 文件夹内新增两个配置文件分别为

    test-configClient-prd.properties

    itmayieduinfo=prd.itmayiedu.com
    

    test-configClient-prd.properties

    itmayieduinfo=prd.itmayiedu.com
    

    最终结构为:

    [外链图片转存失败(img-vJlFpMDz-1568775721509)(images/1568712980517.png)]

在这里插入图片描述

2. 服务端相关实现

  1. 创建项目名称为: springboot2.0-config_server

  2. 添加maven依赖 如下

    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.1.RELEASE</version>
    	</parent>
    	<!-- 管理依赖 -->
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>Finchley.M7</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    	<dependencies>
    		<!--spring-cloud 整合 config-server -->
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-config-server</artifactId>
    		</dependency>
    		<!-- SpringBoot整合eureka客户端 -->
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    		</dependency>
    
    	</dependencies>
    	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
    	<repositories>
    		<repository>
    			<id>spring-milestones</id>
    			<name>Spring Milestones</name>
    			<url>https://repo.spring.io/libs-milestone</url>
    			<snapshots>
    				<enabled>false</enabled>
    			</snapshots>
    		</repository>
    	</repositories>
    
  3. application.yml配置

    ###服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8100/eureka
    spring:
      application:
        ####注册应用名称
        name: config-server
      cloud:
        config:
          server:
            git:
              ### config-server读取git环境地址
              uri: https://gitee.com/bd2star_zxh/mayi_config.git
              ####搜索目录
              search-paths:
                - mayiconfig
          ####读取分支
          label: master
    ####端口号
    server:
      port: 8888
    
  4. 创建SpringBoot启动类

    package com.itmayiedu;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    
    // @EnableConfigServer表示开启 configServer服务器端
    @SpringBootApplication
    @EnableEurekaClient
    @EnableConfigServer
    public class AppConfigServer {
        public static void main(String[] args) {
            SpringApplication.run(AppConfigServer.class, args);
        }
    }
    
    
  5. 启动项目(先启动Eureka注册中心、再启动该项目)

  6. 查看Eureka中服务是否注册成功 如下

    [外链图片转存失败(img-7jGmQZ46-1568775721509)(images/1568713252043.png)]

在这里插入图片描述

  1. 访问git上的配置文件 路径为

    http://localhost:8888/test-configClient-prd.properties

    http://localhost:8888/test-configClient-prd.properties

  2. 返回信息如下

[外链图片转存失败(img-6qOiPxvj-1568775721509)(images/1568713322937.png)]

[外链图片转存失败(img-dXN5knYm-1568775721509)(images/1568713342597.png)]
在这里插入图片描述
在这里插入图片描述

3. 客户端实现

  1. 引入maven依赖信息

    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.1.RELEASE</version>
    	</parent>
    	<!-- 管理依赖 -->
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>Finchley.M7</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    
    		</dependencies>
    	</dependencyManagement>
    	<dependencies>
    		<!-- SpringBoot整合Web组件 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-config-client</artifactId>
    		</dependency>
    		<!-- SpringBoot整合eureka客户端 -->
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    		</dependency>
    	</dependencies>
    	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
    	<repositories>
    		<repository>
    			<id>spring-milestones</id>
    			<name>Spring Milestones</name>
    			<url>https://repo.spring.io/libs-milestone</url>
    			<snapshots>
    				<enabled>false</enabled>
    			</snapshots>
    		</repository>
    	</repositories>
    
  2. 新建bootstrap.yml

    spring:
      application:
        ####注册中心应用名称 (不能乱写 要与git上配置文件名字一致)
        name:  test-configClient
      cloud:
        config:
          ####读取版本环境
          profile: prd
          ####读取config-server注册地址  直接写configServer注册的应用服务名即可
          discovery:
            service-id: config-server
            ### 开启读取权限
            enabled: true
    #####eureka服务注册地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8100/eureka
    ### 服务端口号
    server:
      port: 8882
    ### 开启所有端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    

    注意 应用名称必须与git上的文件名字保持一致 不能乱写 git上文件名如下

    [外链图片转存失败(img-Wy5GLbSd-1568775721510)(images/1568774685753.png)]
    在这里插入图片描述

  3. 创建 TestController.java

    package com.itmayiedu.api.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope
    public class TestController {
    
        @Value("${itmayieduinfo}")
        private String itmayieduinfo;
    
    
        @RequestMapping("/getItmayieduinfo")
        public String getItmayieduinfo() {
            return itmayieduinfo;
        }
    }
    
    
  4. 创建 SpringBoot启动类

    package com.itmayiedu;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class AppConfigClient {
        public static void main(String[] args) {
            SpringApplication.run(AppConfigClient.class, args);
        }
    }
    
    
  5. 启动成功后访问地址如下:

    http://localhost:8882/getItmayieduinfo

    [外链图片转存失败(img-hjIMp03z-1568775721510)(images/1568774806979.png)]x

在这里插入图片描述

12. SpringCloud分布式配置中心刷新

默认情况下不能及时的获取实时变更的配置文件信息

  1. 手动刷新 — 需要人工调用接口 采用actuator端点刷新数据 读取最新配置文件(通过监控中心)
  2. 自动刷新 — 消息总线进行实时通知 —SpringCloud Bus消息总线

手动刷新和自动刷新都不需要重启服务器

在公司当中,不建议大家使用自动刷新功能,因为对性能不是很好,建议在每次修改完配置文件之后 人工调用接口

http://localhost:8882/actuator/refresh进行刷新

actuator端点刷新数据[手动刷新实现]

  1. 添加maven依赖 注意是在 config-client模块中

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  2. bootstrap.yml中新增 开启监控端点

    ### 开启所有端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
  3. 在需要刷新的Bean上添加@RefreshScope注解。

    package com.itmayiedu.api.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    // 当配置更改时,标有@RefreshScope的Bean将得到特殊处理来生效配置
    @RestController
    @RefreshScope
    public class TestController {
    
        @Value("${itmayieduinfo}")
        private String itmayieduinfo;
    
    
        @RequestMapping("/getItmayieduinfo")
        public String getItmayieduinfo() {
            return itmayieduinfo;
        }
    }
    
  4. 手动刷新接口(Post请求手动刷新)

    http://127.0.0.1:8882/actuator/refresh 启动刷新器 从cofnig server读取

[外链图片转存失败(img-vRlu0Vvx-1568775721511)(images/1568775125536.png)]
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iBaoxing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值