前言:微服务架构中,每个项目都有一个yml配置,管理起来麻烦。可以使用spring cloud config来统一管理
Spring Cloud Config是什么?
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色:①config server,②config client
架构:
能干什么?
Spring Cloud Config 和 github集成
温馨小提示:可以和svn,git集成,但是推荐使用github 集成使用
一、服务端配置
1.github创建配置文件 https://github.com/zhengqingya/microservice-config/blob/master/application-user.yml
2.创建springboot项目并且导入jar
<dependencies>
<!--springboot支持-->
<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>
<!-- eureka客户端 -->
<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>
</dependencies>
3.代码方面:
4.测试:启动
http://127.0.0.1:1299/application-user/dev 和 http://127.0.0.1:1299/application-user/test
温馨小提示:这里实际上访问的是如下application-user
二、客户端配置
1.创建普通maven项目:
2.导入依赖:
<dependencies>
<!--公共依赖-->
<dependency>
<groupId>com.zhengqing.springcloud</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<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>
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
3.代码方面:
4.测试:启动 、 、
看控制台端口
Eureka上面名字
最后小结:
①搭建服务端 去访问github上面的统一管理的配置,注册到注册中心
②搭建客户端 通过服务端直接去拿到github上面的配置
【注意:除了configserver,Eureka不用以外,其他都要通过configserver到github上面去获取,以后要维护我们的配置就可以去维护github上面的仓库即可,就达到了我们的配置中心统一管理的效果】