一、简介
在上一篇的文章中已经实现了Spring Cloud 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.boot:spring-boot-dependencies:2.0.4.RELEASE'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR1'
}
}
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/env
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.boot:spring-boot-starter-actuator'
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
新添加application.yml文件
management:
endpoint:
refresh:
enabled: true
endpoints:
web:
exposure:
include:
- info
- health
- refresh
启动文件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
修改配置文件application-dev.properties后,发送一个post请求: http://localhost:8081/actuator/refresh, 然后再查看:http://localhost:8081/hello
hello tom, welcome