08.2 SpringCloud Config服务端配置

SpringCloud Config服务端配置

1. Git托管配置文件

本文使用Github私有仓库来实现,创建过程略(配置文件为敏感数据,尽量使用私有仓库模式)

创建完成后添加文件 application.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spring:
profiles:
active:
- dev
---
spring:
#开发环境
profiles: dev
application:
name: microservicecloud-config-atguigu-dev
---
spring:
#测试环境
profiles: test
application:
name: microservicecloud-config-atguigu-test
#请保存为UTF-8格式

2. 服务端配置

2.1 新建Module模块microservicecloud-config-3344

它即为Cloud的配置中心模块

2.2 pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.hellodev</groupId>
<artifactId>atguigu-microservicecloud</artifactId>
<version>0.0.1</version>
</parent>

<artifactId>microservicecloud-config-3344</artifactId>
<version>0.0.1</version>

<dependencies>
<dependency><!-- 引入自己定义的 api 通用包,可以使用 Dept 部门Entity -->
<groupId>com.hellodev</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>

<!-- springCloud Config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!-- 避免 Config 的 Git 插件报错: org /eclipse/ jgit / api /TransportConfigCallback -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</dependency>

<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

<!-- 将微服务provider侧注册进 eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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-test</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>

2.3 yml文件

1
2
3
4
5
6
7
8
9
10
11
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
#GitHub上面的git仓库名字
uri: git@github.com:lujiahao0708/microservicecloud-config.git

2.4 主启动类

1
2
3
4
5
6
7
@SpringBootApplication
@EnableConfigServer
public class Config3344Application {
public static void main(String[] args) {
SpringApplication.run(Config3344Application.class, args);
}
}

2.5 hosts文件增加

1
127.0.0.1  config-3344.com

2.6 验证

启动config-3344服务,访问url查看结果

前两个分别为dev环境和test环境,最后一个为不存在的环境

3. 配置读取规则

/{application}-{profile}.yml
    http://config-3344.com:3344/application-dev.yml
    http://config-3344.com:3344/application-test.yml
    http://config-3344.com:3344/application-xxx.yml(不存在的配置)
/{application}/{profile}[/{label}]
    http://config-3344.com:3344/application/dev/master
    http://config-3344.com:3344/application/test/master
    http://config-3344.com:3344/application/xxx/master
/{label}/{application}-{profile}.yml
    http://config-3344.com:3344/master/application-dev.yml
    http://config-3344.com:3344/master/application-test.yml

资源获取

公众号回复 : SpringCloud Config服务端配置 获取本节代码

公众号回复 : SpringCloud思维导图

Tips

欢迎收藏和转发,感谢你的支持!(๑•̀ㅂ•́)و✧

欢迎关注我:后端小哥,专注后端开发,希望和你一起进步!