SpringCloud---SpringCloud Config

抛出问题:

       假如有一个微服务项目,有很多微服务实例,那么相对就有很多配置文件,如果在每个微服务中进行配置,那么每次部署或许都要进行修改,太多微服务,则造成配置文件不易管理。

SpringCloud Config:

       此技术用与管理配置文件,C/S模式。把各个微服务实例的配置文件上传到github,通过github进行管理。编写server端,client端通过server端获取自己的配置文件。

准备工作:

        在github新建仓库,上传配置文件。

编写server端module:

       pom.xml添加Config Server端坐标。

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

        application.properties配置文件配置github相关信息

server.port=5050


#配置文件所在git仓库的地址 用https协议
spring.cloud.config.server.git.uri=https://github.com/1611817390/microservicecloud-config.git
#git登录账号
[email protected]
#git登录密码
spring.cloud.config.server.git.password=xxxxxxxxxxxx
#配置文件所在的仓库分支
spring.cloud.config.server.git.label=master
#配置文件所在分支的具体文件夹路径
spring.cloud.config.server.git.search-paths=/SpringCloud/

        SpringBoot启动类上添加注解,表明是Server端:

@EnableConfigServer

测试:

       启动类启动,进行访问:http://ip:port/配置文件名称

编写Client端module:

        pom.xml添加Config Client端坐标。

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

         src/main/resources 下创建文件bootstrap.properties文件,配置Config Server端信息:

#要找的配置文件名称
spring.cloud.config.name=application-eureka1
#Config Server端路径
spring.cloud.config.uri=http://127.0.0.1:5050/

先启动Config Server端module,在启动Config Client端,这样,Client就获取到了自己的配置文件,启动时就会读取相应配置,如果进行修改,只需要进行github上配置文件修改,而不需要修改项目。

发布了66 篇原创文章 · 获赞 26 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_39115469/article/details/104639762