springCloud Finchley 微服务架构从入门到精通【六】Bus RabbitMQ配置刷新

上篇文章介绍了高可用的配置中心,如何在不重启服务器的状况下对配置文件进行更新呢?请看下面的介绍html

1、安装rabbitMq

一、下载地址

http://www.rabbitmq.com/downl...java

二、运行

输入http://localhost:15672,出下如下界面,说明安装成功git

clipboard.png

三、cloud整合配置说明

  • 默认端口: 5672
  • 默认用户名:guest
  • 默认密码:guest

2、代码实现

一、添加依赖

config 服务端:commonservice-config 添加以下依赖:github

<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>

在config 客户端:bussnessservice-user添加web

<dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency> 
       <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文件,加入如下配置:spring

spring:
  rabbitmq: 
    host: localhost
    port: 5672
    username: guest
    password: guest
management: 
  endpoints:
    web:
      exposure: 
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

配置说明:数据库

  • rabbitmq配置
  • 默认关闭了bus请求url,所以须要打开才能使用

在客户端bussnessservice-user的配置文件中添加一个属性server.time=update version1,用来测试属性的变动服务器

三、在须要刷新配置的类上添加@RefreshScope注解

package com.mayi.springcloud.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;

@RestController
@RefreshScope
public class UserManagementController {
    
    @Value("${server.port}")
    String serverPort;
    
    @Value("${server.time}")
    String testValue;
    @GetMapping("/listUsers")
    public String ListUsers(){
        
        /**
         * 模拟从数据库查询
         */
        List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
        for(int i=1; i< 5; i++){
            Map<String, Object> user = new HashMap<String, Object>();
            user.put("id", i);
            user.put("name", "小明" + i);
            users.add(user);
        }
        return "服务器端口号:   " + serverPort + "   |   用户信息:   " + users.toString();
    }
    
    @GetMapping("/getTestValue")
    public String getTestValue(){
        return testValue;
    }
}

3、测试

依次启动eureka server、config server 和 config client(启动两个端口,以测试批量更新配置文件)三个项目
启动配置中心时能够看到以下日志:微信

clipboard.png

  • /actuator/refresh :刷新单个节点
  • /actuator/bus-refresh: 刷新全部节点

启动完成:架构

clipboard.png

使用postman测试:
发送Post请求 http://localhost:8801/getTestValuehttp://localhost:8802/getTestValue

clipboard.png

clipboard.png

修改配置文件:

server:
  port: 8802
  time: update version100

发送post请求 http://localhost:8801/actuator/bus-refresh

clipboard.png

clipboard.png

注意:添加 spring-boot-starter-amqp 依赖有时会报错,这是由于maven里面有多个版本,须要删除,再从新maven - update project

github: https://github.com/tianyana/s...

接下来,我会依次更新文章,直至整个架构完成,若有兴趣的朋友关注做者 或 加我微信 拉你进入spring cloud社区群

clipboard.png

微信公众号:java架构师修行

clipboard.png

本公众号从2018-5.1日 - 2019.5.1日期间,将要按照JAVA高级软件架构师实战培训的路线发布一期完整的架构文章,难度由浅入深,适合有必定开发基础想转架构和正在作初级架构开发的人员学习

相关文章
相关标签/搜索