SpringCloud:分布式配置中心——Spring Cloud Config

一,Spring Cloud Config

    Spring Cloud Config是Spring Cloud体系内部提供的分布式配置中心,用来进行配置信息统一处理。分布式配置中心整体流程如下图,用户推送配置信息到GIT/SVN版本控制中心,创建Config Server服务专门从GIT/SVN上获取配置文件到缓存中,Config Client客户端从Config Server读取配置信息进行项目信息配置;当GIT/SVN上配置文件发生变化时,可以在客户端通过Actuator进行手动刷新或者通过Spring Cloud Bus消息总线进行自动刷新处理(本文演示通过Actuator手动刷新处理)

二,在GIT上创建配置中心仓库,并添加配置信息,如下图;此处需要注意,文件前缀必须与客户端服务的ApplicationName一致

三,整体服务架构

    * 使用Eureka注册中心进行服务注册,Eureka服务搭建参考之前的博文

四,Config Server端服务搭建

         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>

    1,配置文件 -- application.yml

###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8000/eureka
spring:
  application:
    ####注册中心应用名称
    name: server-config
  cloud:
    config:
      server:
        git:
          ###git环境地址
          uri: https://github.com/zpj0427/spring-cloud-config.git
          ####搜索目录
          search-paths:
            -
      ####读取分支
      label: master
####端口号
server:
  port: 8100

    2,启动类

package com.gupao;

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
@EnableEurekaClient
// 开启分布式配置服务
@EnableConfigServer
public class SpringCloudConfigServerApp {
    public static void main( String[] args ) {
        SpringApplication.run(SpringCloudConfigServerApp.class, args);
    }
}

    3,访问路径:http://localhost:8100/member-config-dev.properties

五,Config Client端搭建

        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>
    <!-- acturator服务监控 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</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>

    1,注意:Config Client端服务名称必须为member-config,与配置文件前缀一致

    2,配置文件 -- bootstrap.yml(此处application.yml调用不通)

spring:
  application:
    ####注册中心应用名称
    name: member-config
  cloud:
    config:
    ####读取后缀
      profile: dev
      ####读取config-server注册地址
      discovery:
        service-id: server-config
        enabled: true
#####    eureka服务注册地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8000/eureka
server:
  port: 8300
### 启动actuator所有监控节点
management:
  endpoints:
    web:
      exposure:
        include: "*"

    3,配置文件信息获取

package com.gupao.config.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pj_zhang
 * @create 2019-01-23 21:46
 **/
@RestController
// 通过调用接口刷新配置中心配置文件,需要添加该注解进行识别
@RefreshScope
public class ConfigController {

    @Value("${spring-cloud-member-config}")
    private String configValue;

    @PostMapping("/getConfig")
    public String getConfig() {
        return configValue;
    }

}

    4,服务启动类

package com.gupao;

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

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

    5,服务访问

六,配置信息更改

    1,从git端修改配置信息

    2,直接从服务端读取文件,如下图,文件信息修改成功

    3,先从客户端读取文件,如下图,配置信息未发生变更

    4,调用Actuator提供的/actuator/fresh接口刷新信息;如下图,刷新完成后,存在文件信息变更

    5,重新读取配置新,如下图,配置信息已经更新

多环境变更不演示了,就这样吧,太TM卡了。。。

猜你喜欢

转载自blog.csdn.net/u011976388/article/details/86618833