`
白马探
  • 浏览: 14695 次
社区版块
存档分类
最新评论

整合spring cloud云架构 -使用spring cloud Bus刷新配置

 
阅读更多

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

 

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

 

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

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

 

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

 

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

 

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

 

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

1. commonservice-config服务配置可以参考之前的链接:

http://2147775633.iteye.com/admin/blogs/2396692

 

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

   pom文件配置:

Xml代码  收藏代码
  1. <span style="font-size: 16px;">        <dependency>  
  2.             <groupId>org.springframework.boot</groupId>  
  3.             <artifactId><span style="font-size: 16px;">spring-boot-starter-actuator</span></artifactId>  
  4.         </dependency>  
  5.           
  6.     <dependency>  
  7.          <groupId>org.springframework.cloud</groupId>  
  8.              <artifactId><span style="font-size: 16px;">spring-cloud-starter-bus-amqp</span></artifactId>  
  9.     </dependency></span>  

   

   yml文件配置:

Java代码  收藏代码
  1. <span style="font-size: 16px;">server:  
  2.   port: 5012  
  3. spring:   
  4.   application:  
  5.     name: honghu-member-client  
  6.   profiles:  
  7.     active: dev,discoveryClient  
  8.   cloud:  
  9.     config:  
  10.       discovery:   
  11.         enabled: true  
  12.         service-id: commonservice-config-server  
  13.       <span style="font-size: 16px;"><strong>name: honghu-member  
  14.       profile: dev  
  15.     bus:  
  16.       trace:  
  17.         enabled: true  #开启消息跟踪  </strong>          
  18.   <strong>rabbitmq:  
  19.     host: 192.168.1.254  
  20.     port: 5672  
  21.     username: honghu  
  22.     password: honghu</strong>  </span>   
  23. eureka:  
  24.   client:  
  25.     serviceUrl:  
  26.       defaultZone: http://honghu:123456@localhost:8761/eureka/  
  27.   instance:  
  28.     prefer-ip-address: true  
  29. logging:  
  30.   level:  
  31.     root: INFO  
  32.     org.springframework.security: INFO  
  33. management:  
  34.   security:  
  35.     enabled: false  
  36. security:  
  37.   basic:  
  38.     enabled: false</span>  

 

    编写一个测试类(MemberController.java),用来获取配置项

Java代码  收藏代码
  1. <span style="font-size: 16px;">package com.honghu.cloud.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.cloud.context.config.annotation.RefreshScope;  
  5. import org.springframework.web.bind.annotation.GetMapping;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7.   
  8. <strong>@RefreshScope</strong>  
  9. @RestController  
  10. public class MemberController {  
  11.   
  12.     @Value("${profile}")  
  13.     private String profile;  
  14.   
  15.     @GetMapping("/profile")  
  16.     public String getProfile() {  
  17.         return this.profile;  
  18.     }  
  19. }</span>  

 

3. 查看注册中心,commonservice-config、honghu-member-service服务是否已经注册成功



 

4. 访问一下profile,获取profile对应的配置信息(原配置):

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

 

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

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

 

6. 使用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架构的搭建过程及如何运用于企业项目。

分享到:
评论

相关推荐

    spring-cloud-config + spring-cloud-bus-amqp实现分布式集群配置动态更新

    Spring Cloud Config 和 Spring Cloud Bus AMQP 的结合使用为解决这一问题提供了一种优雅的解决方案。让我们深入探讨这两个组件以及它们如何协同工作来实现配置的动态更新。 Spring Cloud Config 是一个用于微服务...

    springcloud-learning-master.zip springcloud学习合集

    SpringCloud是基于SpringBoot的云应用开发工具集,它允许开发者快速构建一些常见的云原生应用特性,如服务发现、配置管理、断路器模式等。SpringBoot简化了创建独立的、生产级别的基于Spring的应用程序的过程,而...

    spring-cloud项目_springcloud_springcloud项目_springcloud_spring-clou

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具集,它为开发者提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态)...

    spring-cloud-examples-master

    6. **Spring Cloud Bus** - 事件、消息总线:Bus可以用来传播配置更改或者实现轻量级的消息传递。在这里,我们将探讨如何利用Bus实现实时配置更新的广播。 7. **Spring Cloud Stream** - 流处理:Stream提供了一种...

    spring-cloud-examples

    spring-cloud-config-eureka-bus:配置中心和消息总线示例(配置中心终结版) gateway-service-zuul:Spring Cloud Zuul使用初级篇 网关 均衡负载 spring-cloud-zuul:Spring Cloud Zuul使用高级篇 Filter 鉴权 熔断...

    springcloud-demo-master_spring-cloud_cloud_

    【标题】"springcloud-demo-master_spring-cloud_cloud_" 指的是一个基于Spring Cloud的示例项目,这个项目主要用于学习和演示Spring Cloud的核心功能和基本流程。Spring Cloud是微服务架构的重要工具集,它提供了...

    SpringCloud项目实战各组件源代码案例

    包含内容: Spring Cloud系列教程 Spring Boot Spring Cloud Stream...springcloud-config-oracle-bus-kafka.zipspringcloud-feign.zip springcloud-producer.zip springcloud-producer-consumer.zip springcloudstudy.

    spring-cloud使用的各种示例

    - [springcloud(九):配置中心和消息总线(配置中心终结版)](http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html) - [springcloud(十):服务网关zuul]...

    [云框架]基于SpringCloud的微服务架构-用户指南

    【云框架】基于Spring Cloud的微服务架构-用户指南 在现代软件开发中,微服务架构已经成为构建可扩展、高可用且易于维护的应用程序的重要模式。Spring Cloud作为Java开发领域内的一个主流微服务框架,为开发者提供...

    SpringCloud电商项目-cloud-mall-practice.zip

    6. **Spring Cloud Bus消息总线**:Bus常与Config结合使用,用于实时推送配置更新到所有服务实例,确保配置的实时同步。 7. **Docker容器化部署**:项目可能采用Docker进行服务的容器化部署,这样可以提高部署的...

    spring cloud 微服务架构集成-spring-cloud-framework.zip

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具集,它为开发者提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态)...

    springcloud微服务框架+服务模版

    spring-cloud-config-eureka-bus:配置中心和消息总线示例(配置中心终结版) gateway-service-zuul:Spring Cloud Zuul使用初级篇 网关 均衡负载 spring-cloud-zuul:Spring Cloud Zuul使用高级篇 Filter 鉴权 熔断...

    SpringCloud微服务分布式架构开发实战-50000-05-作业及参考答案.rar.rar

    SpringCloud是Java领域中广泛使用的微服务开发工具集,它提供了众多服务发现、配置管理、负载均衡、熔断机制等组件,使得开发者能够轻松构建分布式系统。 首先,我们需要了解SpringCloud的核心组件。Eureka是服务...

    Spring Cloud实战 _springcloud实战_springcloud_

    Spring Cloud Config是配置管理工具,它支持配置服务的集中化管理和动态刷新,使得开发者可以在不重启应用的情况下更新配置。另外,Spring Cloud Bus可以将配置变更实时推送到所有关联的服务,进一步提高了配置管理...

    spring cloud中文版【Spring Cloud Config】--spring cloud中文文档.pdf

    客户端示例部分会介绍如何在Spring应用中使用Spring Cloud Config客户端,通过配置客户端引入配置服务器的信息,并与之进行通信以获取配置信息。 **配置服务Spring Cloud Config Server** 配置服务端是整个Spring ...

    spring-cloud-netflix.rar

    "spring-cloud-netflix.rar"压缩包包含了与Spring Cloud Config Bus相关的配套代码,旨在帮助开发者理解和实践Spring Cloud的核心组件之一——Config Bus,它在微服务架构中的作用至关重要。 Spring Cloud Config是...

    SpringCloud-Learning-master.zip

    总之,《SpringCloud-Learning-master》是一个全面学习Spring Cloud的实践项目,包含了微服务架构中的关键组件,对于想要提升微服务开发能力的开发者来说,是一个非常宝贵的资源。通过深入研究和实践,你将能够构建...

    Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新(2.自动动态刷新).zip

    Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新 spring cloud bus能管理和传播分布式系统间的消息,就像分布式执行器,可用于广播状态更改、时间推送等,也可以当做微服务间的通信通道 spring ...

    Spring Cloud Finchley.SR1-Spring Cloud 手册-Spring Cloud 文档

    在接下来的内容中,我将详细描述标题《Spring Cloud Finchley.SR1-Spring Cloud 手册-Spring Cloud 文档》与《Spring Cloud 2.x手册-Spring Cloud 2.x 文档》以及标签“springCloud spring 微服务”中涉及的知识点。...

Global site tag (gtag.js) - Google Analytics