• 本地demo服务器搭建计划——(三)rabbitmq&配置中心config&配置自动刷新


    本章内容主要使用Spring Cloud Config来启动一个配置中心服务,通过Spring Cloud Bus消息总线(依赖rabbitmq)和Git仓库(Gitee)Webhook钩子函数来实现配置的自动更新(push新的配置到gitee仓库时触发)

    安装rabbitmq

    yum安装

    惯例先贴官网链接
    https://www.rabbitmq.com/install-rpm.html
    就是官网这安装教程写的太乱太复杂,看着头疼

    我们来个简单粗暴的,少走弯路

    # 配置epel,相当于添加了一个第三方源
    yum install epel-release -y
    
    # 安装rabbit-server 服务端必须的
    yum install rabbitmq-server -y
    
    # 启动
    rabbitmq-plugins enable rabbitmq_management
    systemctl start rabbitmq-server
    
    # 开机自启
    systemctl enable rabbitmq-server
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    记得还是放通防火墙
    rabbitmq默认监听端口5672,rabbitmq的控制台端口15672
    浏览器访问 http://ip:15672,ok你的rabbitmq已经起来了
    ps:至于为什么我访问的是localserver,配置下/etc/hosts文件,这个不用说吧
    在这里插入图片描述

    配置rabbitmq service

    惯例应该是配置service让rabbitmq开机自启,方便我们调试。
    rabbitmq的service是直接安装好的,我们看下他的配置:

    [Unit]
    Description=RabbitMQ broker
    After=syslog.target network.target
    
    [Service]
    Type=notify
    User=rabbitmq
    Group=rabbitmq
    WorkingDirectory=/var/lib/rabbitmq
    ExecStart=/usr/lib/rabbitmq/bin/rabbitmq-server
    ExecStop=/usr/lib/rabbitmq/bin/rabbitmqctl stop
    
    [Install]
    WantedBy=multi-user.target
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    想详细了解linux service的查看这篇blog https://blog.51cto.com/u_15077561/4163501

    SpringCloudConfig实现

    Spring Cloud Config的代码就需要我们手动实现一下了,不过也就是改改配置文件,starter已经帮我们做了所有事情

    首先我们需要一个git仓库管理配置文件

    Spring Cloud Config依赖git仓库进行配置文件管理
    申请一个git仓库,这个不用多说,为了国内访问方便直接用gitee https://gitee.com/
    然后创建一个仓库,创建一个config目录,创建一个配置文件,比如文件名就叫config-pro.yml
    配置随便写一写,如下:

    app:
      version: pro2
    server:
      port: 80
    spring:
      rabbitmq:
        host: localserver
        port: 5672
        username: guest
        password: guest
      datasource:
        url: jdbc:mysql://127.0.0.1/scexample?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver
      jpa:
        properties:
          hibernate:
            hbm2ddl:
              auto: update
            dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        show-sql: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    然后创建个SpringBoot项目,作为配置中心服务

    引入SpringCloudConfig依赖,pom文件如下

    
    <dependency>
    	<groupId>org.springframework.cloudgroupId>
    	<artifactId>spring-cloud-starter-consul-discoveryartifactId>
    dependency>
    
    <dependency>
    	<groupId>org.springframework.cloudgroupId>
    	<artifactId>spring-cloud-config-serverartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    为了实现配置的自动刷新,还需要引入以下几个依赖

    
    <dependency>
    	<groupId>org.springframework.cloudgroupId>
    	<artifactId>spring-cloud-busartifactId>
    dependency>
    
    <dependency>
    	<groupId>org.springframework.bootgroupId>
    	<artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    
    <dependency>
    	<groupId>org.springframework.cloudgroupId>
    	<artifactId>spring-cloud-stream-binder-rabbitartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    应用配置如下

    server:
      port: 8001 # 配置中心服务监听的端口
    spring:
      application:
        name: spring-cloud-config-server
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/ideatea/demo-config-repo.git # 用于托管配置的git仓库,确保你的应用能访问
              search-paths: # 配置文件所在仓库的目录
                - config
                - repositories
                - example
              username: admin # 仓库用户名
              password: 1234 # 仓库密码
            default-label: master
          discovery:
            enabled: true # 开启服务发现
        bus:
          enabled: true # 开启bus消息总线
          trace:
            enabled: true # 开启trace之后,可以追踪所有此节点的RemoteApplicationEvent的各个子事件类型事件
        # 配置中心的相关配置
        consul:
          host: localserver
          port: 8500
          discovery:
            enabled: true # 将服务注册到consul
            service-name: config-server
    # bus依赖rabbitmq的相关配置,也可以直接使用默认配置
      rabbitmq:
        host: localserver # rabbitmq所在服务器,我这里配的hosts
        port: 5672
        username: guest
        password: guest
    # bus endpoints依赖actuator,暴露bus-refresh端点
    management:
      endpoints:
        web:
          exposure:
            include: bus-refresh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    spring-cloud-bus的事件机制,有兴趣看下这个blog https://blog.csdn.net/xichenguan/article/details/77535694

    启动注册中心和配置中心,我们就能直接访问获取到配置文件了
    如果你想了解访问url和配置文件的对应关系,看看这篇blog https://www.ngui.cc/el/812654.html
    在这里插入图片描述

    配置自动刷新

    上面已经添加了spring-cloud-bus来实现配置刷新,我们还需要在git仓库添加webhook,在推送配置到git仓库时自动触发配置更新
    在这里插入图片描述
    测试配置自动更新
    启动注册中心和配置中心,修改配置文件并推动到git仓库
    获取配置中心配置,查看配置是否刷新

    惯例把配置中心加到service

    vim /etc/systemd/system/config-server.service
    
    • 1

    配置如下

    [Unit]
    Description=config-server service
    After=network.target consul.service rabbitmq-server.service
    # 因为配置中心依赖服务中心和rabbitmq,把依赖加上
    Requires=consul.service rabbitmq-server.service
    Wants=network.target
    
    [Service]
    Type=simple
    # 启动服务的命令
    ExecStart=/usr/bin/java -jar /ssd/config-server/config-server-0.0.1-SNAPSHOT.jar >/var/log/demo/config-server/config-server_info.log
    
    [Install]
    WantedBy=multi-user.target
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    手动与使用骨架创建Maven工程idea版及tomcat插件安装与web工程启动
    FL Studio21.2宿主软件中文免费版下载
    随机森林RF模型超参数的优化:Python实现
    SoftMax回归
    CISCO设备信息泄漏漏洞案例2
    Linux时间相关C库函数
    【Java】Java中对Map进行排序
    LAMP--实战部署步骤--httpd-2.4--mysql-5.7--php8
    CG Magic分享同一场景里下,VR渲染器和CR渲染器哪个好?
    联邦学习fate框架入门
  • 原文地址:https://blog.csdn.net/q863672107/article/details/127992161