上一章节, 我们讲解了服务网关 zuul, 本章节我们从 Git 和本地两种存储配置信息的方式来讲解 springcloud 的分布式配置中心 - Spring Cloud Config.
一, Spring Cloud Config 简介:
Spring Cloud Config 项目是一个解决分布式系统的配置管理方案. 它包含了 Client 和 Server 两个部分, server 提供配置文件的存储, 以接口的形式将配置文件的内容提供出去, client 通过接口获取数据, 并依据此数据初始化自己的应用.
二, 新建 springcloud-config-server 模块:
1. 参考: 一起来学 Spring Cloud | 第一章 : 如何搭建一个多模块的 springcloud 项目 来新建一个基本模块结构
2. 修改 pom.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>com.haly</groupId>
- <artifactId>springcloud</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.haly</groupId>
- <artifactId>springcloud-config-server</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springcloud-config-server</name>
- <description > 新建一个 config server 项目 </description>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
3. 新建入口启动类 SpringcloudConfigServerApplication
@EnableConfigServer, 表示开启 Config Server
- package com.haly;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.config.server.EnableConfigServer;
- @EnableConfigServer
- @SpringBootApplication
- @EnableDiscoveryClient
- public class SpringcloudConfigServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringcloudConfigServerApplication.class, args);
- }
- }
4. 修改 application.properties 文件 (Git 方法存储配置)
在 GitHub 上创建一个项目, 并在上面添加配置文件 config-client.properties, 配置文件里添加一个属性 config=NewConfig !.
在 application.properties 中配置服务信息以及 Git 信息
- spring.application.name=springcloud-config-server
- server.port=7001
- eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- spring.cloud.config.server.Git.uri=https://github.com/FunriLy/springcloud-study/
- spring.cloud.config.server.Git.searchPaths=config-repo
- spring.cloud.config.server.Git.username=Username
- spring.cloud.config.server.Git.password=Password
启动工程 Config Server, 浏览器输入: http://localhost:7001/config-client/default/master, 得到结果如下
- {
- "name": "config-client",
- "profiles": [
- "default"
- ],
- "label": null,
- "version": "52b88000fc46a8b1d72a2979f4721d45a3d1f429",
- "state": null,
- "propertySources": [
- {
- "name": "https://github.com/FunriLy/springcloud-study//config-repo/config-client.properties",
- "source": {
- "configword": "NewConfig !"
- }
- }
- ]
- }
三, 新建 springcloud-config 模块 (可不要):
其实我在工作中喜欢创建一个 springcloud-config 模块, 没有业务代码, 只有 pom.xml 文件和 resources 目录, resources 放一下公共的配置文件, 可以给其它模块引用
例如配置:
spring-data-MySQL.xml 文件
spring-data-Redis-single.xml 文件
spring-jdbc-MySQL.xml 文件
Bootstrap.YAML 配置文件
1. 新增 pom.xml 文件
- <?xml version="1.0"?>
- <project
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
- xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.sinaif</groupId>
- <artifactId>sinaif-weibo-opt</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <relativePath>../pom.xml</relativePath>
- </parent>
- <groupId>com.sinaif</groupId>
- <artifactId>sinaif-config</artifactId>
- <name>${project.artifactId}</name>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- </project>
2. 新增 Bootstrap.YAML 文件 (可以统一配置 config,eureka ,hystrix 等待)
- (注意这里是 Bootstrap.properties 而不是 appliction.properties. 因为 Bootstrap.properties 会在应用启动之前读取, 而 spring.cloud.config.uri 会影响应用启动)
- spring:
- cloud:
- config:
- name: config-client
- profile: default
- label: master
- uri: http://localhost:7001/
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
上面配置 config 属性的规则, 以及前面我们直接用 http://localhost:7001/config-client/default/master 访问配置的规则
URL 与配置文件的映射关系
- /{
- application
- }/{
- profile
- }[/{
- label
- }]
- /{
- application
- }-{
- profile
- }.YAML
- /{
- label
- }/{
- application
- }-{
- profile
- }.YAML
- /{
- application
- }-{
- profile
- }.properties
- /{
- label
- }/{
- application
- }-{
- profile
- }.properties
application 是 config-client-dev.properties 文件中的 config-client 值, profile 是一个 active 的 profile, 一般用来表示环境信息, label 是一个可选的标签, 一般用来表示目录名称.
比如, 我在 GitHub 上的文件名是 config-client.properties, 在 GitHub 上都是 default 环境, 默认为 master 分支. 所以就是 / config-client/default/master
四, 新建 springcloud-config-server 的 client 模块:
我用之前写过的 springcloud-feign-client 模块, 在 FeignController 类里增加一个 / testconfig 方法充当 Client 端, springcloud-feign-client 模块内容参考: 一起来学 Spring Cloud | 第四章: 服务消费者 ( Feign )
1. 在 pom.xml 文件中, 增加上面新建的模块的依赖包, 这样我们就能使用 springcloud-config 模块中的 eureka 和 config 配置
- <dependency>
- <groupId>
- com.haly
- </groupId>
- <artifactId>
- springcloud-config
- </artifactId>
- <version>
- 0.0.1-SNAPSHOT
- </version>
- </dependency>
2. 在 FeignController 类中增加一个测试方法
- @Value("${configword}")
- String configword;
- @GetMapping(value = "/testconfig")
- public String testconfig(@RequestParam String name) {
- return name +",git 配置值:" + configword ;
- }
3. 启动服务, 进行测试
依次启动 springcloud-eureka-server 模块, 启动 springcloud-config-server 模块, 启动 springcloud-feign-client 模块
访问: http://localhost:9600/testconfig?name=young 码农, 返回结果 :
young 码农, Git 配置值: NewConfig !
五, 使用本地配置获取配置项:
1. 修改 springcloud-config-server 模块中的 application.properties 配置如下
- spring.application.name=springcloud-config-server
- server.port=7001
- eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- # 表示使用本地 config 配置
- spring.profiles.active=native
- # 表示本地配置读取的目录文件位置
- spring.cloud.config.server.native.searchLocations: classpath:/config/
2. 在 springcloud-config-server 模块 resources 目录下新建一个 config 文件, 在 config 文件下新建 2 个配置文件, 配置项内容如下:
- configs-dev.properties:configword: dev-configword
- configs-test.properties:configword: test-configword
3. 修改 springcloud-config 模块的 Bootstrap.YAML 配置文件的 config 配置
ps: 目前我们设置的 profile 为 dev, 所以会从 configs-dev.properties 配置文件中读取数据
- spring:
- cloud:
- config:
- name: configs
- profile: dev
- label: config
- uri: http://localhost:7001/
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
4. 启动服务, 运行结果
依次启动 springcloud-eureka-server 模块, 启动 springcloud-config-server 模块, 启动 springcloud-feign-client 模块
先访问 config-server, 浏览器输入: http://localhost:7001/configs/dev/config
- {
- "name": "configs",
- "profiles": [
- "dev"
- ],
- "label": "config",
- "version": null,
- "state": null,
- "propertySources": [
- {
- "name": "classpath:/config/configs-dev.properties",
- "source": {
- "configword": "dev-configword"
- }
- }
- ]
- }
在访问 config-client, 浏览器输入: http://localhost:9600/testconfig?name=young 码农, 返回结果如下:
young 码农, Git 配置值: dev-configword
5. 修改 springcloud-config 模块的 Bootstrap.YAML 配置文件的 profile 属性:
ps: 目前我们设置的 profile 为 dev, 所以会从 configs-test.properties 配置文件中读取数据
- spring:
- cloud:
- config:
- name: configs
- profile: test
- label: config
- uri: http://localhost:7001/
6. 再次启动服务, 运行结果
依次启动 springcloud-eureka-server 模块, 启动 springcloud-config-server 模块, 启动 springcloud-feign-client 模块
先访问 config-server, 浏览器输入: http://localhost:7001/configs/dev/config
- {
- "name": "configs",
- "profiles": [
- "test"
- ],
- "label": "config",
- "version": null,
- "state": null,
- "propertySources": [
- {
- "name": "classpath:/config/configs-test.properties",
- "source": {
- "configword": "test-configword"
- }
- }
- ]
- }
在访问 config-client, 浏览器输入: http://localhost:9600/testconfig?name=young 码农, 返回结果如下:
young 码农, Git 配置值: test-configword
来源: https://www.cnblogs.com/haly/p/10945138.html