关于spring:十六-整合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文件配置:

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> 

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文件配置:

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),用来获取配置项

1.  <span style="font-size: 16px;">package com.honghu.cloud.controller; 

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; 

8.  <strong>@RefreshScope</strong> 
9.  @RestController 
10.  public class MemberController { 

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

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理