天天看點

Spring-Cloud-Config

一、簡介

  在分布式系統中,由于提供服務的成功非常多,為了使服務的配置檔案能夠統一管理,并且能夠實時的更新,為了解決這個問題,可以提供一個服務來專門提供其他服務啟動時所需的配置資訊。而spring cloud config就有這樣的功能,它分為config server和config client。

  接下來準備使用gradle來做一個簡單的demo,以備查詢。

  包含兩個子項目,一個是config server,另外一個時config config。

二、配置

2.1 建立父項目

build.gradle檔案

version = '0.0.1'
description = 'a demo parent project'

buildscript {
    repositories {
        mavenLocal()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        maven { url "http://repo.spring.io/libs-release" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE")
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin:'io.spring.dependency-management'

    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8

    dependencyManagement {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-config:2.0.1.RELEASE'
        }
    }

    repositories {
        mavenLocal()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    }

    dependencies {
        testCompile 'junit:junit:4.12'
    }
}
           

2.2 建立Spring Cloud Config Server項目

  1. build.gradle檔案
group 'com.spring-cloud-config-server'
dependencies {
    compile 'org.springframework.cloud:spring-cloud-config-server'
}
           
  1. bootstrap.yml檔案
server:
  port: 

spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      name: config-server
      profile: dev
      server:
        native:
          search-locations: classpath:config
           
  1. config/application-dev.properties檔案
username=jack
age=
           
  1. 啟動檔案ConfigServer.java
package demo;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer.class, args);
    }
}
           
  1. 啟動程式後可以在浏覽器檢視到配置檔案的資訊:

    http://localhost:8080/application/dev

{"name":"application","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:config/env/application-dev.properties","source":{"username":"jack","age":"22"}}]}
           

2.2 建立Spring Cloud Config Client項目

  1. build.gradle檔案
group 'com.spring-cloud-config-client'
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-config'
}
           
  1. bootstrap.yml檔案
server:
  port: 

spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080
           
  1. 啟動檔案ConfigClient.java
package demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ConfigClient {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient.class, args);
    }

    @Value("${username}")
    private String name;

    @GetMapping("/hello")
    public String hello() {
        return "hello " + this.name + ", welcome";
    }

    @GetMapping("/")
    public String index() {
        return "hello cloud config client";
    }
}
           
  1. 啟動程式後可以在浏覽器檢視到配置檔案的資訊:

    http://localhost:8081/hello

hello jack, welcome
           

繼續閱讀