上一节中的Config尽管解决了配置文件繁冗的问题,然而有一个弊病,那就是当咱们服务端代码批改后,也就是gitee上的货色批改后须要再次启动服务才可能使批改失效。
为了解决这个问题,SpringCloud推出了Bus,学过计网的同学有印象就会晓得总线这个货色,就相似播送一样,本篇文章也是SpringCloud入门系列的终章。
引入Bus
建设一个空父bus模块,批改父模块的pom文件,导入对应的依赖以供其子模块应用。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
看到spring-cloud-starter-bus-amqp
就晓得须要rabbitmq,在练手的状况个别我都利用wsl2应用docker,下载rabbitmq镜像并且开启,大家能够各显神通,反正能下载就行了。
判断是否下载胜利只须要拜访http://localhost:15672 ,呈现以下界面就是胜利
用户名和明码都是guest,登录进去后能看到以下界面
在bus父模块下首先建设一个bus-config-client9601
子模块。
配置文件
从服务端获取内容,所以得设置服务端的相干内容,也就是spring.cloud.config.xxx
,还须要设置bus,rabbitmq,actuator,eureka等相干内容,最终的配置文件如下:
server.port: 9601
spring:
application:
name: bus-config-client
cloud:
config:
profile: dev
label: master
uri: http://localhost:8101 #服务端地址
bus: #开启bus
enabled: true
trace:
enabled: true
rabbitmq: #设置rabbitmq的相干信息
host: localhost
port: 5672
username: guest
password: guest
management: #开启actuator
endpoints:
web:
exposure:
include: bus-refresh #开启主动刷新的api
eureka: #惯例设置eureka
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8001/eureka/
instance:
instance-id: bus-config-client9601
除此之外的配置文件都是通过conifg从服务端中间接获取
然而因为咱们又是另外一个我的项目,所以须要在gitee的仓库中创立一个bus-config-client-dev.properties
文件。
再次强调,配置文件名:{spring.application.name}-{profile}
主启动类
主启动类还是比较简单
@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope //开启主动刷新的注解
public class BusConfigClient9601 {
@Value("${name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(BusConfigClient9601.class, args);
}
//模仿业务类
@GetMapping("/get")
public String getName() {
return "hello, " + name;
}
}
测试
首先先简略测试一下,开启EurekaServer8001
服务、ConfigServer8101
服务、BusConfigClient9601
[9602(通过批改启动类配置文件)] 服务
拜访localhost:9601/get ,而后批改gitee中的配置文件,接着发送post申请
http://localhost:9601/actuator/bus-refresh ,再次拜访get接口。
在idea的控制面板界面也可能清晰地看到从新读取了文件
进阶刷新
方才的刷新其实是刷新了所有一个主线上的客户端,不过因为我的项目比拟少的缘故无奈体现,为此新建一个BusClient9701
的子模块。
主启动类和配置文件没啥好说的,根本是拷贝BusConfigClient9601
模块的
server.port: 9701
spring:
application:
name: bus-client
cloud:
config:
profile: dev
label: master
uri: http://localhost:8101
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
#eureka:
# client:
# fetch-registry: true
# register-with-eureka: true
# service-url:
# defaultZone: http://localhost:8001/eureka/
# instance:
# instance-id: bus-client9701
这里把eureka相干的给正文掉是为了阐明config的刷新和eureka无关
@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope //一样须要这个
public class BusClient9701 {
@Value("${name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(BusClient9701.class, args);
}
@GetMapping("/get")
public String getName() {
return "hello, " + name;
}
}
能够通过http://[ip地址:port端口号]/actuator/bus-refresh/[spring.application.nema微服务名称]
来刷新指定的微服务
只有是总线上的微服务都能够刷新,也就是说
ip:port
并没有限度
放弃下面开启的微服务不动,新开一个BusClient9701
服务 ,最终开启的服务如下
因为批改配置文件再拜访端口看后果的gif图比拟大,这里就看idea控制台的后果即可。
控制台中无关BusClient9701的输入可能会呈现报错或者正告,那是因为没有注册进Eureka,然而引入了Eureka。
刷新所有总线上的微服务,发送http://localhost:9701/actuator/bus-refresh post申请 。能够看到所有的微服务都再次获取服务端的资源。
刷新总线上指定名称的微服务 ,发送http://localhost:9701/actuator/bus-refresh/bus-config-client post申请来刷新名字为bus-config-client
的微服务。能够看到只有bus-config-client
的微服务才再次获取服务端的资源。
刷新某个微服务上的具体端口 ,发送http://localhost:9701/actuator/bus-refresh/bus-config-client:9602 post申请来刷新名字为bus-config-client
的微服务中端口为9602的服务。能够看到只有名字为bus-config-client
端口为9602的微服务才再次获取服务端的资源。
再次强调,以上刷新的网址,只有是总线上的微服务都能够刷新,
ip:port
并没有限度
http://localhost:9701/actuator/bus-refresh/bus-config-client:9602 和 http://localhost:9601/actuator/bus-refresh/bus-config-client:9602 或者http://localhost:9602/actuator/bus-refresh/bus-config-client:9602 的成果是一样的。
SpringCloud入门系列到这里也告一段落了,创作不易,如果对你有帮忙,欢送点赞,珍藏和分享啦!
上面是集体公众号,有趣味的能够关注一下,说不定就是你的宝藏公众号哦,根本2,3天1更技术文章!!!
发表回复