1. 程式人生 > >Spring Cloud實踐之集中配置Spring-config

Spring Cloud實踐之集中配置Spring-config

serve 啟動 gap rop project 自己的 HA sna tst

將一個系統中各個應用的配置文件集中起來,方便管理。

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

Spring Cloud實踐之集中配置Spring-config