Springcloud 的配置中心和消息总线

186 阅读5分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第9天,点击查看活动详情

Config配置中心

介绍:

用于管理配置文件,在配置文件中存在两种,一种是叫application.yml一般用于配置一些项目信息,另一种是boostrap.yml一般用于配置项目启动时需要加载的配置,两者bootstrap优先级别更高。

image-20200922094047155.png

1. 配置中心服务搭建

步骤:

  1. 创建config-server项目

    • web/eureka-client/config-server
  2. 在启动类添加注解

    • eurekaclient
    • configserver注解声明是一个配置中心
  3. 编写配置文件

    • 端口
    • 应用名称
    • 注册中心
    • git地址(暂时不配置)
  4. 创建git仓库存放配置文件

过程:

  • 启动类添加注解

    @SpringBootApplication
    @EnableEurekaClient //注册到注册中心
    @EnableConfigServer //声明是一个配置中心
    public class ConfigServerApplication {
    ​
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    
  • 配置文件

    server:
      port: 1200
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/guodongzz/jiuye-31.git
              #账密公开仓库不需要填写,如果是私有仓库必须明文填写,
    #          username:
    #          password:
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    
2. 改造用户服务

步骤:

  1. 在gitee创建三个配置文件

    • user-dev.yml

      #db
      spring:
        datasource:
          #serverTimezone=UTC
          url: jdbc:mysql://127.0.0.1/itheima?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: root
        application:
          name: user-service
      #mybatis
      mybatis:
        mapper-locations: classpath:mapper/*.xml
        type-aliases-package: com.itheima.pojo
      personName: caixukun-dev
      
    • user-test.yml

      #db
      spring:
        datasource:
          #serverTimezone=UTC
          url: jdbc:mysql://127.0.0.1/itheima?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: root
        application:
          name: user-service
      #mybatis
      mybatis:
        mapper-locations: classpath:mapper/*.xml
        type-aliases-package: com.itheima.pojo
      personName: caixukun-test
      
    • user-prod.yml

      #db
      spring:
        datasource:
          #serverTimezone=UTC
          url: jdbc:mysql://127.0.0.1/itheima?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: root
        application:
          name: user-service
      #mybatis
      mybatis:
        mapper-locations: classpath:mapper/*.xml
        type-aliases-package: com.itheima.pojo
      personName: caixukun-prod
      
  2. 添加依赖 config

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
  3. 在UserController中获取personName

        @Value("${personName}")
        private String personName; 
    /**
         * @Author: guodong 
         * @Date: 9:50 2020/9/22
         * @Parms []
         * @ReturnType: java.lang.String
         * @Description: 获取personName区分使用的配置文件环境
        */
        @GetMapping("getPersonName")
        public String getPersonName() {
            return personName;
        }
    
  4. 在配置文件中声明要使用配置中心

    • 将application.yml------>bootstrap.yml

    • 将配置文件中的db+mybatis配置删除

    • 编写配置使用配置中心

      • 声明要使用的配置文件名称
      • 配置文件环境
      • git分支名称
      • 声明要使用配置中心
      • 配置中心的服务id
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka #注册中心的地址
      server:
        port: 9091
      spring:
        cloud:
          config:
          #配置文件名称
            name: user
            profile: dev #配置文件环境
            label: master #git分支名称
            discovery:
              enabled: true #开启使用配置中心
              service-id: config-server #配置中心的服务应用名称
      
3. 配置更新问题

演示:

修改配置文件personName内容刷新91和92服务发现配置信息没有发生变化。

解决:

  1. 导入actuator依赖

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  2. 暴露刷新地址---》refresh

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka #注册中心的地址
    server:
      port: 9091
    spring:
      cloud:
        config:
        #配置文件名称
          name: user
          profile: dev #配置文件环境
          label: master #git分支名称
          discovery:
            enabled: true #开启使用配置中心
            service-id: config-server #配置中心的服务应用名称
    management:
      endpoints:
        web:
          exposure:
            include: refresh  #暴露刷新地址
    
  3. 在UserController添加@RefreshScope注解表示允许刷新

    @RefreshScope //表示允许刷新
    public class UserController {
        
        //省略
    }
    
  4. 使用postman请求刷新地址发送post请求

    http://localhost:9091/actuator/refresh

    http://localhost:9092/actuator/refresh

注意:

  • 刷新时必须使用post请求
  • 暴露的刷新地址必须叫refresh

Bus-消息总线

介绍:

消息总线,类比于微信,用于发送接收消息。

1. 安装RabbitMq
#docker复习
docker images
docker search 镜像名称
docker pull 镜像名称:版本号
docker rmi 镜像名称:版本号
docker images -q
​
docker ps 
docker ps -a
#交互式容器 一旦创建直接进入容器内部,退出容器后容器自动停止运行
docker run -it --name=容器名称 镜像 /bin/bash 
#守护式容器  创建后不会进入容器内部,需要使用命令方可进入,容器退出后不会停止运行
docker run -id --name=容器名称 -p 外部端口:内部端口 镜像
​
docker start 容器名称
docker stop 容器名称
docker rm 容器名称 #运行时的容器需要停止后再删除
docker rm -f  容器名称 #不建议使用#安装mq
docker run -id --name=c_rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:management
#测试访问
http://ip:15672 #如果可以看到管理页面代表部署成功 账密: guest/guest
2. 改造配置中心

步骤:

  1. 导入bus-amqp依赖、actuator依赖

        <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. 修改配置文件

    • mq配置信息
    • 暴露刷新地址
    • server:
        port: 1200
      spring:
        application:
          name: config-server
        cloud:
          config:
            server:
              git:
                uri: https://gitee.com/guodongzz/jiuye-31.git
        rabbitmq:
          host: 192.168.200.132
          #以下三个配置使用默认值即可
      #    port: 5672
      #    username: guest
      #    password: guest
                #账密公开仓库不需要填写,如果是私有仓库必须明文填写,
      #          username:
      #          password:
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka
            #暴露刷新地址
      management:
        endpoints:
          web:
            exposure:
              include: bus-refresh
      
      
3. 改造用户服务

步骤:

  1. 导入bus-amqp依赖
  2. 在配置文件中添加mq信息
  3. 在UserController添加@RefreshScope注解表示允许刷新(已经添加过,此处无需再次添加)
4. 使用postman批量刷新,post请求

image-20200922111608448.png http://localhost:1200/actuator/bus-refresh

5 扩展:刷新单个服务

http://localhost:1200/actuator/bus-refresh/user-service:9091

6. bus刷新流程
  1. 使用postman请求config-server配置中心
  2. 配置中心发送更新消息到mq
  3. 用户服务监听到消息
  4. 用户服务从配置中心主动拉取配置文件
  5. 配置中心从gitee获取最新的配置文件