一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
上一章节,我们讲解了服务网关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.yml 配置文件
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.yml文件(可以统一配置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}.yml
- /{label}/{application}-{profile}.yml
- /{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.yml配置文件的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.yml配置文件的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
一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)的更多相关文章
- Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】
Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-24 | Spring Cloud Confi ...
- Spring Cloud第十一篇 | 分布式配置中心高可用
本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)
在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管 ...
- Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config
目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...
- SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)
在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...
- SpringCloud(6)分布式配置中心Spring Cloud Config
1.Spring Cloud Config 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组 ...
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
- 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
随机推荐
- RDD的Lineage血统
1.RDD血统:数据容错,发生错误,可以进行重算恢复.Lineage记录的是特定数据的 Transformation 转换操作. 为了保证RDD中数据的鲁棒性,RDD数据集通过所谓的血统关系(Line ...
- fitnesse wiki界面设置变量
有时候我们可能多组测试数据会到同一个值,这样我们就可以设置一个变量,修改时只需要修改一个地方即可,而不需要对每组测试数据的这列数据进行修改 如下图: (1)定义变量:!define A {10} , ...
- 学习Spring-Data-Jpa(十七)---对Web模块的支持
Spring-Data还提供了Web模块的支持,这要求Web组件Spring-MVC的jar包位于classpath下.通常通过使用@EnableSpringDataWebSupport注解来启用继承 ...
- 洛谷 P3388 【模板】割点(割顶)题解
今天学了割点,就A了这道板子,比较难理解的地方就在于如果是根节点就要找两个点来满足low[y]>=dfn[x],如果不是就只需找一个点来满足.Tarjan(i,i)中第一个i是开始搜索的点而第 ...
- pgloader 学习(四)一些简单操作例子
上边已经说明了pgloader 的基本使用(篇理论),但是对于实际操作偏少,以下是一个简单的操作 不像官方文档那样,我为了方便,直接使用docker-compose 运行,同时这个环境,会在后边大部分 ...
- vue中前端弹窗队列展示
在前端写一个弹窗可能很简单,那如果同时有多个弹窗呢 这样的话就要考虑弹窗的展示问题,肯定是不能叠加在一起的,这时候就要通过队列(先进先出)来展示 思路就是根据队列来实现,至于具体的实现方式,可以在项目 ...
- C Primer Plus AND 菜鸟教程
C语言概述 首先,windows 环境下安装 GCC编译环境 下载 MinGW 下载地址:http://sourceforge.net/projects/mingw/files/ 根据系统环境下载对应 ...
- mysql 获取数学成绩最高以及最低的同学
mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...
- 理解Web路由(浅谈前后端路由与前后端渲染)
1.什么是路由? 在Web开发过程中,经常会遇到『路由』的概念.那么,到底什么是路由?简单来说,路由就是URL到函数的映射. 路由的概念最开始是由后端提出来的,在以前用模板引擎开发页面的时候,是使用路 ...
- Linux 磁盘的分区
如果我们想在系统中新增一块硬盘,需要做什么呢? 1. 对磁盘进行分区,新建可用分区 2. 对该分区进行格式化,以创建系统可用的文件系统 3. 若想要仔细一点,可以对刚才新建好的文件系统进行检验 4. ...