Java springcloud B2B2C o2o多用户商城 springcloud架构-使用spring cloud Bus刷新配置...

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

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

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

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

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

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

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

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

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

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

<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="font-size: 16px;"><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>  

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

<span style="font-size: 16px;">package com.honghu.cloud.controller;  
  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.cloud.context.config.annotation.RefreshScope;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
<strong>@RefreshScope</strong>  
@RestController  
public class MemberController {  
  
    @Value("${profile}")  
    private String profile;  
  
    @GetMapping("/profile")  
    public String getProfile() {  
        return this.profile;  
    }  
}</span>  
  1. 查看注册中心,commonservice-config、honghu-member-service服务是否已经注册成功
    在这里插入图片描述
  2. 访问一下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架构的搭建过程及如何运用于企业项目。

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:三五三六二四七二五九

转载于:https://my.oschina.net/u/4045192/blog/2990761

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1- 安装lombok插件 ## mysql - 下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads - 下载后按提示进行安装 - 导入document/sql下的mall.sql文件 ## redis - 下载地址:https://github.com/MicrosoftArchive/redis/releases - 下载后按提示进行安装 ## OSS - 该项目文件上传采用OSS,需要自行注册OSS账号并配置 - 首先将mall-admin\src\main\resources\application.properties文件中以aliyun.oss.开头的配置改为你自己的配置 - OSS上传文件需要配置跨域资源共享(CORS)规则,参考文档:https://help.aliyun.com/document_detail/31928.html - 上传方式采用服务端签名后直传的形式,参考文档:https://help.aliyun.com/document_detail/31926.html ## mall-admin - 启动项目:直接运行com.macro.mall.MallAdminApplication的main方法即可 - 接口文档地址:http://localhost:8080/swagger-ui.html ## mall-search - 启动项目:直接运行com.macro.mall.search.MallSearchApplication的main方法即可 - 接口文档地址:http://localhost:8081/swagger-ui.html - 使用前需要先调用接口导入数据;http://localhost:8081/esProduct/importAll - 如出现无法启动的问题,可以先删除elasticsearch里面的数据再启动 ## mall-portal - 启动mall-portal项目:直接运行com.macro.mall.portal.MallPortalApplication的main方法即可 - 接口文档地址:http://localhost:8085/swagger-ui.html 1. 本地安装开发环境中的所有工具并启动 2. 克隆源代码到本地,使用IDEA或Eclipse打开,并完成编译; 3. 安装 redis mysql 4. 在mysql中新建mall数据库,导入document/sql下的mall.sql文件; 5. 启动mall-admin项目:直接运行com.macro.mall.MallAdminApplication的main方法即可, 接口文档地址:http://localhost:8080/swagger-ui.html;  6. 启动mall-portal项目:直接运行com.macro.mall.portal.MallPortalApplication的main方法即可, 接口文档地址:http://localhost:8085/swagger-ui.html;  7. 克隆mall-admin-web项目,并导入到IDEA中并完成编译传送门,需要安装node环境,然后到项目下; 8. 运行命令:npm install 然后执行 npm run dev,访问地址:http://localhost:8090 即可打开后台管理系统页面; 9. 克隆Mall-Vue-master项目,并导入到IDEA中并完成编译传送门; 10. 运行命令:npm run dev,访问地址: 即可打开pc商城页面; 11. 克隆vue-jd-master项目,并导入到IDEA中并完成编译传送门; 12. 运行命令:npm run dev,访问地址: 即可打开h5商城页面; 13. 下载小程序 用微信开发工具打卡就可以访问   https://gitee.com/zscat-platform/mall   功能预览   http://www.yjlive.cn:8090/#/home   https://gitee.com/zscat-platform/mall/wikis/pages 645

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值