Getting Started
The build tools:
- gradle 4.8.1
- idea 2018.3
- Create a project using spring initializr ui
- spring cloud version Greenwich.SR1
Configuring warehouse
Create a configuration repository, cloud config default strategy is to use git to manage the configuration files;
A new F: / dir / config-repo directory;
::: tip
my local test directory dir is too long will use to identify all the paths in front of the
:::
And initializes the git repository (git little turtle can be used to initialize).
Profile is placed directly in the root directory, naming rules 服务名-环境名.yml
way to store, such as
mrcode-dev.yml
mrcode-prod.yml
Note: Remember to commit required documents;
Quickly build distribution center program
Quickly build config server through a few simple configuration, the next item in the boot
config-server/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'cn.mrcode.example.spring.cloud.tutorial.config'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "Greenwich.SR1")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-config-server'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Add git repository via the git repository to manage the configuration file (this is the default policy, use git repository to manage configuration files)
application.yml
server:
port: 11000
spring:
cloud:
config:
server:
git:
# uri: http://localhost/mrcode/config.git
# username: xx
# password: xx
uri: file://f:/dir/config-repo
uri can use a local file directory, this option is only available for local testing
file://f:/dir/config-repo
file:///f:/dir/config-repo # 而 windows 上需要多一个斜杠
http://localhost/mrcode/config.git # 线上需要使用 git 地址
Use annotations to open service
@EnableConfigServer
Check the configuration of a service
http:配置中心IP:端口/服务名/环境
Services such as mrcode
http://localhost:11000/mrcode/development
http://localhost:11000/mrcode/dev
http://localhost:11000/mrcode/prod
Output the following information, propertySources is specific profile information,
- name: is to identify the specific configuration file in the configuration repository path
- source: Profiles original content information
If propertySources is empty, there is no corresponding identity profile environment
{
"name": "mrcode",
"profiles": [
"dev"
],
"label": null,
"version": "c0fe8977dd5935f84c991e1ff501df339f012d16",
"state": null,
"propertySources": [
{
"name": "file:///F:/dir/config-repo/mrcode-dev.yml",
"source": {
"test": "测试"
}
}
]
}
Clients use
Create a client program
config-client/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'cn.mrcode.example.spring.cloud.tutorial.config'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "Greenwich.SR1")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
yml Configuration
server:
port: 11001
spring:
application:
name: config-client # 默认名称为 application
cloud:
config:
uri: http://localhost:11000 # 指向刚才创建的配置中心项目
profile: dev # 默认为 default
logging:
level:
root: info
# 打印 requestMapping
org.springframework.web: trace
management:
endpoints:
web:
exposure:
# 配合 spring-boot-starter-actuator 包,打开 /actuator/env 接口
include: "*"
After starting the client project visit: http://localhost:11001/actuator/env
output following information
configService must first figure is the configuration file in a new lab-client-dev.yml config, to see. Otherwise, after starting not see this information