【记录贴】SpringCloud自学之路-Bus

前言

记录一次学习SpringCloud的过程,不断积累经验,手撸方能熟能生巧。

新手上路难免有误,人非圣贤,欢迎各位指出不足之处,虚心听取各位的建议与意见。

项目源码:https://github.com/Ahua0918/hwa

一、准备工作

本文还是基于上一篇文章来实现。我们希望一旦git上的配置信息修改之后,就可以自动的刷新到微服务里,而不是需要手动重启才可以。SpringCloud通过RabbitMQ来进行消息广播,以达到有配置信息发生改变的时候,广播给多个微服务的效果。

二、接着改造hwa-eureka-feign

2.1 修改pom文件添加所需依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <artifactId>hwa</artifactId>
            <groupId>com.lee</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>


        <artifactId>hwa-eureka-feign</artifactId>
        <packaging>jar</packaging>


        <description>SuperFeign</description>

        <dependencies>
            <!--  Feign客户端依赖  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--  Config客户端依赖  用于访问配置服务器  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <!--  用于访问路径:/actuator/bus-refresh  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!--  RabbitMQ依赖  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
</project>

2.2 修改Bootstrap.yml

spring:
  cloud:
    config:
      label: master
      profile: dev
      discovery:
        enabled: true
        service-id: hwa-config-server
    #bus消息总线
    bus:
      enabled: true
      trace:
        enabled: true
  #RabbitMQ配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#主机名
eureka:
  client:
    #eureka服务地址
    #注明自己的服务注册中心的地址,与Eureka Server中的配置application.yml相对应
    service-url:
      defaultZone: http://localhost:1116/eureka/

2.3 修改Application.yml

      新增路径访问允许,这样才能访问/actuator/bus-refresh

#注册中心端口
server:
  port: 8884
spring:
  application:
    #服务名称
    name: hwa-eureka-feign
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

2.4 修改HwaFeignApplication

      新增RabbitMQ端口检测

package com.lee.feign;

import cn.hutool.core.net.NetUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients     //通过注解@EnableFeignClients配置Feign客户端功能
@RestController
@RefreshScope       //配置文件自动刷新
public class HwaFeignApplication {
    public static void main(String[] args) {
        //判断 RabbitMQ 是否启动
        int rabbitMQPort=5672;
        if(NetUtil.isUsableLocalPort(rabbitMQPort)){
            System.err.printf("未在端口%d 发现 rabbitMQ服务,请检查rabbitMQ 是否启动", rabbitMQPort);
            System.exit(1);
        }
        SpringApplication.run(HwaFeignApplication.class, args);
    }
    @Value("${hwa}")
    String hwa;
    @RequestMapping(value = "/hiConfigClient")
    public String hi(){
        return hwa;
    }
}

2.5 启动并访问

      依次启动HwaEurekaApplication,HwaConfigServerApplication,HwaFeignApplication,访问http://localhost:8884/hiConfigClient,浏览器显示

hwa springcloud version 1.1

      这时我们去代码仓库将hwa的值改为“hwa springcloud version 1.2”,即可改变配置文件hwa的值。此时我们不需要再重启服务,我们只需要发送post请求:http://localhost:8884/actuator/bus-refresh,(不支持Get方式访问,直接把这个地址放在浏览器里,会抛出405错误,可借助PostMan工具)会发现重新读取配置文件

      重新读取配置文件

      这时我们再访问http://localhost:8884/hiConfigClient,浏览器显示

hwa = hwa springcloud version 1.2

     另外,/actuator/bus-refresh接口可以指定服务,即使用”destination”参数,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的所有服务

2.6 总结

      当git文件更改的时候,通过pc端用post向端口为8884的config-client发送请求/bus/refresh/;此时8884端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值