springcloud config分布式配置中心的搭建

开发环境:

  • 开发工具:Intellij IDEA 2018.3.2
  • springboot: 2.1.2
  • jdk:1.8.0_172
  • maven:3.5.4

步骤

  • 1.Git环境搭建

    使用码云环境搭建git服务器端 

    码云环境地址:

    https://gitee.com/xuluj/SpringCloud-config.git
  • 2.使用 IDEA建立一个maven项目
  • 3.在此maven项目中搭建eureka注册中心

     3.1 在maven项目上右键,依次选择  New-->Model

    3.2项目结构如下

    3.3 application.yml文件配置

    #服务端口号
    server:
    port: 8020
    eureka:
    instance:
    #注册到eureka ip地址
    hostname: 127.0.0.1
    client:
    service-url:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    #自己是注册中心,不需要注册自己
    register-with-eureka: false
    #自己是注册中心,不需要检索服务
    fetch-registry: false


    3.4 eureka注册中心启动类添加

    @EnableEurekaServer注解

    3.5 eureka服务启动类右键,选择run或者debug运行

    3.6 服务启动完毕,浏览器输入localhost:8020 (注:8020为application.yml配置文件配置的服务端口号,至此springcloud config注册中心搭建完毕)

  • 4.在此maven项目中搭建springcloud config服务端
       4.1 在maven项目上右键,依次选择  New-->Model
       

    4.2 项目结构如下

    4.3 application.yml文件配置:

    #服务注册到eureka地址
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:8020/eureka
    spring:
    application:
    #注册中心应用名称
    name: config-service
    cloud:
    config:
    server:
    git:
    ###git环境地址
    uri: https://gitee.com/xuluj/SpringCloud-config.git
    ####搜索目录
    search-paths:
    - config
    ####读取分支
    label: master
    ####端口号
    server:
    port: 8030

    4.4springcloud config服务端启动类:

    4.5 启动服务

    4.6 服务启动完毕,浏览器输入localhost:8020 (注:8020为application.yml配置文件配置的服务端口号,至此springcloud config服务端搭建完毕)

  • 5.springcloud config客户端搭建
       5.1同上4.1,在maven项目右键, 依次选择  New-->Model-->Next-->Next

     

    5.2项目结构如下:

    5.3 bootstrap.yml文件配置:

    ##配置在application.properties/yml都不行,
    ##必须是配置在bootstrap.properties/yml里面,否则还是会取本机的8888端口!!!
    spring:
    application:
    #注册中心服务名称,git服务器文件名称必须是:服务名称+读取的后缀,即config-client-dev
    name: config-client
    cloud:
    config:
    #读取后缀
    profile: dev
    discovery:
    #读取config-server注册地址
    service-id: config-service
    #这里必须配置为true,否则还是会取localhost:8888端口
    enabled: true
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:8020/eureka
    server:
    port: 8040
    #开启监控断点
    management:
    endpoints:
    web:
    exposure:
    include: "*"

    5.4 springcloud-config-client服务启动类:

    5.5 在springcloud-config-client服务中新建ConfigController读取远程git服务器配置文件内容

    5.6 启动springcloud-config-client服务

    5.7 服务启动完毕,浏览器输入localhost:8020 (注:8020为application.yml配置文件配置的服务端口号,至此springcloud config客户端端搭建完毕)

    5.8 测试读取远程git文件内容:

      5.8.1 浏览器输入:localhost:8040/getName

      5.8.2 动态刷新数据

           在SpringCloud config中有手动刷新配置文件和实时刷新配置文件两种方式。

           手动方式采用actuator端点刷新数据

           实时刷新采用SpringCloud Bus消息总线(实时刷新耗内存)

       actuator端点刷新数据

       5.8.2.1 在springcloud-config-client服务中添加Maven依赖信息,重新启动服务

    <!-- actuator监控中心 -->

                                <dependency>

                                              <groupId>org.springframework.boot</groupId>

                                              <artifactId>spring-boot-starter-actuator</artifactId>

                                </dependency>



        5.8.2.2 修改远程git服务器配置文件内容:

      5.8.2.3 手动刷新接口

        Post请求手动刷新(使用postman提交post请求)   http://127.0.0.1:8040/actuator/refresh
       

     5.8.2.4 浏览器输入http://localhost:8040/getName

  •  6.springcloud config分布式配置中心搭建完毕

    注:本人菜鸟一枚,一边学习一边搭建的项目,如有问题请多谅解

猜你喜欢

转载自www.cnblogs.com/xlj-xlj/p/10370090.html