SpringCloud(6)SpringCloud Config 分布式配置中心

一 简介

面临的问题

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

介绍

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

SpringCloud Config分为服务端和客户端两部分。

在这里插入图片描述

  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

二 服务端demo

配置文件部分

  1. 用自己的GitHub账号在GitHub上新建一个名为microservicecloud-config的新Repository(这里为了逼真,用了private的仓库)
    在这里插入图片描述
  2. 本地检出
    在这里插入图片描述
  3. 创建一个目录app1,然后里面新建一个application.yml(保存格式必须为UTF-8)
server: 
  port: 8201 
spring:
  profiles: dev
  application: 
    name: microservicecloud-config-client
testmsg: devmsback
---
server: 
  port: 8202 
spring:
  profiles: test
  application: 
    name: microservicecloud-config-client
testmsg: testmsg
#  请保存为UTF-8格式
  1. 提交并推送修改到github

创建服务端项目

  1. 新建Module模块microservicecloud-config-3344
  2. 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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>com.zyc.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-config-3344</artifactId>

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

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


</project>
  1. yml配置
server:
  port: 3344

spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zycCome/microservicecloud-config.git
          search-paths: app1 # 目录名
          username: xxx
          password: xxx
      label: master
eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: microservicecloud-config-1
    prefer-ip-address: true     #访问路径可以显示IP地址
    hostname: config1.com
  1. 启动类
package com.zyc.springcloud;

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;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class Config_3344_StartSpringCloudApp
{
    public static void main(String[] args)
    {
        SpringApplication.run(Config_3344_StartSpringCloudApp.class,args);
    }
}

  1. 测试效果:1.http://localhost:3344/application-dev.yml 2.http://localhost:3344/application-test.yml
    在这里插入图片描述

小结:成功实现了用SpringCloud Config通过GitHub获取配置信息

配置读取规则

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

三 客户端demo

步骤

  1. 新建项目microservicecloud-config-client-3355
  2. 添加bootstrap.yml文件(说明在下一节)
spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
      profile: dev   #本次访问的配置项
      label: master
#      uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
      discovery:
        enabled: true
        service-id: microservicecloud-config #config服务端的应用名
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

# 动态刷新配置 ---需要忽略权限拦截
management:
  security:
    enabled: false

  1. 编写controller,测试动态获取配置信息效果
package com.zyc.springcloud.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
@RestController
class MessageRestController {

    @Value("${testmsg: defaultmsg}")
    private String message;

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
}


  1. 启动类
package com.zyc.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClient_3355_StartSpringCloudApp
{
    public static void main(String[] args)
    {
        SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class,args);
    }
}



测试

  1. 访问接口地址http://localhost:8201/message,查看返回值
  2. 修改github上的配置文件中的值
  3. 刷新配置,通过post请求refresh地址
    在这里插入图片描述
  4. 再次请求http://localhost:8201/message。会发现返回值已经变了

bootstrap.yml

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的EnvironmentBootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。 Bootstrap contextApplication Context有着不同的约定,
所以新增了一个bootstrap.yml文件,保证Bootstrap ContextApplication Context配置的分离。

参考

发布了107 篇原创文章 · 获赞 1 · 访问量 3953

猜你喜欢

转载自blog.csdn.net/m0_38060977/article/details/104109750