------------恢复内容开始------------

SpringCloud Config 配置中心

Config 配置中心

Spring Cloud Config为分布式系统中的外部化配置提供服务器端和客户端支持。使用Config Server,您可以在中心位置管理所有环境中应用程序的外部属性。客户端和服务器上的概念都与Spring EnvironmentPropertySource抽象映射相同,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。在应用程序从开发人员到测试人员再到生产人员的整个部署过程中,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持带标签的配置环境版本,并且可以通过各种工具来访问这些内容来管理内容。添加替代实现并将其插入Spring配置很容易。

Config配置中心实例

Config-Server

1.新建一个配置中心服务 springcloud-config-6001,并添加依赖

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--配置中心服务需要添加进注册中心,所以要加eureka-client依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>

2.创建配置文件 application.yml

server:
port: 6001 spring:
application:
name: springcloud-config
cloud:
config:
server:
git:
uri: https://gitee.com/little_gofy/config-server.git #git仓库的html地址
#如果配置文件放在子目类下,则需在search-paths配置添加子目录路径
search-paths:
#如果仓库为私有,则需在username和password配置添加用户名和密码
username:
password: eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/

3.在启动类上添加注解@EnableConfigServer,允许配置服务

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}

4.在git仓库创建配置文件application.yml,随便添加点配置

spring:
profiles: dev
application:
name: config-dev
my:
name: gofy-dev ---
spring:
profiles: test
application:
name: config-test
my:
name: gofy-test

开启注册中心服务和配置中心服务, 访问 localhost:6001/application-dev.ymllocalhost:6001/application-test.yml, 返回git仓库配置文件对应模式的配置.

HTTP服务具有以下形式的资源:

# application:配置文件名, profile:配置的模式, label:配置文件所在分支
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

如果报以下异常, 把 spring-cloud-config-server 降到 2.1.0.RELEASE 即可

NoClassDefFoundError: org/springframework/cloud/config/environment/PropertyValueDescriptor

Config-Client

1.创建一个客户端 springcloud-config-client, 并添加依赖

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>

2.创建 bootstrap.yml

spring:
cloud:
config:
discovery:
service-id: springcloud-config #指定配置中心,对应配置中心服务名
enabled: true #开启配置信息发现
label: master #配置文件所在分支, 默认为master
profile: dev #配置的模式, 多个用逗号分隔 eureka:
client:
service-url:
defaultZone: http://server1:7001/eureka/
register-with-eureka: false

再创建一个 application.yml

server:
port: 80 spring:
application:
name: config-client

注意, 不要把bootstrap.yml的配置写在application.yml里, 因为bootstrap.yml的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。需要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.yml,不然客户端是无法获取配置中心参数的,会启动失败!

3.创建Controller调用配置中心的配置

@RestController
public class ConfigController {
@Value("${my.name}") //注入配置中心里的配置文件的my.name值
private String myName; @RequestMapping("/my")
public String getMyName(){
return myName;
}
}

4.创建启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigClient {
public static void main(String[] args) {
SpringApplication.run(ConfigClient.class, args);
}
}

启动注册中心、配置中心服务和客户端, 访问 localhost/my , 返回的是dev模式配置的值gofy-dev .

当我们把bootstrap.yml里的profile值改为test, 则返回的是test模式配置的值gofy-test .

如果报以下异常, 是因为配置中心服务还没有注册到注册中心, 等一会再启动客户端就行了. 如果还报错, 请检查配置是否和上面的配置一致.

java.lang.IllegalStateException: No instances found of configserver (springcloud-config)

修改Config-Server本地仓库位置

使用基于VCS的后端(git,svn),文件被检出并克隆到本地文件系统。默认情况下,它们以config-repo-为前缀放在系统临时目录中。例如,在Linux上,它可能是/tmp/config-repo-,在Window上,它是/Temp/config-repo-。一些操作系统通常会清除临时目录。这可能导致意外行为,例如缺少属性。

为避免此问题,请通过将spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir设置为不在系统临时结构中的目录来更改Config Server使用的目录。

spring:
cloud:
config:
server:
git:
basedir: E:/local-config-repo

我的个人博客站

SpringCloud Netflix (六):Config 配置中心的更多相关文章

  1. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  2. SpringCloud的入门学习之概念理解、Config配置中心

    1.SpringCloud Config分布式配置中心.分布式系统面临的配置问题. 答:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个 ...

  3. SpringCloud之Config配置中心+BUS消息总线原理及其配置

    一.配置中心作用 在常规的开发中,每个微服务都包含代码和配置.其配置包含服务配置.各类开关和业务配置.如果系统结构中的微服务节点较少,那么常规的代码+配置的开发方式足以解决问题.当系统逐步迭代,其微服 ...

  4. 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  5. SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版

    前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心( ...

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

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

  7. springcloud之config配置中心-Finchley.SR2版

    本篇和大家分享的是springcloud-config配置中心搭建,写到这里突然想起自己曾今开源过基于Redis发布订阅编写的一个配置中心,刚看了git星数有点少哈哈,这里顺势发个连接欢迎大侠们点赞: ...

  8. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  9. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

随机推荐

  1. STL下<algorithm>下的sort函数

    定义: sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序.sort函数进行排序的时间复杂度为nlog2n,比冒泡之类的排序算法效率要高,sort函数包含在头文件为#i ...

  2. 201771030117-祁甜 实验一 软件工程准备—<阅读《现代软件工程——构建之法》提出的三个问题>

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/nwnu2020SE 这个作业要求链接 https://www.cnblogs.com/nwnu- ...

  3. 学习RxJava+Retrofit+OkHttp+MVP的网络请求使用

    公司的大佬用的是这一套,那我这个菜鸟肯定要学习使用了. 我在网上找了很多文章,写的都很详细,比如 https://www.jianshu.com/u/5fd2523645da https://www. ...

  4. 【Spark】RDD(Resilient Distributed Dataset)究竟是什么?

    目录 基本概念 官方文档 概述 含义 RDD出现的原因 五大属性 以单词统计为例,一张图熟悉RDD当中的五大属性 解构图 RDD弹性 RDD特点 分区 只读 依赖 缓存 checkpoint 基本概念 ...

  5. Linux Kernel Makefiles Kbuild en

    来自Linux kernel docs,顺便整理了一下排版 Linux Kernel Makefiles This document describes the Linux kernel Makefi ...

  6. DotNet:Socket Server 异步套接字服务端实现

    异步服务器套接字示例 From https://msdn.microsoft.com/zh-cn/library/fx6588te(v=vs.110).aspx 下面的示例程序创建接收来自客户端的连接 ...

  7. FPGA六位共阳极数码管动态显示

    `timescale 1ns/1ps module adc_dis( clk , rst_n , sm_seg , sm_bit ); input clk;//50HZ input rst_n; :] ...

  8. select 标签的数据绑定

    修改数据的页面 进入页面绑定select的值 会value绑定但是没有显示相应的option <script> $("#id option[value=${item.decora ...

  9. 计算python内部数据结构时间效率-源代码

    #Author:qinjiaxi '''本程序计算各种循环的时间效率''' from timeit import Timer def test1(n): li = [] for i in range( ...

  10. tpcc-mysql 试用

    percona 出的一个mysql压力测试工具,至于tpcc的话,是一个衡量事务处理能力的一个值.具体可以看老外对他的定义. http://www.tpc.org/tpcc/results/tpcc_ ...