💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
根据上面的架构图,可以看到使用 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 仓库配置的值。