SpringCloudConfig+SpringCloudBus学习笔记

本文详细介绍了SpringCloudConfig的使用,包括配置服务端,将配置文件上传到码云,以及配置客户端的步骤。同时,还探讨了SpringCloudBus的基本概念,如何配置服务端和客户端,以实现配置的实时更新。
摘要由CSDN通过智能技术生成

目录

1.集中配置组件 SpringCloudConfig

1.1.SpringCloudConfig 简介

1.2.配置服务端

1.2.1. 将配置文件提交到码云

1.2.2. 配置中心微服务

1.3.配置客户端,我们还是用tensquare_base为例

2.SpringCloudBus

2.1.SpringCloudBus 简介

2.2配置服务端

2.3 配置客户端


1.集中配置组件 SpringCloudConfig

1.1.SpringCloudConfig 简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需
要分布式配置中心组件。在 Spring Cloud 中,有分布式配置中心组件 spring cloud config ,它
支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。在 spring cloud
config 组件中,分两个角色,一是 config server ,二是 config client
Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境
下的配置,默认使用 Git 存储配置文件内容,也可以使用 SVN 存储,或者是本地文件存储。
Config Client Config Server 的客户端,用于操作存储在 Config Server 中的配置内容。
微服务在启动时会请求 Config Server 获取配置文件的内容,请求到后再启动容器。
详细内容看在线文档: https://springcloud.cc/spring-cloud-config.html

 

1.2.配置服务端

1.2.1. 将配置文件提交到码云

使用 GitHub 时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况。如果我们希望体验 Git 飞一般的速度,可以使用国内的 Git 托管服务——码云 (gitee.com )。
GitHub 相比,码云也提供免费的 Git 仓库。此外,还集成了代码质量检测、项目演示等 功能。对于团队协作开发,码云还提供了项目管理、代码托管、文档管理的服务。
 
1 )浏览器打开 gitee.com ,注册用户 ,注册后登陆码云管理控制台。
2 )创建项目 tensquare-config ( 点击右上角的加号 ,下拉菜单选择创建项目 )
3 上传配置文件,将 tensquare_base 工程的 application.yml 改名为 base-test.yml 后上传

文件命名规则:
{application}-{profile}.yml {application}-{profile}.properties application 为应用名称 profile 指的开发环境(用于区分开发环境,测试环境、 生产环境等)
 
4 )复制 git 地址 , 备用

1.2.2. 配置中心微服务

1 )创建工程模块 配置中心微服务 tensquare_config ,pom.xml 引入依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
(2) 创建启动类 ConfigServerApplication
package com.tensquare.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * 配置中心微服务
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class,args);
    }

}
(3)编写配置文件 application.yml
server:
  port: 12000
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xxxxxx此处用你复制的地址
(4)启动项目,浏览器测试:http://localhost:12000/base- test .yml 可以看到配置内容
 

1.3.配置客户端,我们还是用tensquare_base为例

1 )在 tensquare_base 工程添加依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

(2)添加 bootstrap.yml ,删除 application.yml

spring:
  cloud:
    config:
      uri: http://127.0.0.1:12000  # 配置中心服务端地址
      name: base    # 配置文件前面部分
      profile: test   # 配置文件后面部分
      label: master  # 从仓库哪个分支获取
(3)测试: 启动工程 三个微服务tensquare_eureka,tensquare_config,tensquare_base,看tensquare_base是否可以正常运行。 http://localhost:9001/label
 

2.SpringCloudBus

2.1.SpringCloudBus 简介

如果我们更新码云中的配置文件,那客户端工程是否可以及时接受新的配置信息 呢?我们现在来做有一个测试,修改一下码云中的配置文件中 mysql 的端口 ,然后测试 http://localhost:9001/label 数据依然可以查询出来,证明修改服务器中的配置并没有更新立刻到工程,只有重新启动程序才会读取配置。 那我们如果想在不重启微服务的情况下更新 配置如何来实现呢? 我们使用 SpringCloudBus 来实现配置的自动更新。
 

2.2配置服务端

(1) 修改 tensquare_config 工程的 pom.xml ,引用依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
(2) 修改 application.yml ,添加配置
rabbitmq:
  host: 192.168.141.141
management: #暴露触发消息总线的地址
  endpoints:
    web:
      exposure:
        include: bus-refresh

2.3 配置客户端

我们还是以基础模块为例,加入消息总线。
1 )修改 tensquare_base 工程 ,引入依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
2 )在码云的配置文件中配置 rabbitMQ 的地址, 增加自定义配置sms,整体配置文件如下。
server:
  port: 9001
spring:
  application:
    name: tensquare-base #基础微服务
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.141.141:3306/tensquare_base?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: true
  rabbitmq:
    host: 192.168.141.141
eureka:
  client:
    register-with-eureka: true # 是否把该服务注册到eureka服务
    fetch-registry: true # 是否需要从eureka获取信息
    service-url:   # eureka注册地址
      defaultZone: http://127.0.0.1:6868/eureka
  instance:   # 在多个相同微服务的时候,需要eureka自动给每个微服务分配ip地址
    prefer-ip-address: true
sms: 
  ip: 127.0.0.1
 
 
(3) 在 tensquare_base中LabelController新增方法
    @Value("${sms.ip}")
    private String ip;
    /**
     * 读取自定义配置
     */
    @RequestMapping(value = "/showIp",method = RequestMethod.GET)
    public Result showIp(){
        return new Result(true,StatusCode.OK,"查询成功",ip);
    }

(4)启动 tensquare_eureka tensquare_config tensquare_base 看是否正常运行

(5) 修改码云上的配置文件中的自定义配置 ,IP 改为 bugger  
(6 postman 测试 Url: http://127.0.0.1:12000/actuator/bus-refresh Method: post
(7 )再次观察输出的数据是否改变了
 
以下流程为:
改完(5)之后,先get请求 http://localhost:9001/label/showIp,获取到如下图
之后执行(6),post请求 http://127.0.0.1:12000/actuator/bus-refresh,请求成功后内容为空。
观察结果(7)如下图
你好!对于Spring Cloud Alibaba的学习笔记,我可以为你提供一些基本的信息和指导。在学习Spring Cloud Alibaba之前,你可能需要对Spring Cloud和Alibaba的相关技术有一定的了解。 Spring Cloud Alibaba是基于Spring Cloud开发的一套微服务框架,它融合了阿里巴巴的中间件技术栈,提供了一系列开箱即用的解决方案,包括服务注册与发现、分布式配置管理、消息驱动等功能。它旨在帮助开发者快速构建微服务架构。 以下是一些学习Spring Cloud Alibaba的步骤和资源推荐: 1. 了解Spring Cloud和Alibaba的基础知识:在开始学习Spring Cloud Alibaba之前,你需要对Spring Cloud和阿里巴巴的相关技术有一定的了解。你可以先学习Spring Cloud的核心概念和基本使用方式,再深入了解阿里巴巴的中间件技术栈。 2. 官方文档:Spring Cloud Alibaba官方文档是学习的重要参考资料,你可以从官方文档中了解框架的核心概念、使用方式以及各个组件的详细说明。 3. 示例代码:官方文档中通常会提供一些示例代码,你可以通过运行示例代码来实践学习,加深对框架的理解。 4. 开发实践:尝试在自己的项目中应用Spring Cloud Alibaba,可以从简单的项目开始,逐步扩展和深入应用框架的各个功能。 5. 社区资源:参与Spring Cloud Alibaba的相关社区活动,例如论坛、博客、技术分享等,与其他开发者交流和学习。 希望以上信息对你有所帮助!如果你有更具体的问题,欢迎继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值