根据上面的架构图,可以看到使用 Spring Cloud Config 需要创建存储配置文件的 Git 仓库、Config Server 服务,最后是我们的一般的微服务。
#### 创建 Git 仓库
以`https://github.com/JaredTan95/MicroService-Study-Samples`为例,在仓库根目录存放一些配置文件:
`microservice-consumer-movie-dev.properties`:
~~~properties
timeout = 1000
key1 = test
key2 = hello world
~~~
#### 编写 Config Server 服务
* 创建 Spring Boot 项目
~~~bash
mvn archetype:generate -DgroupId=com.shiyanlou -DartifactId=microservice-config-server -DarchetypeArtifactId=maven-archetype-quickstart
~~~
* 引入 Config Server 依赖
~~~xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.shiyanlou</groupId>
<artifactId>microservice-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-config-server</name>
<description>config server</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
~~~
* 在启动主类`App.java`上面添加注解`@EnableConfigServer`:
~~~java
package com.shiyanlou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
~~~
* 配置`application.yml`文件:
~~~yml
server:
port: 9999
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
# Git仓库地址
uri: https://github.com/JaredTan95/MicroService-Study-Samples.git
# Git仓库账号
username:
# Git仓库密码
password:
~~~
* 启动 Config Server 服务,通过 URL 可以获取 Git 仓库的配置文件:
~~~bash
curl http://localhost:9999/microservice-consumer-movie-dev.properties
~~~
即可得到如下带有配置项的响应:
~~~json
{
"key1": "test",
"key2": "hello world",
"timeout": 1000
}
~~~
#### 路径规则
Spring Cloud Config Server 提供了 RESTful API,可用来访问存放在 Git 仓库中的配置文件,其中的{appliation}、{profile} 和 {label} 都是占位符。
~~~bash
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
~~~
#### 集成 Config Client 端(对之前的服务改造)
Config Client 主要是提供给我们的微服务来向 Config Server 发起请求获取配置。因此,需要稍微改造我们的微服务。这里只是为了展示 Config Client 集成,因此采用实验二中最简单的**电影服务**为例。
* 将实验二中的`microservice-consumer-movie`项目拷贝过来
* Maven 中引入`Config Client`的依赖:
~~~xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
~~~
* `resources`添加配置文件:\*\*`bootstrap.yml`\*\*
~~~yml
spring:
application:
name: microservice-consumer-movie # 对应config server所获取的配置文件的{application}
cloud:
config:
uri: http://localhost:9999/ # config server的地址
profile: dev # profile对应config server所获取的配置文件中的{profile}
label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
~~~
* 添加一个接口`ConfigClientController.java`,演示输出拉取到的配置项:
~~~java
package com.shiyanlou;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${timeout:1}")
private String timeout;
@Value("${key1:22}")
private String key1;
@GetMapping("/configs")
public String configs() {
return toString();
}
@Override
public String toString() {
return "ConfigClientController{" +
"timeout='" + timeout + '\'' +
", key1='" + key1 + '\'' +
'}';
}
}
~~~
* 启动之后访问:`http://localhost:8010/configs`,会返回如下响应:
~~~bash
ConfigClientController{timeout='1000', key1='test'}
~~~
发现其中`timeout`和`key1`的值为我们在 Git 仓库配置的值。
- 微服务开发框架 SpringCloud
- 单体应用
- 如何解决单体应用架构中存在的问题
- 如何实现微服务架构以及技术选型
- Spring Cloud 特点
- 开始使用 Spring Cloud 实战微服务
- 快速搭建开发脚手架
- 编写服务提供者-用户微服务
- 编写服务消费者【电影微服务】
- 整合 Spring Boot Actuator
- 开始整合
- 微服务注册与发现
- 编写服务发现服务
- 注册微服务至 Eureka Server
- 更新服务提供者 (用户微服务)
- 更新服务消费者 (电影微服务)
- 查看注册结果
- Ribbon 客户端负载均衡
- Ribbon 简介
- 引入 Ribbon
- Ribbon 入门
- Feign 声明式 REST 调用
- 改造项目
- Hystrix 容错处理
- 实现容错的手段
- Hystrix 简介
- 开始使用
- 测试
- Zuul 网关
- 网关是什么
- Spring Cloud Zuul 介绍
- Zuul 入门使用
- 网关测试
- Spring Cloud Config 配置管理
- 配置中心的作用
- Spring Cloud Config 简介
- Spring Cloud Config 使用
- Sleuth 与 Zipkin 结合图形化展示
- 分布式追踪相关基础概念
- Spring Cloud Sleuth 介绍及使用
- Zipin 简介
- Docker 入门
- 云原生概念
- Docker 容器介绍
- Docker 常用命令
- 微服务运行在 Docker 之上
- Dockerfile 及其常见指令介绍
- 改造 Eureka Server 微服务
- Docker Compose 编排微服务
- 安装 Compose
- Compose 快速入门
- Compose 编排 SpringCloud微服务
- 将 Eureka 等微服务运行在 Docker 容器中
- Docker-Compose 编排文件的编写
- 通过 Docker Compose 启动、停止
- Compose编排Spring Cloud微服务2
- Docker-Compose 来部署一个双节点的 Eureka 集群
