spring cloud config server接入github

首先,github的内容如下:

地址:https://github.com/stringhuang/SpringCloud-Learning

spring cloud config server接入github_第1张图片

spring_cloud_in_action/config-repo 存储文件为:didispace-xxx.properties

spring cloud config server接入github_第2张图片

里面的内容分别为:

spring cloud config server接入github_第3张图片

commitId为:c9a668d1cf75d7bd5c27f0884214c61b8e0f5c6a

spring cloud config server接入github_第4张图片

接着,回到springboot, 创建新项目config-server,引入spring-cloud-config-server和dependencyManagement:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.7.RELEASE
		 
	
	com.sc
	config-server
	0.0.1-SNAPSHOT
	config-server
	Demo project for Spring Boot of Spring Cloud Config Server

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		


		
			org.springframework.cloud
			spring-cloud-config-server
			2.0.2.RELEASE
		

	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.SR2
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


application.yml定义为:注意,uri和search-paths和上面的github设置一一对应。

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/stringhuang/SpringCloud-Learning.git
          username: ********
          password: ******
          search-paths: spring_cloud_in_action/config-repo
server:
  port: 7001

运行之后,curl http://localhost:7001/didispace/prod/master/ 和 curl http://localhost:7001/didispace-prod.properties:

RdeMacBook-Pro:gitRepository r$ curl http://localhost:7001/didispace/prod/master/
{"name":"didispace","profiles":["prod"],"label":"master","version":"c9a668d1cf75d7bd5c27f0884214c61b8e0f5c6a","state":null,"propertySources":[{"name":"https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace-prod.properties","source":{"from":"git-prod-1.0"}},{"name":"https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace.properties","source":{"from":"git-default-1.0"}}]}
RdeMacBook-Pro:gitRepository r$ 
RdeMacBook-Pro:gitRepository r$ curl http://localhost:7001/didispace-prod.properties
from: git-prod-1.0
RdeMacBook-Pro:gitRepository r$ 
{
	"name": "didispace",
	"profiles": ["prod"],
	"label": "master",
	"version": "c9a668d1cf75d7bd5c27f0884214c61b8e0f5c6a",
	"state": null,
	"propertySources": [{
		"name": "https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace-prod.properties",
		"source": {
			"from": "git-prod-1.0"
		}
	}, {
		"name": "https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace.properties",
		"source": {
			"from": "git-default-1.0"
		}
	}]
}

应用层面添加注解@EnableConfigServer:

package com.sc.configserver;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

其中的,version对应git的commitId,label对应git的branch,name对应spring的application概念,profiles对应spring的profiles概念,propertySources对应具体的文件以及内容。具体关联关系如下:

spring cloud config server接入github_第5张图片

另外,日志打印显示本地有缓存配置信息:

下面创建新的分支config-label-test,并且把里面的版本号改为2.0:记得push到github。

spring cloud config server接入github_第6张图片

运行curl http://localhost:7001/didispace/prod/config-label-test/ 和 http://localhost:7001/config-label-test/didispace-prod.properties

RdeMacBook-Pro:gitRepository r$ curl http://localhost:7001/didispace/prod/config-label-test/
{"name":"didispace","profiles":["prod"],"label":"config-label-test","version":"96e64f4e57c93739a5f92fa361830e60a92e620f","state":null,"propertySources":[{"name":"https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace-prod.properties","source":{"from":"git-prod-2.0"}},{"name":"https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace.properties","source":{"from":"git-default-2.0"}}]}
RdeMacBook-Pro:gitRepository r$ 
RdeMacBook-Pro:gitRepository r$ curl http://localhost:7001/config-label-test/didispace-prod.properties
from: git-prod-2.0
RdeMacBook-Pro:gitRepository r$ 
{
	"name": "didispace",
	"profiles": ["prod"],
	"label": "config-label-test",
	"version": "96e64f4e57c93739a5f92fa361830e60a92e620f",
	"state": null,
	"propertySources": [{
		"name": "https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace-prod.properties",
		"source": {
			"from": "git-prod-2.0"
		}
	}, {
		"name": "https://github.com/stringhuang/SpringCloud-Learning.git/spring_cloud_in_action/config-repo/didispace.properties",
		"source": {
			"from": "git-default-2.0"
		}
	}]
}

明显的,获得的内容是分支config-label-test。

你可能感兴趣的:(Spring-boot,spring-cloud,java,github,spring-boot,spring-cloud,java,github)