JavaEE进阶知识学习

Wesley13
• 阅读 628

SpringCloudConfig配置中心

概述

就前面项目而言,分布面临的问题是配置问题,每一个项目都有一个yml文件,不好运维管理,所有需要一套集中式,动态的配置管理设施,SpringCloud提供了ConfigServer来解决这个问题。

SpringCloud Config是为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为 各个不同的微服务应用的环境提供了一个 中心化的外部配置。SpringCloud Config分为客户端和服务端,服务端也称 分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密和解密信息等访问接口,客户端是通过指定的配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具管理和访问配置内容。

作用

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

config服务端与GitHub通信

GitHUb上新建一个microservicecloud-config的Repository

本地硬盘目录新建git仓库并clone

在D:\workspace2018\micorservicecloude-config\microservicecloud-config新建application.yml文件

Spring:
    profiles:
    active:
    - dev
---
Spring:
    profiles: dev 
    application:
        name: micorservicecloud-config-luo-dev
---
Spring:
    profiles: test 
    application:
        name: micorservicecloud-config-luo-test

注意保存为utf-8的文件格式

将yml文件推送到GitHub上

git add .
git commit -m""
git push origin master

新建项目microservicecloud-config-3344

POM.xml文件

<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>com.luo.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-3344</artifactId>
<dependencies>
        <!-- springCloud Config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>4.10.0.201712302008-r</version>
        </dependency>
        <!-- 图形化监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 熔断 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 热部署插件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

application.yml文件

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:luokangyuan/microservicecloud-config.git #GitHub上面的git仓库名字

主启动类

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

修改host文件

127.0.0.1 config-3344.com

测试通过config微服务从GitHub上获取配置内容

启动服务3344,访问http://config-3344.com:3344/application-dev.yml,http://config-3344.com:3344/application-test.yml

config客户端获取github配置

本地新建microservicecloud-config-client.yml文件,并推送到github

server:
    port: 8201
spring:
    profiles: dev
    application:
        name: microservicecloud-config-client
eureka:
    client:
        service-url:
            defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
    port: 8202
spring:
    profiles: test
    application:
        name: microservicecloud-config-client
eureka:
    client:
        service-url:
            defaultZone: http://eureka-test.com:7001/eureka/

新建项目microservicecloud-config-client-3355,pom.xml文件如下

<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>com.luo.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>microservicecloud-config-client-3355</artifactId>

    <dependencies>
        <!-- SpringCloud Config客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

新建bootstrap.yml文件

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
      profile: test   #本次访问的配置项
      label: master   
      uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

application.yml是用户级的资源配置文件,bootstrap.yml是系统级,优先级更高,保证不会被本地配置文件所覆盖

修改host文件,增加映射

127.0.0.1 client-config.com

新建测试controller,从github读取配置信息

package com.luo.springcloud.rest;

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

@RestController
public class ConfigClientRest
{

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

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

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

    @RequestMapping("/config")
    public String getConfig()
    {
        String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
        System.out.println("******str: " + str);
        return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
    }
}

新建主启动类

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

测试

启动3344服务,启动3355服务,bootstrap.yml中的profile值是什么,决定从github上读取什么,ruguo

访问http://client-config.com:8201/config得到是github上的microservicecloud-config-client.yml文件中dev相关的配置信息

访问http://client-config.com:8202/config得到是github上的microservicecloud-config-client.yml文件中test相关的配置信息

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Spring Cloud套件
SpringCloud提供的服务  配置管理服务注册服务发现断路器负载均衡智能路由服务间调用一次性令牌微代理思维导图模板全局锁控制总线分布式式会话集群状态领导选举分布式消息  子项目功能说明SpringCloudConfig配置中心,利用git来集
Easter79 Easter79
2年前
SpringBoot项目使用配置中心Nacos
阅读文本大概需要3分钟。目前市面上用的比较多的配置中心有Disconf:2014年7月百度开源的配置管理中心,同样具备配置的管理能力,不过目前已经不维护了,最近的一次提交是两年前了。SpringCloudConfig:2014年9月开源,SpringCloud生态组件,可以和SpringCloud
Easter79 Easter79
2年前
SpringCloud微服务(06):Config组件,实现配置统一管理
一、Config简介在微服务系统中,服务较多,相同的配置:如数据库信息、缓存、参数等,会出现在不同的服务上,如果一个配置发生变化,需要修改很多的服务配置。springcloud提供配置中心,来解决这个场景问题。系统中的通用配置存储在相同的地址:GitHub,Gitee,本地配置服务等,然后配置中心读取配置以restful发布
Stella981 Stella981
2年前
Spring Cloud Alibaba Nacos 服务配置中心和注册中心
学习在SpringCloud中使用Nacos实现服务配置中心和注册中心,类似SpringCloudConfig和SpringCloudNetflixEureka提供的功能。1概述SpringCloudAlibaba是阿里巴巴提供的一套微服务开发一站式解决方案。主要提供的功能:分布式配置中心
Stella981 Stella981
2年前
SpringBoot项目使用配置中心Nacos
阅读文本大概需要3分钟。目前市面上用的比较多的配置中心有Disconf:2014年7月百度开源的配置管理中心,同样具备配置的管理能力,不过目前已经不维护了,最近的一次提交是两年前了。SpringCloudConfig:2014年9月开源,SpringCloud生态组件,可以和SpringCloud
Easter79 Easter79
2年前
SpringCloud配置中心——ConfigServer搭建
_在实际应用中,一个系统的每个微服务都会有相同的配置,如数据库配置信息等等。为了将每个微服务的公共配置可以抽取出来。SpringCloud提供了Config配置中心的配置,作为配置中心(ConfigServer),提供给微服务(ConfigClient)读取并且加载配置。使用git仓库存放配置文件,SpringCloudConfig读取到之后会自
Easter79 Easter79
2年前
SpringCloud 进阶之分布式配置中心(SpringCloud Config)
1\.SpringCloudConfigSpringCLoudConfig为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置;SpringCloudConfig分为服务端和客户端:服务端也称为分布式配置中心,它是一个独立的微服务应用,
Easter79 Easter79
2年前
SpringCloud学习笔记(七)之SpringCloud Config分布式配置中心
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自带一个application.yml。上百个配置管理。是什么
Easter79 Easter79
2年前
SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客...
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅。1、概念:SpringCloudConfig基础配置2、具体内容通过名词就可以发现,SpringCloudConfig核心作用一定就在于进行配置文件的管理上。也就是说为了更好的进行所有微服务的配置项的管理,在SpringCloud设计架构里面就考虑
linbojue linbojue
1个月前
SpringCloud原理解析与实战技巧
SpringCloud原理详解SpringCloud是一套基于SpringBoot的开源微服务架构构建工具集。它提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话和集群状态)环境中设计、构