SpringCloud创建Config Client配置读取
1.说明
本文详细介绍配置中心客户端使用方法,
即Config Client到Config Server读取配置,
这里以创建Config Client服务为例,
基于已经创建好的Config Server模块,
请参考SpringCloud创建Config模块,
到配置中心读取配置。
2.创建工程,添加依赖
在父工程下面创建一个Maven模块config-client,
在pom.xml中增加config client的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
其中spring-boot-starter-web是spring boot应用,
可以对外提供Rest服务,
下面会通过Rest接口查询客户端读取到的配置。
3.新增配置文件
新增配置文件bootstrap.yml:
server:
port: 8001
spring:
application:
name: config-client
cloud:
config:
label: master
name: config-client-demo
profile: test
uri: http://localhost:9009
上面的spring.cloud.config配置,
指定了配置中心http://localhost:9009,
拉取master分支下的config-client-demo.yml中的test配置。
4.git仓库增加配置
由于http://localhost:9009配置中心,
指定了spring-cloud-config仓库的地址:
https://gitee.com/bugzeroman/spring-cloud-config.git
需要把config-client-demo.yml上传到仓库,
这样客户端才能通过配置中心拉取到配置文件。
config-client-demo.yml内容:
spring:
profiles:
active:
- test
---
# 开发环境
spring:
profiles: dev
application:
name: config-server-dev
config:
info: config info dev
---
# 测试环境
spring:
profiles: test
application:
name: config-server-test
config:
info: config info test
5.新增查询接口
ConfigClientController.java对外提供config.info参数的查询,
config.info是从配置中心读取的。
package com.yuwen.spring.config.client.controller;
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("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
6.新增启动类
主启动类ConfigClientApplication.java,
只要加上SpringBootApplication注解:
package com.yuwen.spring.config.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
7.启动服务
Config Client服务启动日志如下,
注意要先启动Config Server服务:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-09-11 15:59:00.454 INFO 19236 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:9009
2020-09-11 15:59:01.868 INFO 19236 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client-demo, profiles=[dev], label=master, version=befaf0fdb3897ac7b7de2a8df3192decb0192cb7, state=null
2020-09-11 15:59:01.869 INFO 19236 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/config-client-demo.yml (document #1)'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/application.yml (document #1)'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/config-client-demo.yml (document #0)'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/application.yml (document #0)'}]
2020-09-11 15:59:01.874 INFO 19236 --- [ main] c.y.s.c.client.ConfigClientApplication : No active profile set, falling back to default profiles: default
2020-09-11 15:59:02.239 INFO 19236 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=7db33b17-4254-3471-a240-faf5a3bf9fa9
2020-09-11 15:59:02.660 INFO 19236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8001 (http)
2020-09-11 15:59:02.668 INFO 19236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-11 15:59:02.668 INFO 19236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-09-11 15:59:02.750 INFO 19236 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-11 15:59:02.750 INFO 19236 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 865 ms
2020-09-11 15:59:02.847 INFO 19236 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-11 15:59:03.248 INFO 19236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8001 (http) with context path ''
2020-09-11 15:59:03.531 INFO 19236 --- [ main] c.y.s.c.client.ConfigClientApplication : Started ConfigClientApplication in 4.184 seconds (JVM running for 4.49)
2020-09-11 15:59:30.056 INFO 19236 --- [nio-8001-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-11 15:59:30.056 INFO 19236 --- [nio-8001-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-09-11 15:59:30.060 INFO 19236 --- [nio-8001-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
8.测试服务
访问Config Client提供的Rest服务:
http://localhost:8001/configInfo
修改bootstrap.yml中spring.cloud.config.profile为dev,
然后重新启动,再次访问上面的接口:
可以看到两次分别拉取到了config-client-demo.yml中对应的配置。
9.参考文章
SpringCloud创建Config Client配置读取的更多相关文章
- SpringCloud创建Config Client通过Eureka访问Config
1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...
- SpringCloud创建Config读取本地配置
1.说明 Config Server获取配置支持的方式很多, 包括Git仓库(github/gitee等),任何与JDBC兼容的数据库, Subversion,Hashicorp Vault,Cred ...
- SpringCloud创建Eureka Client服务注册
1.说明 本文详细介绍微服务注册到Eureka的方法, 即Eureka Client注册到Eureka Server, 这里用任意一个Spring Cloud服务为例, 比如下面已经创建好的Confi ...
- SpringCloud创建Config模块
1.说明 本文详细介绍Spring Cloud创建Config模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 创建Config模块这个子工 ...
- SpringCloud创建Config多客户端公共配置
1.说明 基于已经创建好的Spring Cloud配置中心, 在配置中心仅保存一套配置文件, 多个客户端可以通过配置中心读取到相同的配置, 而不需要在每个客户端重复配置一遍, 下面以一个Config ...
- java框架之SpringCloud(7)-Config分布式配置中心
前言 分布式系统面临的配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中标会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...
- springcloud config配置读取优先级
情景描述 最近在修复Eureka的静态页面加载不出的缺陷时,最终发现是远程GIT仓库将静态资源访问方式配置给禁用了(spring.resources.add-mappings=false).虽然最后直 ...
- 创建客户端项目并读取服务化的配置中心(Consul + Spring Cloud Config)
创建客户端项目并读取服务化的配置中心 将配置中心注册到服务中心(Consul) POM文件添加依赖: <dependency> <groupId>org.springframe ...
- SpringCloud学习系列之四-----配置中心(Config)使用详解
前言 本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程. SpringCloud Config Config 介绍 Spring Clou ...
随机推荐
- shell脚本下载网页图片
和大家分享一个shell脚本写的图片抓取器.使用方法:img_downloader.sh.使用时在shell下输入:./img_downloader.sh www.baidu.com -d image ...
- 【Spring Framework】Spring入门教程(六)Spring AOP使用
Spring的AOP 动态代理模式的缺陷是: 实现类必须要实现接口 -JDK动态代理 无法通过规则制定拦截无需功能增强的方法. Spring-AOP主要弥补了第二个不足,通过规则设置来拦截方法,并对方 ...
- js调用高德地图API获取地理信息进行定位
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=(需要自 ...
- 【Java基础】Java中如何获取一个类中泛型的实际类型
泛型的术语 <>: 念做typeof List<E>: E称为类型参数变量 ArrayList<Integer>: Integer称为实际类型参数 ArrayLis ...
- Jenkins代码检查
目录 一.静态代码分析 二.规范检查 PMD进行检查 分析器区别 三.持续代码质量检测 Maven与SonarQube集成 Jenkins与SonarQube集成 代码扫描 SonarQube集成p3 ...
- 学Java,Java书籍的最佳阅读顺序
疫情以来,好久没出差了,今天出差去趟上海,早上 4 点多就起床了,到机场天都没亮.到登机口离起飞还一小时,趁着等飞机的时间,抓紧码字,把这篇文章收个尾. 今天和大家说说学 Java 的读书路线.路线中 ...
- 安装xampp开发环境更改默认项目路径
xampp开发环境中默认的项目路径在xampp下的htdocs文件下 如果想修改默认项目的位置步骤如下: 1)D:\xampp\apache\conf 找到httpd.conf打开 2)找到 Docu ...
- CF415A Mashmokh and Lights 题解
Content 有 \(n\) 个灯,一开始都是亮着的. 有 \(m\) 次操作,每次操作按下开关 \(x\),按下之后所有编号 \(\geqslant x\) 的灯全部熄灭.问你所有的灯第一次被熄灭 ...
- mpstat 查看多核CPU负载状态
mpstat是Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有 ...
- signal 信号
python学习笔记--信号模块signal 阅读目录(Content) 1 signal基本信号名 2 常用信号处理函数 2.1 设置发送SIGALRM信号的定时器 2.2 设置信号处理函数 3 常 ...