config unified configuration center
1. Create a module project
In the "Create maven project spring cloud of (a)" to add a module called chaotic-config on maven project, the project is structured as follows:
2, the configuration pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
3, configuration engineering boot entry
@EnableConfigServer //加载服务器上的配置文件
@SpringBootApplication
@EnableDiscoveryClient //consul能动性
public class ConfigApp {
public static void main(String[] args) {
SpringApplication.run(ConfigApp.class, args);
}
}
4, 配置 bootstrap.yml
spring:
application:
name: chaotic-config
cloud:
consul: #向consul服务中心注册
host: localhost
port: 8500
config:
server:
git:
uri: https://gitee.com/heihei/chaotic.git #gitee上的git仓库名
username: heiheiname
password: heiheipsw
force-pull: true #设置强行pull拉取
default-label: master #配置文件分支
search-paths: chaotic-config/src/main/resources/config #配置文件所在根目录
5, placed application.yml
server:
port: 8501
6, addition of server-dev.yml sum server-prod.yml
Currently contents are as follows (see the development of real time decision how to configure)
server:
port: 8503
spring:
application:
name: chaotic-server
cloud:
consul:
host: localhost
port: 8500
#注册到consul的服务名称
discovery:
serviceName: chaotic-server
Note that the server-dev.yml and server.yml inside the content must be standardized yml grammar, otherwise parsing error as follows:
在这里插入代码片Caused by: org.yaml.snakeyaml.constructor.DuplicateKeyException: while constructing a mapping
in 'reader', line 1, column 1:
spring:
^
found duplicate key spring
in 'reader', line 6, column 1:
spring:
^
7, testing and start-up variation postman View consul services
Get server.yml file information:
acquisition server-dev.yml information:
8, summary
- If the project up and running, be sure to note the version cloud and boot to be consistent.
- git config file to load the default root of the repository, if not in the root directory, be sure to pay attention to specify the "search-paths: the file name."
- Problems encountered can not be accessed, make sure git user name and password are correct.
- Be sure yml specification, otherwise there will be problems to resolve.
- By loading the file is to be found there, such as server-dev.yml, loading path corresponding server / dev; Full is: http: // localhost: 8501 / server / dev. The last one is the file name before Leverage, Leverage is the last corresponding version behind.