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.参考文章

Spring Cloud Config Server 官方手册
Spring Cloud Config 中文手册

SpringCloud创建Config Client配置读取的更多相关文章

  1. SpringCloud创建Config Client通过Eureka访问Config

    1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...

  2. SpringCloud创建Config读取本地配置

    1.说明 Config Server获取配置支持的方式很多, 包括Git仓库(github/gitee等),任何与JDBC兼容的数据库, Subversion,Hashicorp Vault,Cred ...

  3. SpringCloud创建Eureka Client服务注册

    1.说明 本文详细介绍微服务注册到Eureka的方法, 即Eureka Client注册到Eureka Server, 这里用任意一个Spring Cloud服务为例, 比如下面已经创建好的Confi ...

  4. SpringCloud创建Config模块

    1.说明 本文详细介绍Spring Cloud创建Config模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 创建Config模块这个子工 ...

  5. SpringCloud创建Config多客户端公共配置

    1.说明 基于已经创建好的Spring Cloud配置中心, 在配置中心仅保存一套配置文件, 多个客户端可以通过配置中心读取到相同的配置, 而不需要在每个客户端重复配置一遍, 下面以一个Config ...

  6. java框架之SpringCloud(7)-Config分布式配置中心

    前言 分布式系统面临的配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中标会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...

  7. springcloud config配置读取优先级

    情景描述 最近在修复Eureka的静态页面加载不出的缺陷时,最终发现是远程GIT仓库将静态资源访问方式配置给禁用了(spring.resources.add-mappings=false).虽然最后直 ...

  8. 创建客户端项目并读取服务化的配置中心(Consul + Spring Cloud Config)

    创建客户端项目并读取服务化的配置中心 将配置中心注册到服务中心(Consul) POM文件添加依赖: <dependency> <groupId>org.springframe ...

  9. SpringCloud学习系列之四-----配置中心(Config)使用详解

    前言 本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程. SpringCloud Config Config 介绍 Spring Clou ...

随机推荐

  1. ganglia -api

    setup 命令: virtualenv ve source ve/bin/activate pip install -r requirements.txt python ganglia/gangli ...

  2. 手写Starter

    一. Starter工程的命名 Spring 官方定义的Starter通常命名遵循的格式为spring-boot-starter-{name},例如 spring-boot-starter-web.S ...

  3. table表格数据无缝循环滚动

    分享一个好看的表格无缝滚动:(实战用起来很舒服) 直接copy代码到你的程序中: 1.HTML <div class="tablebox">              ...

  4. 【Java 基础】Java Enum

    概览 在本文中,我们将看到什么是 Java 枚举,它们解决了哪些问题以及如何在实践中使用 Java 枚举实现一些设计模式. enum关键字在 java5 中引入,表示一种特殊类型的类,其总是继承jav ...

  5. springmvc中的异常处理方法

    //1.自定义异常处理类       2.编写异常处理器    3.配置异常处理器 package com.hope.exception;/** * 异常处理类 * @author newcityma ...

  6. 12月第二周bug总结

    1.bug总结 复制 别人的依赖和依赖指定类型 报错 解决:依赖还没加载完成,你就指定了版本型号,所以报错,所以先让他加载依赖,后指定该型号 eureka(优瑞卡) 注册服务 控制台没有显示出来的话 ...

  7. 二级C复习

    二级C语言 队列 计算队列中元素个数 种 : rear > front ,直接减 第二种: rear < front 上面两种综合一起,求元素个数公式 :(r - f + maxsize) ...

  8. [Java Web 王者归来]读书笔记1

    第一章 Java web 开发概述 1 WEB服务器运行时一直在TCP 80(默认端口)监听, 若使用其他端口在url中需要显示标注端口号(例如:8080) 2 WEB服务器:微软的IIS.Apach ...

  9. 【二进制】CTF-Wiki PWN里面的一些练习题(Basic-ROP篇)

    sniperoj-pwn100-shellcode-x86-64 23 字节 shellcode "\x31\xf6\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\ ...

  10. 成本计划的输出(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 所谓"输出"就是把数据摆出来让人看,好吧,这种"看",可以直接在屏幕上看,也可以打 ...