Spring Cloud官網: 本篇主要講 "Spring Cloud Config" ,參考內容如下: "Spring Cloud Config 2.2.1.RELEASE參考文檔" "Spring Cloud Config 實現配置中心,看這一篇就夠了" 實現簡單的配置中心 配置文件就在Spri ...
Spring Cloud官網: https://spring.io/projects/spring-cloud
本篇主要講Spring Cloud Config,參考內容如下:
實現簡單的配置中心
配置文件就在Spring官方提供的配置倉庫:https://github.com/spring-cloud-samples/config-repo
1 創建配置中心服務端
完整代碼參考:https://github.com/sxpujs/spring-cloud-examples/tree/master/config/config-server
1 新建Spring Boot項目,引入config-server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2 配置config相關的配置項
bootstrap.yml
spring:
application:
name: foo # 應用名
profiles:
active: dev,mysql
application.yml
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
basedir: target/config
3 Application啟動類,增加相關註解:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4 啟動服務,進行相關測試
curl localhost:8888/foo/development
{
"name" : "foo",
"profiles" : ["development"],
"propertySources" : [
{"name":"https://github.com/spring-cloud-samples/config-repo/foo-development.properties",
"source":{"bar":"spam",
"foo":"from foo development"}},
{"name":"https://github.com/spring-cloud-samples/config-repo/foo.properties",
"source":{"foo":"from foo props",
"democonfigclient.message":"hello spring io"}},
{"name":"https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)",
"source":{"info.url" : "https://github.com/spring-cloud-samples",
"info.description":"Spring Cloud Samples",
"foo":"baz",
"eureka.client.serviceUrl.defaultZone":"http://localhost:8761/eureka/"}}
]
}
# 訪問不存在的profile(mysql)
curl http://localhost:8888/foo/mysql
{
"name":"foo",
"profiles":["mysql"],
"propertySources":[
{"name":"https://github.com/spring-cloud-samples/config-repo/foo.properties",
"source":{"foo":"from foo props",
"democonfigclient.message":"hello spring io"}},
{"name":"https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)",
"source":{"info.description":"Spring Cloud Samples",
"info.url":"https://github.com/spring-cloud-samples",
"eureka.client.serviceUrl.defaultZone":"http://localhost:8761/eureka/",
"foo":"baz"}}
]
}
curl http://localhost:8888/foo-dev.yml # 從結果來看,包含了
bar: spam
democonfigclient:
message: hello from dev profile
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
foo: from foo development
info:
description: Spring Cloud Samples
url: https://github.com/spring-cloud-samples
my:
prop: from application-dev.yml
2 創建配置中心客戶端,使用配置
完整代碼參考:https://github.com/sxpujs/spring-cloud-examples/tree/master/config/config-client
1 引用相關的maven依賴。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2 配置文件
bootstrap.yml
spring:
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888
application:
name: myapp
application.yml
management:
endpoint:
shutdown:
enabled: false
endpoints:
web:
exposure:
include: "*" # * 在yaml 文件屬於關鍵字,所以需要加引號
3 Application啟動類
@SpringBootApplication
@RestController
public class ConfigClientApplication {
@Value("${foo}")
private String foo;
@Value("${my.prop}")
private String myProp;
@RequestMapping("/")
public String home() {
return "Hello World!" + myProp + "," + foo;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
4 驗證數據
訪問env埠,註意:從Spring Boot 2.x開始,不能直接訪問 http://localhost:8080/env,需要添加actuator。
curl localhost:8080 # 可以看出my.prop這個屬於是來自application-dev.yml,foo來自application.yml
Hello World!from application-dev.yml,baz
curl localhost:8080/actuator/env|json_pp
{
"activeProfiles" : [],
"propertySources" : [
{"name":"server.ports","properties":{"local.server.port":{"value":8080}}},
{
"name" : "bootstrapProperties-https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)",
"properties" : {
"info.url" : {"value" : "https://github.com/spring-cloud-samples"},
"eureka.client.serviceUrl.defaultZone" : {"value" : "http://localhost:8761/eureka/"},
"foo" : {"value" : "baz"},
"info.description" : {"value" : "Spring Cloud Samples"}
}
}
]
}