`
springcloud关注者
  • 浏览: 304463 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
12d8ea3d-4199-3941-8a17-acd5024729b8
Spring_Cloud构...
浏览量:246541
文章分类
社区版块
存档分类
最新评论

(十六) 整合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文件配置:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
	<dependency>
	     <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
	</dependency>

   

   yml文件配置:

server:
  port: 5012
spring: 
  application:
    name: honghu-member-client
  profiles:
    active: dev,discoveryClient
  cloud:
    config:
      discovery: 
        enabled: true
        service-id: commonservice-config-server
      name: honghu-member
      profile: dev
    bus:
      trace:
        enabled: true  #开启消息跟踪          
  rabbitmq:
    host: 192.168.1.254
    port: 5672
    username: honghu
    password: honghu   
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

 

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

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;

@RefreshScope
@RestController
public class MemberController {

	@Value("${profile}")
	private String profile;

	@GetMapping("/profile")
	public String getProfile() {
		return this.profile;
	}
}

 

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配置中心动态刷新方案整理完毕(需要源码可以加qq:2147775633)!!

 

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

 

 

      

 

  • 大小: 227.1 KB
12
0
分享到:
评论
15 楼 springcloud关注者 2018-08-28  
公园美丽 写道
牛呀,真正的实战经验分享,支持一下

下一节出来mq集群方案
14 楼 公园美丽 2018-08-27  
牛呀,真正的实战经验分享,支持一下
13 楼 springcloud关注者 2018-08-24  
6696 写道
一直在找这个,公司也遇到了这个问题,终于可以解决了,谢谢分享

希望可以帮助到您
12 楼 springcloud关注者 2018-08-24  
天空晴朗 写道
多谢分享,看了你的系列文章,都很 不错,收益很深

呵呵,一起学习
11 楼 springcloud关注者 2018-08-24  
mangogo 写道
非常感谢,希望能跟大神交流


可以给我留言,谢谢支持
10 楼 springcloud关注者 2018-08-24  
mangogo 写道
非常感谢,希望能跟大神交流

有问题可以给我留言,一起学习
9 楼 springcloud关注者 2018-08-24  
苦艾酒 写道
哥们,终于看到你发新文章了,你让我等的好苦呀,最近一个月你仿佛消失了

....最近公司在赶项目,所以一直没有更新,最近会持续更新一下文章
8 楼 苦艾酒 2018-08-24  
哥们,终于看到你发新文章了,你让我等的好苦呀,最近一个月你仿佛消失了
7 楼 田田万558 2018-08-24  
高手,可以留个联系方式吗,想和你学习学习
6 楼 mangogo 2018-08-24  
非常感谢,希望能跟大神交流
5 楼 6696 2018-08-24  
一直在找这个,公司也遇到了这个问题,终于可以解决了,谢谢分享
4 楼 天空晴朗 2018-08-24  
多谢分享,看了你的系列文章,都很 不错,收益很深
3 楼 waterml 2018-08-24  
希望作者分享更多的干货
2 楼 springcloud关注者 2018-08-24  
dreamday 写道
谢谢分享~技术很赞

没啥,给大家分享一点技术,希望可以帮助到您
1 楼 dreamday 2018-08-24  
谢谢分享~技术很赞

相关推荐

Global site tag (gtag.js) - Google Analytics