SpringCloud Config 分布式配置中心
SpringCloud 2018
- 微服务概述
- SpringCloud 概述
- 微服务工程搭建
- Eureka 服务注册与发现
- Ribbon 客户端负载均衡
- Feign 服务接口调用
- Hystrix 断路器
- Zuul 路由网关
- SpringCloud Config 分布式配置中心
- SpringCloud 技术梳理
GitHub
- https://github.com/hjyang19/microservicecloud.git
- https://github.com/hjyang19/microservicecloud-config.git
SpringCloud Config 分布式配置中心
- 1-SpringCloud Config 概述
- 1.1-是什么
- 1.2-怎么玩
- 1.3-能干嘛
- 2-SpringCloud Config 服务端
- 2.1-读取配置
- 2.2-读取规则
- 3-SpringCloud Config 客户端
- 4-SpringCloud Config 配置示例
- 4.1-Github 配置文件
- 4.2-Config 版 Eureka 服务端
- 4.3-Config 版 Dept 微服务
1-SpringCloud Config 概述
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud 提供了 Config Server 来解决这个问题,每一个微服务自己带着一个 application.yml,上百个配置文件的管理。
1.1-是什么
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
1.2-怎么玩
SpringCloud Config分为服务端和客户端两部分。
服务端:也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息、加密、解密信息等访问接口。
客户端:是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用 Git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。
1.3-能干嘛
集中管理配置文件:
- 不同环境不同配置,动态化的配置更新,分环境部署(dev、test、prod、beta、release)
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
- 将配置信息以 Rest 接口的形式暴露。
GitHub 整合配置:由于 SpringCloud Config 默认使用 Git 来存储配置文件(也有其它方式,比如支持 SVN 和本地文件),但最推荐的还是 Git,而且使用的是 Http、Https 访问的形式。
2-SpringCloud Config 服务端
2.1-读取配置
a、在 GitHub 上新建一个名为 microservicecloud-config 的仓库
b、复制 microservicecloud-config 仓库 Https 协议的地址
git@github.com:hjyang19/microservicecloud-config.git
c、本地硬盘新建目录 D:\config,克隆 microservicecloud-config 仓库
git clone git@github.com:hjyang19/microservicecloud-config.git
d、本地目录 D:\config\microservicecloud-config 里新建 application.yml
保存格式必须为UTF-8
spring:
profiles:
active:
- dev
---
spring:
profiles: dev # 开发环境
application:
name: microservicecloud-config-atguigu-dev
---
spring:
profiles: test # 测试环境
application:
name: microservicecloud-config-atguigu-test
e、本地目录 D:\config\microservicecloud-config 仓库变更推送到 GitHub 上
# 推送 D:\config\microservicecloud-config 仓库变更
git status
git add .
git commit -m "init yml"
git push origin master
f、右击父工程 microservicecloud,新建子模块 microservicecloud-config-3344(配置中心模块)
g、修改子模块 microservicecloud-config-3344 中 pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-3344</artifactId>
<dependencies>
<!-- 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>
<!-- 图形化监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<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>
<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>
h、修改子模块 microservicecloud-config-3344,目录 /src/main/resources 中新建 application.yml
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
# GitHub 仓库地址
uri: https://github.com/hjyang19/microservicecloud-config.git
#uri: git@github.com:hjyang19/microservicecloud-config.git # SSH 协议可能访问不到数据
i、修改子模块 microservicecloud-config-3344,目录 /src/main/java 中新建包 com.atguigu.springcloud
j、修改子模块 microservicecloud-config-3344,包 com.atguigu.springcloud 中新建启动类 Config_3344_StartSpringCloudApp.java
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);
}
}
k、修改 Windows 网络映射配置
找到 C:\Windows\System32\drivers\etc 路径下的 hosts 文件,添加内容:
127.0.0.1 config-3344.com
l、测试 Config 服务端
测试子模块 microservicecloud-config-3344 是否可以从 GitHub 上获取配置内容
- 启动子模块 microservicecloud-config-3344
- http://config-3344.com:3344/application-dev.yml
- http://config-3344.com:3344/application-test.yml
http://config-3344.com:3344/application-xxx.yml
不存在的配置
2.2-读取规则
/{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
3-SpringCloud Config 客户端
a、在本地目录 D:\config\microservicecloud-config 里新建 microservicecloud-config-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/
b、本地目录 D:\config\microservicecloud-config 仓库变更推送到 GitHub 上
# 推送 D:\config\microservicecloud-config 仓库变更
git status
git add .
git commit -m "init yml"
git push origin master
c、右击父工程 microservicecloud,新建子模块 microservicecloud-config-client-3355
d、修改子模块 microservicecloud-config-client-3355 中 pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-client-3355</artifactId>
<dependencies>
<!-- SpringCloud Config 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<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>
<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>
e、修改子模块 microservicecloud-config-client-3355,目录 /src/main/resources 中新建 bootstrap.yml
spring:
cloud:
config:
# Github 上读取的资源名称,注意没有 yml 后缀名
name: microservicecloud-config-client
profile: dev # 访问的配置项
label: master
# 微服务启动后先去找端口 3344 服务,通过 SpringCloud Config 获取 GitHub 的服务地址
uri: http://config-3344.com:3344
bootstrap.yml:
- applicaiton.yml:用户级的资源配置项。
- bootstrap.yml:系统级的,优先级更加高。
Spring Cloud 会创建一个 Bootstrap Context,作为 Spring 应用的 Application Context 的父上下文。
初始化的时候,Bootstrap Context 负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
Bootstrap 属性有高优先级。默认情况下,它们不会被本地配置覆盖。Bootstrap context 和 Application Context 有着不同的约定,所以新增了一个 bootstrap.yml 文件,保证 Bootstrap Context 和 Application Context 配置的分离。
f、修改子模块 microservicecloud-config-client-3355,目录 /src/main/resources 中新建 application.yml
spring:
application:
name: microservicecloud-config-client
g、修改子模块 microservicecloud-config-client-3355,目录 /src/main/java 中新建包 com.atguigu.springcloud
h、修改子模块 microservicecloud-config-client-3355,包 com.atguigu.springcloud 中新建启动类 ConfigClient_3355_StartSpringCloudApp.java
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);
}
}
i、修改子模块 microservicecloud-config-client-3355,目录 /src/main/java 中新建包 com.atguigu.springcloud.rest
j、修改子模块 microservicecloud-config-client-3355,包 com.atguigu.springcloud 中新建 ConfigClientRest.java
验证是否能从 GitHub 上读取配置。
package com.atguigu.springcloud.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientRest {
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServers;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig() {
String str = "applicationName: " + applicationName +
"\t eurekaServers:" + eurekaServers + "\t port: " + port;
System.out.println("******str: " + str);
return str;
}
}
k、修改 Windows 网络映射配置
找到 C:\Windows\System32\drivers\etc 路径下的 hosts 文件,添加内容:
127.0.0.1 client-config.com
l、测试 Config 客户端
子模块 microservicecloud-config-client-3355 通过访问子模块 microservicecloud-config-3344 获取GitHub 配置信息。
启动子模块 microservicecloud-config-3344
等待 Config 服务端完全启动。
- http://config-3344.com:3344/application-dev.yml
启动子模块 microservicecloud-config-client-3355
bootstrap.yml 里面的 profile 值是什么,决定从 Github 上读取什么。
当前 bootstrap.yml 里是 profile : dev,dev 在 GitHub上端口号是 8021
http://client-config.com:8201/config
若 bootstrap.yml 里是 profile : test,test 在 GitHub上端口号是 8202
http://client-config.com:8202/config
4-SpringCloud Config 配置示例
目前情况:
- Config 服务端配置测试通过,可以通过 Config + GitHub 进行配置修改并获得内容。
- 此时做一个 Eureka 服务、一个 Dept 访问的微服务,将两个微服务的配置统一由 GitHub 获得,实现统一配置分布式管理,完成多环境的变更。
4.1-Github 配置文件
a、在本地目录 D:\config\microservicecloud-config 里新建 microservicecloud-config-eureka-client.yml
spring:
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://eureka7001.com: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://eureka7001.com:7001/eureka/
b、在本地目录 D:\config\microservicecloud-config 里新建 microservicecloud-config-dept-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 8001
spring:
profiles: dev
application:
name: microservicecloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB01
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.atguigu.springcloud.entities
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 客户端注册进 Eureka 服务列表
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: dept-8001.com
prefer-ip-address: true
info:
app.name: atguigu-microservicecloud-springcloudconfig01
company.name: www.atguigu.com
build.artifactId: "@project.artifactId@"
build.version: "@project.version@"
---
server:
port: 8001
spring:
profiles: test
application:
name: microservicecloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB02
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.atguigu.springcloud.entities
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 客户端注册进 Eureka 服务列表
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: dept-8001.com
prefer-ip-address: true
info:
app.name: atguigu-microservicecloud-springcloudconfig02
company.name: www.atguigu.com
build.artifactId: "@project.artifactId@"
build.version: "@project.version@"
c、本地目录 D:\config\microservicecloud-config 仓库变更推送到 GitHub 上
# 推送 D:\config\microservicecloud-config 仓库变更
git status
git add .
git commit -m "init yml"
git push origin master
4.2-Config 版 Eureka 服务端
a、右击父工程 microservicecloud,新建子模块 microservicecloud-config-eureka-client-7001
b、修改子模块 microservicecloud-config-eureka-client-7001 中 pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-eureka-client-7001</artifactId>
<dependencies>
<!-- SpringCloud Config 配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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>
c、修改子模块 microservicecloud-config-eureka-client-7001,目录 /src/main/resources 中新建 bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-eureka-client # 从 Github 上读取的资源名称,注意没有 yml 后缀名
profile: dev
label: master
uri: http://config-3344.com:3344 # SpringCloud Config 获取的服务地址
d、修改子模块 microservicecloud-config-eureka-client-7001,目录 /src/main/resources 中新建 application.yml
spring:
application:
name: microservicecloud-config-eureka-client
e、修改子模块 microservicecloud-config-eureka-client-7001,目录 /src/main/java 中新建包 com.atguigu.springcloud
f、修改子模块 microservicecloud-config-eureka-client-7001,包 com.atguigu.springcloud 中新建启动类 Config_Git_EurekaServerApplication.java
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
}
}
g、测试 Config 版 Eureka 服务端
启动子模块 microservicecloud-config-3344
等待 Config 服务端完全启动。
- 启动子模块 microservicecloud-config-eureka-client-7001
http://eureka7001.com:7001/
出现 Eureak 主页表示成功启动
4.3-Config 版 Dept 微服务
a、右击父工程 microservicecloud,新建子模块 microservicecloud-config-dept-client-8001
b、修改子模块 microservicecloud-config-dept-client-8001 中 pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-dept-client-8001</artifactId>
<dependencies>
<!-- SpringCloud Config 配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</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>
c、修改子模块 microservicecloud-config-dept-client-8001,目录 /src/main/java 中新建包 com.atguigu.springcloud
d、复制子模块 microservicecloud-provider-dept-8001 中包 com.atguigu.springcloud 内容到子模块 microservicecloud-config-dept-client-8001 包 com.atguigu.springcloud
e、修改子模块 microservicecloud-config-dept-client-8001,包 com.atguigu.springcloud 启动类重命名为 DeptProvider8001_Config_App.java
f、复制子模块 microservicecloud-provider-dept-8001 中目录 /main/resources 内容到子模块 microservicecloud-config-dept-client-8001 目录 /main/resources
g、修改子模块 microservicecloud-config-dept-client-8001,目录 /src/main/resources 中新建 bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-dept-client # Github 上读取的资源名称,注意没有 yml 后缀名
# profile 配置是什么就取什么配置 dev or test
#profile: dev
profile: test
label: master
uri: http://config-3344.com:3344 # SpringCloud Config 获取的服务地址
g、修改子模块 microservicecloud-config-dept-client-8001,目录 /src/main/resources 中 application.yml
spring:
application:
name: microservicecloud-config-dept-client
主要看 bootstrap.yml 文件里面的 profile: xxx 属性是什么值,从而确定它能从 GitHub 上取什么样的配置。若配置 profile: test 取值如下图路。具体各自数据库不同,从而依据配置得到分布式配置的目的。
h、测试 Config 版 Eureka 服务端
启动子模块 microservicecloud-config-3344
等待 Config 服务端完全启动。
- 启动子模块 microservicecloud-config-eureka-client-7001
- 启动子模块 microservicecloud-config-dept-client-8001
http://localhost:8001/dept/get/1
默认配置 profile: test 访问数据库 cloudDB02。
http://localhost:8001/dept/get/1
换成配置 profile: dev 访问数据库 cloudDB01。
还没有评论,来说两句吧...