自学内容网 自学内容网

SpringCloud Config配置中心 SpringCloud Bus消息总线

一、SpringCloud Config  使用git储存配置信息

        1)什么是

        SpringCloud Config项目实现的目标是将配置文件从本地项目中抽出来放到git仓库中,项目启动时自动从git仓库中取配置文件。

        但是本地项目不直接和git仓库通信,而是通过配置服务器中转。


         2)怎么实现

〇、打开Eureka服务器

        使用Eureka完成服务发现,所有其他服务器都视为Eureka客户端。

①、配置git

        在gitee网址中新建本地仓库cloud.config、 新建本地yml文件application-diy.yml


②、config-server

        1)依赖坐标:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
        2)配置中心服务器的配置文件
server:
  port: 8888

# 增加分布式配置中心服务端配置。连接什么GIT仓库
spring:
  application:
    name: config-server
  cloud: # spring cloud常用配置前置
    config: # 分布式配置中心配置前置
      server: # 服务端配置
        git: # git文件仓库配置
          uri: https://gitee.com/ben10rk/cloud.config.git # git仓库具体地址
          #username: wollo_test # 私有仓库必须配置用户名和密码。
          #password: 123456789 # 公开仓库可以省略用户名和密码配置。
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
        3)启动类添加  启动服务注解


③、config-client

        1)依赖坐标

                将配置中心服务器的启动器依赖改为客户端依赖

        2)配置文件

                需要使用bootstrap.yml命名配置文件,该文件加载在application文件前,可以看作是实际配置文件的模板父文件。

                

 # 新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。
 # 配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: config-client
  cloud:
    config: # spring cloud config 客户端配置
      discovery:
        enabled: true
        service-id: config-server
      uri: http://config-server/ # 分布式配置中心服务端地址。 默认http://localhost:8888
      name: application # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
      profile: diy # 要读取的配置文件环境是什么,默认default
      label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。

 

3)实现效果

        和正常在客户端项目中直接编写配置文件效果一样。但是该方式足够灵活,可以实现动态加载。

        在客户端编写控制器:

        



二、SpringCloud Bus  服务器集群配置消息总线

        SpringCloud Bus 是在 SpringCloud config的基础上,实现动态加载集群的配置文件,这个过程首先是热部署,其次是集群,再然后是消息队列。

        1)依赖坐标

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>

         2)配置文件

                连接rabbitmq;

                开放actuator所有功能;

                编写两个启动类模拟集群。

eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: config-client
  cloud:
    config: # spring cloud config 客户端配置
      discovery:
        enabled: true
        service-id: config-server
      uri: http://config-server/ # 分布式配置中心服务端地址。 默认http://localhost:8888
      name: application # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
      profile: diy # 要读取的配置文件环境是什么,默认default
      label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。
  rabbitmq:
    host: 192.168.222.128 # RabbitMQ服务器的IP。默认localhost
    port: 5672 # RabbitMQ服务器的端口。
    username: wollo # RabbitMQ的访问用户名。默认guest。
    password: wollo # RabbitMQ的访问密码。默认guest
    virtual-host: / # 连接RabbitMQ中的哪一个虚拟主机。默认 /

management:
  endpoints:
    web:
      exposure:
        include: "*"
server:
  port: 8080

        3)开启作用域范围

                添加注解 @RefreshScope

       4)测试

             1、   一开始访问两个端口8080 , 8081:

            2、     修改git仓库配置文件:

            3、    热刷新其中一个客户端,这里刷新8080端口。

           4、     效果是两个端口都刷新了:

 


原文地址:https://blog.csdn.net/weixin_45939821/article/details/142696094

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!