一、简介
在分布式系统中,由于提供服务的成功非常多,为了使服务的配置文件能够统一管理,并且能够实时的更新,为了解决这个问题,可以提供一个服务来专门提供其他服务启动时所需的配置信息。而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项目
- build.gradle文件
group 'com.spring-cloud-config-server'
dependencies {
compile 'org.springframework.cloud:spring-cloud-config-server'
}
- bootstrap.yml文件
server:
port: 8080
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
name: config-server
profile: dev
server:
native:
search-locations: classpath:config
- config/application-dev.properties文件
username=jack
age=22
- 启动文件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);
}
}
- 启动程序后可以在浏览器查看到配置文件的信息:
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项目
- 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'
}
- bootstrap.yml文件
server:
port: 8081
spring:
application:
name: config-client
cloud:
config:
profile: dev
uri: http://localhost:8080
- 启动文件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";
}
}
- 启动程序后可以在浏览器查看到配置文件的信息:
http://localhost:8081/hello
hello jack, welcome