配置中心的作用就在于可以在项目启动时加载远程或本地的配置文件,将配置文件集中管理

springboot版本:

2.1.6.RELEASE

springcloud版本:

Finchley.RELEASE

一、注册中心

 1、依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>

 2、配置文件

server.port=8090
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/

 配置说明

  • eureka.client.register-with-eureka:表示是否将自己注册到Eureka Server,默认是true。
  • eureka.client.fetch-registry:表示是否从Eureka Server获取注册信息,默认为true。

  接下来只需要在主启动类上加上@EnableEurekaServer注解就万事大吉了

二、配置中心服务端:

 1、依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 2、配置文件

server.port=8050
spring.application.name=config-server
# 注册到服务注册中心
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
# github的仓库地址
spring.cloud.config.server.git.uri=https://github.com/kurean/springconfig.git
# github的文件路径
spring.cloud.config.server.git.searchPaths=repo
# github的分支,默认是master
spring.cloud.config.label=master

 最后只要在主启动类上添加@EnableConfigServer和@EnableEurekaClient后就可以了

说明:

  1、如果在GitHub上建立的仓库是私有的,那么还要加上spring.cloud.config.server.git.username和spring.cloud.config.server.git.password 这两个配置

  2、springcloud config 的URL与配置文件的映射关系如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

  3、如果github上建立的目录下的文件为configtest-dev.properties,那么当启动配置中心服务器端时,可以通过http://localhost:8050/configtest/dev/master访问配置文件,如果访问成功则表示配置中心搭建成功

三、配置中心客户端

 1、依赖

<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>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 2、配置文件

  创建bootstrap.properties文件,这时项目启动时会通过这个文件从配置中心服务端中设置的配置文件存放地址加载指定的配置文件,因为这个文件是优先于application.properties文件加载的,因此指定注册中心的配置需要放在前者中,否则会造成启动失败

spring.cloud.config.name=configtest  #配置文件名称
spring.cloud.config.profile=dev    #读取后缀名称
spring.cloud.config.label=master #分支名称
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
spring.application.name=config-client
server.port=9006

当然,端口号和服务名字可以放在application.properties中

配置说明:

  • spring.cloud.config.name和spring.cloud.config.profile的值结合就是GitHub上上传的配置文件名,中间用"-"隔开,至于是.properties还是.yml取决于你在本地项目中使用的配置文件是前者还是后者
  • 如果项目中没有使用注册中心,那么spring.cloud.config.discovery.serviceId和spring.cloud.config.discovery.enabled就要换成spring.cloud.config.uri来指定配置中心服务端的地址
  • 如果项目中使用了注册中心,但是通过uri的方式访问配置中心,那么为了防止本身服务不注册进注册中心,需要在主启动类上加上@EnableDiscoveryClient或@EnableEurekaClient这一服务发现和注册注解

这时就可以创建一个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("${cs.name}")
String name;
@Value("${cs.age}")
String age; @GetMapping(value = "/hi")
public String hi(){
return "我的名字是:"+name+",年龄是:"+age;
}
}

github上的配置文件

configtest-dev.properties

cs.name=kevin
cs.age=18

测试效果

配置动态刷新之手动版

修改配置中心客户端

添加依赖

 <!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

修改bootstrap.properties

spring.cloud.config.name=configtest
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
management.endpoints.web.exposure.include=refresh

在controller上添加@RefreshScope注解

这时当修改了GitHub上文件后,需要手动刷新客户端配置,发送post请求“http://localhost:9006/actuator/refresh”,使用postman或curl命令(curl -X POST "http://localhost:9006/actuator/refresh")

此时客户端配置文件已经同步修改,重新测试效果

最后,如果遇见Eureka Client启动后就关闭日志打印“ Unregistering application xxx with eureka with status DOWN”,多半是因为本身项目作为web项目启动时没有导入web依赖所致,只要加入就好了

参考文章:https://blog.csdn.net/qazwsxpcm/article/details/88578076

SpringCloud之配置中心(config)的使用的更多相关文章

  1. 七、springcloud之配置中心Config(二)之高可用集群

    方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...

  2. SpringCloud分布式配置中心Config

    统一管理所有配置. 1.微服务下的分布式配置中心 简介:讲解什么是配置中心及使用前后的好处 什么是配置中心: 一句话:统一管理配置, 快速切换各个环境的配置 相关产品: 百度的disconf 地址:h ...

  3. 六、springcloud之配置中心Config

    一.配置中心提供的核心功能 Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对S ...

  4. springcloud(四):应用配置中心config的安全设置

    springcloud应用配置中心config的安全设置 在springcloud应用开发中,为了方便在线管理我们的配置文件,通常会配一个配置中心config-server,这里托管着应用的一些配置文 ...

  5. Spring-cloud微服务实战【九】:分布式配置中心config

      回忆一下,在前面的文章中,我们使用了spring cloud eureka/ribbon/feign/hystrix/zuul搭建了一个完整的微服务系统,不管是队内还是对外都已经比较完善了,那我们 ...

  6. SpringCloud配置中心config

    1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config Apollo 实现分布式配置中心 zookeeper:实现分布式配置中心, ...

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

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

  8. SpringCloud 分布式配置中心

    SpringCloud 分布式配置中心 服务端 创建工程并完善结构 国际惯例,把maven工程创建完善 pom.xml <?xml version="1.0" encodin ...

  9. (七)Spring Cloud 配置中心config

      spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...

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

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

随机推荐

  1. [cocos2d-x]关于动画

    声明一下:看见这篇文章总结的已经非常好了,没必要再去自己到处东翻西找了,链接:http://shahdza.blog.51cto.com/2410787/1546998 [唠叨] 基本动画制作需要用到 ...

  2. Hexo博客搭建记录

    Hexo博客搭建记录 参考视频:手把手教你从0开始搭建自己的个人博客 |无坑版视频教程 以下命令操作建议使用管理员权限完成 1. nodejs & hexo 安装 1.首先下载node.js, ...

  3. 从log4j切换到logback后项目无法启动

    1.背景 有个旧项目之前使用的是log4j2来打印日志的,因为某些原因,同事想换成logback. 换成logback改动也很简单,大致就一下2步: 删除log4j2.xml配置,新增logback. ...

  4. Ubuntu 安装 SSH

    sudo apt install openssh-server sudo systemctl status ssh sudo ufw allow ssh

  5. yarn使用 以及和npm对比

    yarn是facebook发布的一款取代npm的包管理工具. yarn的特点: 速度超快. Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载. 同时利用并行下载以最大化资源利用率,因此安装速 ...

  6. 让人眼前一亮的应用「GitHub 热点速览」

    大开眼界的一期 GitHub 热门项目,类似 Django 存在的 pynecone,搞定 Windows.Office 激活的 Microsoft-Activation-Scripts,都让我的收藏 ...

  7. 系列化和反序列化的概述-对象的序列化_Object Output Stream类

    系列化和反序列化的概述 Java提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存了 ...

  8. By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5", but CMake did not find one.

    环境 qt5.12.3  deepin15.10 cmake构建 由于之前使用的是仓库自带的qt环境,后来需要更高版本qt,于是从官网下载安装器自己安装,重新构建之后便出现这个问题,具体报错如下 CM ...

  9. 完整工作流整合方案,自定义配置,Java+Vue+Activiti@附配套文档

    前言 activiti工作流引擎项目,企业erp.oa.hr.crm等企事业办公系统轻松落地,一套完整并且实际运用在多套项目中的案例,满足日常业务流程审批需求. 一.项目形式 springboot+v ...

  10. Linux密钥认证

    网站集群批量管理-秘钥认证 一.概述 管理更加轻松:两个节点,通过秘钥形式进行访问,不需要输入密码,单向 应用场景: 一些服务在使用前要求做秘钥认证 手动写批量管理脚本 名字:秘钥认证,免密码登录,双 ...