config-server-bus动态更新配置
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。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2013-12-05 我心中的核心组件(可插拔的AOP)~第六回 消息组件~续
2013-12-05 基础才是重中之重~Data层如何调用BLL层的方法,如果觉得奇怪请看本文章
2012-12-05 Html内容超出标记宽度后自动隐藏