SpringCloud中Config和Bus的理论及使用

本篇博客主讲SpringCloud中Config和bus的理论及如何去使用。

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。市面上开源的配置中心有很多,BAT每家都出过,360的QConf、淘宝的diamond、百度的disconf都是解决这类问题。国外也有很多开源的配置中心Apache的Apache Commons Configuration、owner、cfg4j等等。在Spring Cloud中,有分布式配置中心组件 spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client

一个配置中心提供的核心功能
提供服务端和客户端支持
集中管理各环境的配置文件
配置文件修改之后,可以快速的生效
可以进行版本管理
支持大的并发查询
支持各种语言
Spring Cloud Config可以完美的支持以上所有的需求。

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。

二、使用Spring Cloud Config

该操作还是在之前的博客上进行

Spring Cloud的Eureka实现服务发现注册和Feign实现服务调用 https://blog.csdn.net/weixin_42236165/article/details/92842773
SpringCloud的熔断器之Hystrix的理论与实操
https://blog.csdn.net/weixin_42236165/article/details/93159034
SpringCloud 的Zuul理论和使用Zuul构建微服务网关以及Zuul网关使用熔断器
https://blog.csdn.net/weixin_42236165/article/details/93169057

使用的测试工具是Postman

1.先创建config server(通过配置config server去连接服务器,得到一个接口(存放的就是配置信息))

创建一个spring-boot项目,取名为config-server。

在这里插入图片描述
pom.xml中引入依赖:

<dependencies>
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--表示为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-config-server</artifactId>
        </dependency>
  </dependencies>

新建入口类ConfigServerApplication:

package com.szh.configserver;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

application.properties文件:

server.port=8888
spring.cloud.config.server.default-application-name=config-server

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/bufuqing/Spring-Cloud-config.git

#以下部分可以不需要配
# 配置仓库路径
spring.cloud.config.server.git.search-paths=myconfigpath
# 配置仓库的分支
spring.cloud.config.label=master
# 访问git仓库的用户名
spring.cloud.config.server.git.username=xxxxoooo
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
spring.cloud.config.server.git.password=xxxxoooo

2.将分布式项目中的yml,properties文件放到一个服务器上(本地服务器,github,gitee)

到gitee中新建一个仓库(公共的)
在这里插入图片描述
建好的仓库如下:
在这里插入图片描述

把eureka-student模块中的application.properties文件抽取出来,
改名字为student-dev.properties
上传到gitee上,步骤如下:

在这里插入图片描述
上传文件
在这里插入图片描述
在这里插入图片描述
3.修改eureka-student模块(eureka-student客户端通过调用接口,可以得到配置信息并加载到本地)

给eureka-student模块添加依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

其配置文件bootstrap.properties:(名字不能必须是bootstrap后缀要与上传的一致)

#和git里的文件名对应
spring.application.name=student-dev
#远程仓库的分支
spring.cloud.config.label=master
#dev 开发环境配置文件 |  test 测试环境  |  pro 正式环境
#和git里的文件名对应
spring.cloud.config.profile=dev
#指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:8888/

分别启动eureka-server,config-server,eureka-student
访问 http://localhost:8888/student-dev.properties

在这里插入图片描述
访问 http://localhost:8091/students
在这里插入图片描述
缺点:
修改服务器上配置文件,本地项目并不能实时刷新(还是需要手动重启)

针对这种问题:
使用spring cloud bus springcloud的消息总线解决。
客户端会有一个监听,一直监听服务器有没有给我发送消息,发送的消息就是通过rmq传送给客户端,客户端监听到消息就能自动进行编译,自己拉取服务器的配置文件,完成自动部署。

三、使用Spring Cloud Bus

如果要修改gitee中的配置文件 但不需要重新启动服务,就要使用springcloud中的bus组件。

1.首先在config-server的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.为config-server的application.properties中填写配置文件(连接rabbitmq的):

spring.rabbitmq.host=192.168.72.129
management.endpoints.web.exposure.include=bus-refresh

3.再为eureka-student的pom.xml中添加依赖:

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
 	<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>

4.修改gitee中的配置文件student-dev.properties
在这里插入图片描述
记得加上
在这里插入图片描述
访问http://127.0.0.1:8888/actuator/bus-refresh刷新(必须以post方式提交)
在这里插入图片描述
空为成功

再回过来访问http://localhost:8091/students。就访问不到数据,因为修改了配置文件的账号。

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章