(十六) 整合spring cloud云架构 -使用spring cloud Bus刷新配置

简介: 针对以上问题,commonservice-config服务端和业务微服务分别做了相关的配置,服务端负责将git(svn或本地文件系统)中存储的配置文件进行配置化(我们使用的是本地配置方案,方便直接将配置文件更新到linux上), 完整项目的源码来源 技术支持1791743380

我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服务进行了细粒度拆分,做成了不同的业务微服务。

当我们的业务系统越来越庞大复杂的时候,各种配置也会随之增多。配置文件只要一修改,会对commonservice-config配置中心先停止服务,然后再重新启动,最后使配置生效。

如果服务少,我们可以手动方式来启动,但是对业务和系统的稳定性肯定有一定的影响。

如果是成百上千的服务都靠手动操作,我估计运维人员或技术人员会疯掉的。

针对以上问题,commonservice-config服务端和业务微服务分别做了相关的配置,服务端负责将git(svn或本地文件系统)中存储的配置文件进行配置化(我们使用的是本地配置方案,方便直接将配置文件更新到linux上),

业务微服务通过配置从服务端配置中心获取相关配置,如果配置文件变动了,通过刷新业务微服务的方式,将最新的配置信息获取。

spring cloud Bus通过一个轻量级消息代理连接分布式系统的节点。这可以用于广播状态更改(如配置更改)或其他管理指令。

接下来,我们就来实施通过spring cloud Bus方案,动态刷新服务端配置,具体步骤如下:

  1. 业务微服务配置(以honghu-member-servcie会员服务为例):

pom文件配置:

<span style="font-size: 16px;">        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId><span style="font-size: 16px;">spring-boot-starter-actuator</span></artifactId>
        </dependency>
        
    <dependency>
         <groupId>org.springframework.cloud</groupId>
             <artifactId><span style="font-size: 16px;">spring-cloud-starter-bus-amqp</span></artifactId>
    </dependency></span>

yml文件配置:

<span style="font-size: 16px;">server:
  port: 5012
spring: 
  application:
    name: honghu-member-client
  profiles:
    active: dev,discoveryClient
  cloud:
    config:
      discovery: 
        enabled: true
        service-id: commonservice-config-server
      <span style="color: #ff0000;"><strong>name: honghu-member
      profile: dev
    bus:
      trace:
        enabled: true  #开启消息跟踪  </strong>        
  <strong>rabbitmq:
    host: 192.168.1.254
    port: 5672
    username: honghu
    password: honghu</strong>  </span> 
eureka:
  client:
    serviceUrl:
      defaultZone: http://honghu:123456@localhost:8761/eureka/
  instance:
    prefer-ip-address: true
logging:
  level:
    root: INFO
    org.springframework.security: INFO
management:
  security:
    enabled: false
security:
  basic:
    enabled: false</span>
  1. 查看注册中心,commonservice-config、honghu-member-service服务是否已经注册成功
  1. 访问一下profile,获取profile对应的配置信息(原配置):

访问http://localhost:7071/profile ==》 访问结果:123456

  1. 修改config配置中心的配置文件,将profile=123456修改为honghu123456

再次访问http://localhost:7071/profile ==》 访问结果:123456

  1. 使用spring cloud bus 刷新方案(使用post man测试工具进行测试)

http://localhost:7071/bus/refresh

再次访问http://localhost:7071/profile ==》 访问结果:honghu123456

到此,整个commonservice-config配置中心动态刷新方案整理完毕!

欢迎大家和我一起学习spring cloud构建微服务云架构,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。

完整项目的源码来源 技术支持1791743380

目录
相关文章
|
7月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
8月前
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
1360 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
9月前
|
人工智能 安全 Java
AI 时代:从 Spring Cloud Alibaba 到 Spring AI Alibaba
本次分享由阿里云智能集团云原生微服务技术负责人李艳林主讲,主题为“AI时代:从Spring Cloud Alibaba到Spring AI Alibaba”。内容涵盖应用架构演进、AI agent框架发展趋势及Spring AI Alibaba的重磅发布。分享介绍了AI原生架构与传统架构的融合,强调了API优先、事件驱动和AI运维的重要性。同时,详细解析了Spring AI Alibaba的三层抽象设计,包括模型支持、工作流智能体编排及生产可用性构建能力,确保安全合规、高效部署与可观测性。最后,结合实际案例展示了如何利用私域数据优化AI应用,提升业务价值。
817 4
|
10月前
|
负载均衡 Java 开发者
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
542 5
|
负载均衡 Java API
【Spring Cloud生态】Spring Cloud Gateway基本配置
【Spring Cloud生态】Spring Cloud Gateway基本配置
919 0
|
Java Spring
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
268 1
|
Java Spring 容器
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
118 0
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
134 0
|
SQL Java 数据库连接
【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
110 0
|
Java 开发工具 Spring
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
144 0