Spring Cloud实践之集中配置Spring-config

时间:2022-05-11 16:43:44

    将一个系统中各个应用的配置文件集中起来,方便管理。

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

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

如上,对一个spring boot应用加上@EnableConfigServer就可以将其变成一个集中配置服务。

build.gradle文件:

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
    maven { url  "http://dl.bintray.com/oembedler/maven" }
    maven { url "https://repo.spring.io/libs-release" }
    maven { url "http://10.100.122.249:8881/nexus/content/groups/public/" }
}

dependencyManagement {
  imports {
    //mavenBom 'org.springframework.cloud:spring-cloud-netflix:1.2.0.M1'
    mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
  }
}

dependencies {
    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
    // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server
    compile("org.springframework.cloud:spring-cloud-starter-parent:Camden.SR2")
    compile("org.springframework.boot:spring-boot-starter-security")
      compile("org.springframework.cloud:spring-cloud-config-server")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-actuator")
}

config server自己的配置文件如下:

spring:
  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/shared
  profiles:
     active: native

server:
  port: 8888

security:
  user:
    password: root
native代表将各个接入应用的配置文件放在config server的本地目录,这里放在classpath:/shared ;并对接入应用开启验证,用户名user密码root; 
启动config server,验证http://localhost:8888/demo/application.property (这里假设在shared目录放入了demo应用的配置文件demo.property)

再来看看接入应用如何使用这个config server
在demo应用中:
//在dependencies中加入
compile("org.springframework.cloud:spring-cloud-starter-parent:Camden.SR2")
compile("org.springframework.cloud:spring-cloud-starter-config") 
//在build.gradle根下加入如下依赖项
dependencyManagement {
imports {
  mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
  }
}

 

* 将项目resource下application.properties文件复制到server-config resource下的shared文件夹中
并重命名为项目名.properties,如demo项目,命名为demo.properties
* 清空application.properties内容
3. 在resource下新建bootstrap.yml文件(以demo项目举例)

spring:
  application:
    name: demo
  profiles:
    active: default
  cloud:
    config:
      uri: http://localhost:8888/
      name: demo
      fail-fast: true
      password: root
      username: user