欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 前端技术 > javascript >内容正文

javascript

springcloud教程-凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 javascript 16 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 springcloud教程-消息总线bus 客户端(client)刷新(springcloud版本greenwich.sr4) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • 消息总线(bus)介绍
    • 项目示例
      • config-client-bus

代码地址:github-spring-cloud地址

前言:前面文章讲了spring cloud config配置中心如何使用,当我们在更新git上面的配置,想要获取最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。使用spring cloud bus可以完美解决这一问题。使用消息总线需要依赖mq,本章文章将采用rabbitmq,具体怎么安装rabbitmq可以查看此文章:安装rabbitmq

消息总线(bus)介绍

spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用amqp消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。

项目示例

本章节消息总线集成在客户端,本章节在前几张springcloud基础上改进。

config-client-bus

添加pom文件

spring-cloud-learncom.sl.learn.cloud1.0-snapshot4.0.0com.sl.learn.cloudconfig-client-bus1.0-snapshotorg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.bootspring-boot-starter-actuatororg.springframework.cloudspring-cloud-starter-bus-amqporg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.springframework.cloudspring-cloud-starter-config

添加配置文件bootstrap.properties

spring.application.name=config-client-bus spring.cloud.config.label=master spring.cloud.config.profile=dev spring.cloud.config.uri=http://localhost:8088/ server.port=8098eureka.client.service-url.defaultzone=http://localhost:8080/eureka/management.endpoints.web.exposure.include= * management.endpoint.health.show-details= always management.endpoint.shutdown.enabled= true info.app.name = spring-boot-actuator info.app.version = 1.0.0 info.app.test = test## 开启消息跟踪 spring.cloud.bus.trace.enabled=truespring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest

启动类application

@springbootapplication @restcontroller @refreshscope @enableeurekaclient @enablediscoveryclient public class configclientbusapplication {public static void main(string[] args) {springapplication.run(configclientbusapplication.class,args);}@value("${helloconfig}")private string helloconfig;@requestmapping(value = "/hi")public string helloconfig() {return helloconfig;} }
  • 创建配置文件注意两点:
    rabbitmq的默认端口为:5672

  • 监控 actuator 一定要配下面这几个,不然访问http://localhost:8097/actuator会报404
    management.endpoints.web.exposure.include= *
    management.endpoint.health.show-details= always
    management.endpoint.shutdown.enabled= true

客户端分别启动两个端口8097,8098
分别访问地址:http://localhost:8098/hi和http://localhost:8097/hi
返回结果为:1233

我们对端口为8098的服务发送一个/actuator/bus-refresh的post请求,在win10下使用下面命令来模拟webhook。curl -v -x post http://localhost:8098/actuator/bus-refresh

返回结果为可以看到已经把最新的结果返回:

注意一点springboot2.0访问地址为:actuator/bus-refresh

本篇文章的消息总线在配置中心的使用机制用图表示(图片来源于网上)

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的springcloud教程-消息总线bus 客户端(client)刷新(springcloud版本greenwich.sr4)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图