Spring Cloud Config可以用来作分布式配置中心,本质上ConfigServer会从git / svn / 本地目录上下载到本地环境,EnvironmentController提供了一些了接口给ConfigClient启动时调用,EnvironmentController会返回本地缓存的配置文件给ConfigClient,从而实现分布式配置

 

首先我们需要创建一个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.hlab.gongura</groupId>
    <artifactId>gongura</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>gongura</name>
    <description>Config Server</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
        <springfox-version>2.6.1</springfox-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
      
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

application.yml配置

server:
  port: 8888


spring:

#config server 配置 ,从git获取配置
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/**********
#          username: *******   如果是私有项目需要填写
#          password: *******    如果是私有项目需要填写

uri填写git地址,这里的配置是从git获取的,也可以使用本地文件填写配置

 

Application主类代码,主要是@EnableConfigServer

package com.hlab.gongura;

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;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

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

配置完成后试下访问看看得到的结果

{
	"name": "application-dev",
	"profiles": ["default"],
	"label": null,
	"version": null,
	"state": null,
	"propertySources": [{
		"name": "https://gitee.com/CheungQ/spring-cloud-config-repository/application-dev.yml",
		"source": {
			"demo.title": "new 新内容 (yml 11文件)"
		}
	}]
}

远程git中的配置文件application-prod.yml中的内容为

demo:
  title: "prod env title"

至此,server端配置完成,接下来我们要配置的是client端,client端其实就是一个普通的工程,在resources目录下创建一个新的配置文件bootstrap.yml,注意不是application.yml,内容如下

spring:
  application:
    name:  orchid-client
  cloud:
    config:
      uri: http://localhost:8888
      name: application
      profile: prod

 

接下来就可以直接用@value注解获取配置了,另外有个tips,本地配置中最好加下对应的配置,比如我在application.yml中加了

demo:
  title: "本地"

 

否则会报错

 

文档:https://springcloud.cc/spring-cloud-config-zhcn.html#true-encryption-and-decryption