SpringCloud配置中心config
1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config
zookeeper:实现分布式配置中心,主要是通过持久节点存储配置信息加上事件通知
Apollo:实现分布式配置中心,主要是通过mysql 数据库存储配置信息,通过有图形界面可以管理配置文件信息
srpingcloud config: 实现分布式配置中心,主要是通过版本控制器(git/svn)存储配置文件信息,没有后台
2,搭建springcloud-config 分布式配置中心

1,搭建码云(git),开源的git 服务器,创建账号,创建仓库,创建文件
https://gitee.com/aiyuesheng/springcloud-config/blob/master/config/config-client-dev.properties
这个放在git 上的配置文件信息,有参数age=10333
配置文件名:config-client-dev.properties
配置文件名称规范:服务名-环境.properteis 例如开发环境是:config-client-dev.properties,生产环境:config-client-prd.properties
3, 搭建eureka 注册中心,这个之前已经写过,需要将springcloud-config 以及 配置中心的客户端注册上去
最后就是以下三个服务都起来,然后springcloud-config-client 通过springcloud-server 读取git 上的配置文件

4,搭建springcloud-config(配置中心)
maven依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> </dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
application.yml: springcloud-config(配置中心) 这个服务注册到eureka 上的别名:config-server, git环境地址,以及文件夹搜索配置文件信息都有
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
spring:
application:
####注册中心应用名称
name: config-server
cloud:
config:
server:
git:
###git环境地址 仓库
uri: https://gitee.com/aiyuesheng/springcloud-config.git
####搜索目录
search-paths:
- config
####读取分支
label: master
####端口号
server:
port: 8888
启动类:
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigApp {
public static void main(String[] args) {
SpringApplication.run(ConfigApp.class, args);
}
}
5,搭建springcloud-client 端,也就是正常的服务,需要从配置中心上面读取配置文件信息
maven:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- actuator监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
配置文件信息:name:应用名称,如果git 上没有这个应用开头的名字,会找不到,profile:dev:git 上配置文件的环境名,service-id:config-server, 是配置中心服务器注册到eureka 上的别名
spring:
application:
####注册中心应用名称
name: config-client
cloud:
config:
####读取后缀
profile: dev
####读取config-server注册地址
discovery:
service-id: config-server
enabled: true
#####eureka服务注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
server:
port: 8882 #配置手动实时刷新
#managementendpoints.web.exposure.include=*
management:
endpoints:
web:
exposure:
include: "*"
测试类:
@Component
@Data
@RefreshScope
public class Parameter { @Value("${age}")
private String age; }
@RestController
public class IndexService { @Autowired
private Parameter parameter; @RequestMapping("/getAge")
private String getAge() {
System.out.println(parameter.getAge());
return parameter.getAge();
} }
启动类:
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApp { public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class, args);
} }
测试完,可以读取数据
5,刷新数据----手动刷新
如果git 上配置文件信息修改了,本地缓存的配置文件信息是不会立即刷新的。在上面的例子中,我将读取的参数、,放入到了一个单独的Parameter 中,加上了注解@RefreshScope,同时,需要客户端需要加上actuator 监控中心
<!-- actuator监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
同时,配置文件增加吗,每个接口都实时监控:
#配置手动实时刷新
#managementendpoints.web.exposure.include=*
management:
endpoints:
web:
exposure:
include: "*"
每次修改完毕,需要手动发送post 请求,我是在post man 上发送
http://127.0.0.1:8882/actuator/refresh
如果有更新,会有提示,没有,则返回【】
再次刷新,配置文件信息,就刷新了
本来,我是将@RefreshScope 放入到了控制层的类上,但是就是读取为null ,很坑。。。。。。
实时刷新采用SpringCloud Bus消息总线,可以实时刷新,但是影响性能。。。
SpringCloud配置中心config的更多相关文章
- SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版
前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心( ...
- 七、springcloud之配置中心Config(二)之高可用集群
方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...
- 六、springcloud之配置中心Config
一.配置中心提供的核心功能 Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对S ...
- springcloud(四):应用配置中心config的安全设置
springcloud应用配置中心config的安全设置 在springcloud应用开发中,为了方便在线管理我们的配置文件,通常会配一个配置中心config-server,这里托管着应用的一些配置文 ...
- Spring-cloud微服务实战【九】:分布式配置中心config
回忆一下,在前面的文章中,我们使用了spring cloud eureka/ribbon/feign/hystrix/zuul搭建了一个完整的微服务系统,不管是队内还是对外都已经比较完善了,那我们 ...
- 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- SpringCloud分布式配置中心Config
统一管理所有配置. 1.微服务下的分布式配置中心 简介:讲解什么是配置中心及使用前后的好处 什么是配置中心: 一句话:统一管理配置, 快速切换各个环境的配置 相关产品: 百度的disconf 地址:h ...
- (七)Spring Cloud 配置中心config
spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...
- SpringCloud配置中心实战
SpringCloud配置中心实战 1.统一配置中心(Config) 1.1 Spring项目配置加载顺序 1.2 配置规则详解 1.3 Git仓库配置 1.3.1 使用占位符 1.3.2 模式匹配 ...
随机推荐
- NOI Online 赛前刷题计划
Day 1 模拟 链接:Day 1 模拟 题单:P1042 乒乓球 字符串 P1015 回文数 高精 + 进制 P1088 火星人 搜索 + 数论 P1604 B进制星球 高精 + 进制 D ...
- Chrome 插件安装时报错: "CRX_HEADER_INVALID"
1. 将 crx 插件后缀名改为 rar 2. 解压 rar 包 3. 进入浏览器 1). 点击Chrome浏览器地址栏最右边的三个点 --> 更多工具 --> 扩展插件 --> 点 ...
- C与ARM汇编结合实现mini2440串口uart简单程序
最近学完了ARM的一些基础知识,开始在mini2440上开发一些简单的程序,串口发送程序是一开始涉及多个寄存器的例子,稍有繁多的步骤应该是开发过程中要慢慢适应的境况 下面的程序的目的是实现mini24 ...
- Java多态实现的机制
Java提供了编译时多态和运行时多态两种多态机制.前者是通过方法重载实现的,后者是通过方法的覆盖实现的. 在方法覆盖中,子类可以覆盖父类的方法,因此同类的方法会在父类与子类中有着不同的表现形式. 在J ...
- 小白的springboot之路(十六)、mybatis-plus 的使用
0-前言 mybatis plus是对mybatis的增强,集成mybatis plus后,简单的CRUD和分页就不用写了,非常方便,五星推荐: 1-集成 1-1.添加依赖 <!-- .集成my ...
- python小白入门
阅读目录 一python介绍 二安装python解释器 三第一个python程序 四变量 五用户与程序交互 六基本数据类型 七格式化输出 八基本运算符 九流程控制之if...else 十流程控制之wh ...
- C++ const用法,看这一篇就够了!
本文主要介绍const修饰符在C++中的主要用法,下面会从两个方面进行介绍:类定义中使用const.非类定义中使用const 1. 非类定义中使用const 非类定义中使用const是指:在除了类定义 ...
- 2020年ubuntu sever1804 安装和配置
最后一次折腾linux服务器,应该是13的我的VPS.因为转行后,没有及时关注vps续费的问题,结果过期,所有的数据丢失了 当时觉得,反正都不做了,丢了就丢了吧,可现在想起来,实在是太后悔了. 今天, ...
- 使用 ALSAlib 播放 wav
在 ARM 2440 开发板上正常播放 16bit 44100 采样率的wav , 为了程序简单,没有判断返回值. 补充,在 ubunto 上也能正常播放. 编译方法: arm-linux-gcc ...
- 基于Redis未授权访问的挖矿蠕虫分析
0x01 攻击方式 利用的是通用漏洞入侵服务器并获得相关权限,从而植入挖矿程序再进行隐藏. 通过对脚本的分析,发现黑客主要是利用 Redis未授权访问漏洞进行入侵.脚本里有个python函数. imp ...