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. Python中 * 与 **, *args 与 **kwargs的用法

    * 用于传递位置参数(positional argument) ** 用于传递关键字参数(keyword argument) 首先,先通过一个简单的例子来介绍 * 的用法: def add_funct ...

  2. 报错 Please make sure you have the correct access rights and the repository exists (git 添加ssh密钥 )

    1.设置Git的user name和email $ git config --global user.name "wubaiwan" $ git config --global u ...

  3. git 使用详解(10)-- 远程分支

    远程分支 远程分支(remote branch)是对远程仓库中的分支的索引.它们是一些无法移动的本地分支:只有在Git 进行网络交互时才会更新.远程分支就像是书签,提醒着你上次连接远程仓库时上面各分支 ...

  4. openstack网络(四)-虚机流量分析

    几种网络名词解释 使用LinuxBridge时虚机流量分析 VLAN FLAT Local VXLAN 使用OVS时虚机流量分析 几种网络名词解释 1.local网络:local网络是与其他网络和节点 ...

  5. [TimLinux] Linux shell获取进程pid

    调用脚本时,获取进程PID: (/this/is/a/script/file.sh > /out/to/log.txt & echo $!) & 脚本内部,获取进程PID: ec ...

  6. 2018 ACM/ICPC 南京 I题 Magic Potion

    题解:最大流板题:增加两个源点,一个汇点.第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1). 每个怪物连终点,边权为1: 参 ...

  7. Python之数据分析工具包介绍以及安装【入门必学】

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 首先我们来看 Mac版 按照需求大家依次安装,如果你还没学到数据分析,建议你 ...

  8. CSS3(2)--- 过渡(transition)

    CSS3(2)--- 过渡(transition) 一.概念 1.什么是过渡 通俗理解 是从一个状态 渐渐的过渡到 另外一个状态. 比如一个盒子原先宽度为100px,当鼠标点击时盒子的宽度变成200p ...

  9. python爬虫--爬虫介绍

    一 爬虫 1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样 2.互联网建立的目的? 互联网的核心价值在于数据的共享/传递:数据是存放于一台 ...

  10. python学习-os引入

    # 引入import os # 路径处理 -- 外部资源-os # 获取当前的工作路径workspace = os.getcwd() # os模块下的getcwd函数print(workspace) ...