spring cloud config 使用 git 仓库的配置中心

一、配置服务器

1. pom.xml 配置



	4.0.0

	com.sande
	config-server
	0.0.1-SNAPSHOT
	jar

	config-server
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Finchley.SR1
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-security
		
		
			org.springframework.cloud
			spring-cloud-config-server
		
		
		
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

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



2. src/main/resources/application.properties 

server.port=7001
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://gitee.com/lixiaxin200319/config-repo
spring.cloud.config.server.git.username=lixiaxin200319
spring.cloud.config.server.git.password=密码
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.label=master
#spring.cloud.config.server.git.repos.dev.pattern=dev/*
#spring.cloud.config.server.git.repos.dev=https://gitee.com/lixiaxin200319/config-repo/dev
spring.cloud.config.server.git.basedir=E:\\JAVA\\Spring\\config-server\\src\\main\\resources\\repos

注意:如果使用 spring.cloud.config.server.git.repos.dev=https://gitee.com/lixiaxin200319/config-repo/dev 配置了多个仓库源,会导致指定本地仓库的  spring.cloud.config.server.git.basedir 失效。

 

3. 启动类

package com.sande.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

//@Configuration
//@EnableAutoConfiguration
//@RestController
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

 

4. 代码仓库目录结构

config-repo
  |
  |-----sdcc--sdcc-dev.properties
  |
  |-----dev

spring cloud config 使用 git 仓库的配置中心_第1张图片

二、配置中心客户端配置

1. pom.xml



	4.0.0

	com.sande
	config-client
	0.0.1-SNAPSHOT
	jar

	config-client
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Finchley.SR1
	

	
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-config
		

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

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

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



2. src/maiin/resources/bootstrap.yml

spring: 
  application: 
    name: sdcc
  cloud:
    config:
      uri: http://localhost:7001
      profile: dev
      label: master
      username: user
      password: 123456 
      
server:
  port: 7002
      

注意:上面这些属性必须配置在 bootstrap.yml 或 bootstrap.properties 中,这样 config-server 中的配置信息才能被正确加载。

 

3. 启动类

package com.sande.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ConfigClientApplication {

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

@RefreshScope
@RestController
class MessageRestController {

    @Value("${message:Hello default}")
    private String message;
    
  

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
    
   /* @Value("${info}")
    private String info;
    
  

    @RequestMapping("/info")
    String getInfo() {
        return this.info;
    }*/
    
    @Value("${sdccSellLimit}")
    private String sdccSellLimit;

    @RequestMapping("/sdccSellLimit")
    String getSdccSellLimit() {
        return this.sdccSellLimit;
    }
    
    @Value("${sdccSellLimitNumber}")
    private String sdccSellLimitNumber;

    @RequestMapping("/sdccSellLimitNumber")
    String getSdccSellLimitNumber() {
        return this.sdccSellLimitNumber;
    }
   /* @Value("${name}")
    private String name;

    @RequestMapping("/name")
    String getName() {
        return this.name;
    }
    
    @Value("${id}")
    private String id;

    @RequestMapping("/id")
    String getId() {
        return this.id;
    }*/
    
    /*@Value("${prod_version}")
    private String prod_version;
    
    @RequestMapping("/prod_version")
    String getProd_version() {
        return this.prod_version;
    }*/
    
   
    

}

4. 通过浏览器访问配置中心客户端为相关参数设置的 controller 进行测试

spring cloud config 使用 git 仓库的配置中心_第2张图片

你可能感兴趣的:(spring cloud config 使用 git 仓库的配置中心)