一、介绍
简答来说就是把配置文件交给一个配置中心统一管理,这个配置中心从远程下载配置文件。
二、统一配置中心微服务
统一配置中心也是一个微服务,这个微服务是个公共的,其他微服务通过读取此微服务来获取配置文件。
2.1、pom
<!-- 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>
2.2、yml
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: https://github.com/zhuxian89/microservicecloud-config.git #GitHub上面的git仓库名字
自己在github上面创建配置仓库,创建属于自己的仓库url(就是上面的uri)
2.3、启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer /**统一配置中心服务类*/
public class Config_3344_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
}
}
2.4、github配置中心截图
从配置中心访问(文件名字+profiles)
**比如文件名字:microservicecloud-config-client,其中一个profiles名字是dev 所以总的路径是microservicecloud-config-client-dev.yml **
http://localhost:3344/microservicecloud-config-client-dev.yml
2.5、关于DEV和Test等分支
目前配置dev test real配置文件有二种方式
第一种就是所有的分支配置文件写在一起
pring:
profiles:
active:
- dev
---
server:
port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
spring:
profiles: dev
application:
name: microservicecloud-config-eureka-client
eureka:
instance:
hostname: eureka7001.com #冒号后面必须要有空格
client:
register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
fetch-registry: false #不通过eureka获取注册信息
service-url:
defaultZone: http://localhost:7001/eureka/
---
server:
port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
spring:
profiles: test
application:
name: microservicecloud-config-eureka-client
eureka:
instance:
hostname: eureka7001.com #冒号后面必须要有空格
client:
register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
fetch-registry: false #不通过eureka获取注册信息
service-url:
defaultZone: http://localhost:7001/eureka/
这种方式自我感觉虽然简化了书写,但是不如单个文件清楚明了
注意不同配置分支用 — 分开
第二种就是每个分支一个配置文件
microservicecloud-config-eureka-server.yml,这个是总的入口文件。
spring:
profiles:
active:
- dev
microservicecloud-config-eureka-server-dev.yml, DEV分支文件。
server:
port: 7001
spring:
application:
name: microservicecloud-config-eureka-client
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
hostname: eureka7001.com-dev
microservicecloud-config-eureka-server-test.yml, TEST分支文件。
server:
port: 7001
spring:
application:
name: microservicecloud-config-eureka-client
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
hostname: eureka7001.com-test
三、从统一配置中心取数据
3.1 pom
<!-- SpringCloud Config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.1 yml
创建bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: test #本次访问的配置项
label: master
uri: http://localhost:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
通过这样的配置就从http://localhost:3344这个微服务里面 取出文件是microservicecloud-config-client的配置,其中利用profile取出指定的配置