Spring Cloud Config作为分布式配置中心组件 ,包括Config 服务端,和Config 客户端。
- Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。
- Config Client是Config Server的客户端,用于操作存储在Config Server中的配置内容。微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。
Config实战
1、创建项目config服务端
创建子模块config-server,pom.xml引入eureka-client 和config-server的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2、创建配置文件
新建config-server自身的配置文件application.yml
server:
port: 8005
spring:
application:
name: config-server
profiles:
active: native #使用本地文件
cloud:
config:
server:
native:
search-locations: classpath:/repo #本地配置仓库地址
# git:
# uri: https://gitee.com/xxxx/xxxxx.git
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
这里我们以使用本地配置仓库地址为例,spring.profiles.active
设置为native
,配置仓库路径为repo文件夹,所以我们在resources文件下创建repo文件夹,并创建新的一个configclient-dev.yml的文件,内容如下:
server:
port: 8007
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
feign:
hystrix:
enabled: true
logging:
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
3、新建启动类
/\*\*
\* @Author:公众号:程序员965
\* @create 2022-07-05
\*\*/
@EnableConfigServer //开启配置服务
@EnableEurekaClient
@SpringBootApplication
public class ConfitServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfitServerApplication.class, args);
}
}
注意增加@EnableConfigServer
注解,表示这是个配置中心服务端。
4、创建配置中心客户端
服务端开发完成后,我们再新建一个客户端config-client项目,引入如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
与服务端不同的是,客户端的配置文件我们创建bootstrap.yml文件
spring:
cloud:
![img](https://img-blog.csdnimg.cn/img_convert/6bf425bc740b93076ea43d1494b521f2.png)
![img](https://img-blog.csdnimg.cn/img_convert/dd992edad43bd39eb0a681a07f556968.png)
![img](https://img-blog.csdnimg.cn/img_convert/b8358743019949097d92b88824470505.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**