Spring Cloud微服务架构(十)分布式配置中心(spring cloud config)服务端 config server

1、spring cloud config 简介

在分布式系统中,每一个功能模块都能拆分成一个独立的服务,一次请求的完成,可能会调用很多个服务协调来完成,为了方便服务配置文件统一管理,更易于部署、维护,所以就需要分布式配置中心组件了,在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置文件放在在配置服务的内存中,也支持放在远程Git仓库里。引入spring cloud config后,我们的外部配置文件就可以集中放置在一个git仓库里,再新建一个config server,用来管理所有的配置文件,维护的时候需要更改配置时,只需要在本地更改后,推送到远程仓库,所有的服务实例都可以通过config server来获取配置文件,这时每个服务实例就相当于配置服务的客户端config client,为了保证系统的稳定,配置服务端config server可以进行集群部署,即使某一个实例,因为某种原因不能提供服务,也还有其他的实例保证服务的继续进行。

2、架构原理图

Spring Cloud微服务架构(十)分布式配置中心(spring cloud config)服务端 config server

3、构建Config Server

Spring Cloud微服务架构(十)分布式配置中心(spring cloud config)服务端 config server

创建完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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vesus</groupId>
    <artifactId>springcloud-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springcloud-config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.vesus</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- eureka:微服务注册 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- config server :-->
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、创建application.yml配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 注册中心地址
spring:
  application:
    name: springcloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/vesus198/spring-cloud-config #配置git仓库地址
          search-paths: config-repos #配置仓库路径
          username: #访问git仓库的用户名
          password: #访问git仓库的用户密码
      label: master #配置仓库的分支
server:
  port: 8770

4、入口方法加入@EnableConfigServer,增加分布式配置支持。

package com.vesus.springcloudconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringcloudConfigServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringcloudConfigServerApplication.class, args);
    }
}

5、访问 http://localhost:8770/dev/springcloud-config-server ,访问文件springcloud-config-server-dev.properties

{"name":"dev","profiles":["springcloud-config-server"],"label":null,"version":"21facfcc422e519104b102a6e5db369b3166cf18","state":null,"propertySources":[]}

http请求地址和资源文件映射如下:

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

源码:https://gitee.com/vesus198/springcloud-demo/tree/master/springcloud-config-server