1、环境&版本:
spring.boot.version = 1.5.4.RELEASE
spring.cloud.version = 1.3.1.RELEASE
jdk.version = 1.8
2、服务端搭建(demo-springcloud-config-server):
pom.xml 添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
创建启动类:Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
修改配置文件:application.properties
server.context-path=/
server.port=8888
# 读取本地配置文件
spring.profiles.active=native
# 自动抓取src/main/resources/config/文件夹下的配置
spring.cloud.config.server.native.searchLocations=classpath:/config
在config文件夹下创建config-client-dev.properties 和 config-client-product.properties,内容分别如下:
config-client-dev.properties
env.name=dev
env.password=dev123456
config-client-product.properties
env.name=product
env.password=product123456
启动服务(运行Application.java)
访问http://localhost:8888/config-client/product,结果如下:
{
"name": "config-client",
"profiles": [
"product"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/config/config-client-product.properties",
"source": {
"env.password": "product123456",
"env.name": "product"
}
}
]
}
访问http://localhost:8888/config-client-product.properties,结果如下:
env.name: product
env.password: product123456
以上两种方式通用,都可以被客户端识别;修改config-client-product.properties文件,刷新上面两个url,都可立即看到变化;
配置匹配规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties
其中:
application=config-client
profile=product
lable=""
也可以认为是:
application=config
profile=client-product
lable=""
3、客户端搭建(demo-springcloud-config-client): pom.xml 添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
创建启动类(Application.java):
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建Controller:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class HomeController {
@Value("${env.name:World!}")
private String bar;
@RequestMapping("/app")
String hello() {
return "Hello " + bar + "!";
}
}
添加配置文件(application.properties):
server.context-path=/
server.port=8080
# default true
management.security.enabled=false
endpoints.restart.enabled=true
spring.application.name:config-client
spring.cloud.config.env:dev
spring.cloud.config.profile:dev
spring.cloud.config.uri:http://localhost:8888
启动服务(运行Application.java),访问http://localhost:8080/app(需要服务端开启),可以读取在server的配置了。如果服务端配置发生变化,可以在客户端刷新,获取最新配置,POST请求接口:http://localhost:8080/refresh,客户端配置生效。
源码:https://github.com/xiaojianhx/demo-springcloud