config-server用来搭建配置中心,而配置信息一般使用gitlab仓库来存储,这样在你的配置发生改变时,不需要从新打包,而如果使用native的试,则需要从新打一个config-server的jar包。

配置的热更新

当你的服务的配置信息发生改变时,一般来说需要从新重启你的服务,配置信息才能生效,这对于我们来说是不友好的,所以springcloud有一种消息总线的方式来实现配置信息的热更新,更你的服务不需要从新启动。

项目搭建

eureka-server

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

程序添加注解

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

yml文件

server:
port: 8761 eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false #自我保护机制
eviction-interval-timer-in-ms: 30000 #及时踢出已关停的节点

config-server

它是配置中心,其它服务如果通过config-server在eureka里的服务名去连接它,这种是以eureka为核心;也可以单独指定,并把eureka的信息写到config-server仓库里,这是以config-server为核心,这两种方式都可以,咱们这个例子是以eureka为核心的,所以config-server也是一个eureka-client.

<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>

程序添加注解

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

添加yml配置

spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git@github.com:***/config_repo.git
username: ***
password: ***
searchPaths: '{profile}'
server:
port: 8888
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/

config-client-service1

这是我们具体的业务服务,它是一个eureka-client也是一个config-client,它需要把自己注册到eureka里,也需要从config-server拉自己的信息,它需要有配置信息的热更新,所以这需要引用amqp包和actuator健康检测包。

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

程序添加注解

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class Service1Application { @Value("${auth.name:empty}")
String author; public static void main(String[] args) {
SpringApplication.run(Service1Application.class, args);
} @GetMapping("/hello")
public String hello() {
return author;
}
}

添加yml文件

spring:
cloud:
bus.trace.enabled: true #配置动态更新
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
config:
discovery:
enabled: true #这块表示启用service-id不用uri
service-id: config-server #这块是服务的id
label: master
profile: svt
application:
name: service1
server:
port: 8081
eureka:
instance:
prefer-ip-address: true #基于IP地址的注册而不是主机名
client:
service-url:
defaultZone: http://localhost:8761/eureka
logging:
level:
org:
springframework:
security: DEBUG
management:
endpoints:
web:
exposure:
include: bus-refresh

配置更新

当你的config_repo仓库有文件更新时,你可以调用任意一个服务去触发它,测试的代码如

 curl -v -X POST "http://localhost:8081/actuator/bus-refresh"

如果成功后,一般会返回httpstatuscode:204的状态码,当然这种也是手动更新,如果希望动态更新,可以在gitlab或者github上对config_repo项目添加webhook的事件,当有分支合并到dev或者master时,去自动触发bus-refresh。

config-server-bus动态更新配置的更多相关文章

  1. 带你入门SpringCloud 之 通过SpringCloud Bus 自动更新配置

    前言 在<带你入门SpringCloud统一配置 | SpringCloud Config>中通过 SpringCloud Config 完成了统一配置基础环境搭建,但是并没有实现配置修改 ...

  2. webhooks动态更新配置

    config server 项目中加入 monitor依赖 <dependency> <groupId>org.springframework.cloud</groupI ...

  3. Spring Cloud Bus 自动更新配置

    ---恢复内容开始--- Spring Cloud Config 结合 Spring Cloud bus 实现 git 仓库提交配置文件 触发消息队列 应用自动更新配置 1. config 服务端 添 ...

  4. Spring Cloud(八):使用Spring Cloud Bus来实现配置动态更新

    使用Spring Cloud Config我们能实现服务配置的集中化管理,在服务启动时从Config Server获取需要的配置属性.但如果在服务运行过程中,我们需要将某个配置属性进行修改,比如将验证 ...

  5. 玩转Spring Cloud之配置中心(config server &config client)

     本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1.2.svn方式 1.3.本地文件方式 1.4.解决配置中包含中文内容返回乱码问题 二.搭建配置消费客户端( ...

  6. 微软Azure配置中心 App Configuration (三):配置的动态更新

    写在前面 我在前文: <微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core>已经介绍了Asp.net Core怎么轻易的接入azure ...

  7. Spring Cloud Config、Apollo、Nacos配置中心选型及对比

    Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...

  8. 9.Spring Cloud Config统一管理微服务配置

    Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...

  9. [RPC学习]Dubbo+nacos实现动态更新内存RTree

    1.背景 服务架构一般都是从 单体架构 -> 微服务架构 -> 分布式架构 的迭代,我上一家公司就是在业务发展到一定规模时,开始拆老的单体服务,按业务维度拆成多个微服务,服务之间用的是HT ...

随机推荐

  1. [UWP]使用CompositionGeometricClip裁剪复杂图形及进行动画

    1. UWP中的其它裁剪方案 之前在 这篇文章 里,我介绍了如何使用UIElement.Clip裁剪UIElement的内容,使用代码如下: <Canvas> <Image Sour ...

  2. iNeuOS 工业互联网 从网关到云端一体化解决方案。教你如何做PPT。

          iNeuOS 专注打造云端操作系统,提供全新解决方案   (凑够150字) 核心组件包括:边缘网关(iNeuLink).设备容器(iNeuKernel).视图建模(iNeuView).机器 ...

  3. 宜信SDL实践:产品经理如何驱动产品安全建设

    一.序言 本文从产品经理的角度出发,对产品经理的安全职责.产品驱动安全的内涵.工作内容.工作方法.所需安全资源.以及产品经理的安全工作量进行了分析.希望所有产品经理在没有心理负担的情况下,有目标.有方 ...

  4. 前后台分离的 NET Core 通用权限管理系统

    前言 从事软件开发这个行业现在已经有十几年了,项目无论大小权限认证.授权模块总是或多或少有功能需求的,这一块费时费力但是又存在于后台,使用最多的可能是运维人员所以处于出力不讨好的尴尬地位,每次有新的项 ...

  5. P1387 最大正方形 |动态规划

    题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入格式 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m个数字,用空格隔 ...

  6. PG数计算

    PG数计算 原地址:http://xiaqunfeng.cc/2017/09/15/too-many-PGs-per-OSD/ ceph告警问题:”too many PGs per OSD” 的解决方 ...

  7. imagenet-vgg-verydeep-19.mat格式详解

    .mat是matlab生成的文件.用matlab打开文件imagenet-vgg-verydeep-19.mat可以帮助理解其结构.matlab代码如下: a = open('D:\imagenet- ...

  8. 【CSS】378- [译]44个 CSS 精选知识点

    写在前面 一个周五的晚上,闲来无事整理下自己的 github(经常做收藏党),今天打算都过一遍,发现一个 star很高的项目,里面有大量的 CSS 片段,而且标题很诱人,然后又花了将近1个小时从头到尾 ...

  9. 【集合系列】- 深入浅出分析 ArrayDeque

    一.摘要 在 jdk1.5 中,新增了 Queue 接口,代表一种队列集合的实现,咱们继续来聊聊 java 集合体系中的 Queue 接口. Queue 接口是由大名鼎鼎的 Doug Lea 创建,中 ...

  10. SpringBoot系列之集成logback实现日志打印(篇二)

    SpringBoot系列之集成logback实现日志打印(篇二) 基于上篇博客SpringBoot系列之集成logback实现日志打印(篇一)之后,再写一篇博客进行补充 logback是一款开源的日志 ...