Spring Cloud 配置文件管理 - Config

Spring Cloud 配置文件管理 - Config

1587795739754

  • Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护。

  • 好处:
    集中管理配置文件
    不同环境不同配置,动态化的配置更新
    配置信息改变时,不需要重启即可更新配置信息到服务

2.2-config-快速入门

2.2.1-gitee搭建远程仓库

1.编写仓库名称、仓库路径、公开(公开的比较方便)

1587796233360

2.语言和模板可以不选,一般使用单分支模型,只创建master分支

1587796290872

3.使用小乌龟工具,将远程仓库clone到本地

1587796447003

4.使用小乌龟工具将配置文件提交到远程仓库

1587796545052

1587796586956

2.2.2-config server搭建

config server:

  1. 使用gitee创建远程仓库,上传配置文件

  2. 搭建 config server 模块

  3. 导入 config-server 依赖

    <dependencies>
        <!-- config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    
  4. 编写配置,设置 gitee 远程仓库地址

server:
  port: 9527

spring:
  application:
    name: config-server
  # spring cloud config
  cloud:
    config:
      server:
        # git 的 远程仓库地址
        git:
          uri: https://gitee.com/itheima_cch/itheima-configs.git
      label: master # 分支配置

  1. 测试访问远程配置文件

1587797404156

2.2.3-config client搭建

config client:

  1. 导入 starter-config 依赖

       <!--config client -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    
  2. 配置config server 地址,读取配置文件名称等信息

    创建配置文件bootstrap.yml

    # 配置config-server地址
    # 配置获得配置文件的名称等信息
    spring:
      cloud:
        config:
          # 配置config-server地址
          uri: http://localhost:9527
          # 配置获得配置文件的名称等信息
          name: config # 文件名
          profile: dev # profile指定,  config-dev.yml
          label: master # 分支
    
  3. 获取配置值

        @Value("${itheima}")
        private String itheima;
    
  4. 启动测试

1587797821512

2.2.4-config client刷新

  1. 在 config 客户端引入 actuator 依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  2. 获取配置信息类上,添加 @RefreshScope 注解

/**
 * Goods Controller 服务提供方
 */

@RestController
@RequestMapping("/goods")
@RefreshScope // 开启刷新功能
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Value("${server.port}")
    private int port;


    @Value("${itheima}")
    private String itheima;
    ...
    }
  1. 添加配置management.endpoints.web.exposure.include: refresh 配置暴露的端点
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
  cloud:
    config:
      # 配置config-server地址
      uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  config-dev.yml
      label: master # 分支

management:
  endpoints:
    web:
      exposure:
        include: refresh

  1. 使用curl工具发送post请求
    curl -X POST http://localhost:8001/actuator/refresh

1587798535633

2.3-config集成Eureka

1587798585588

1.config-server pom.xml中引入eureka-client 坐标

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

2.配置文件中配置eureka地址

eureka:
	client:
		service-url:
			defaultZone: http://localhost:8761/eureka/

3.启动类中添加注解

package com.itheima.config;

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

@SpringBootApplication
@EnableConfigServer // 启用config server功能
@EnableEurekaClient
public class ConfigServerApp {

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

4.config-provider 工程中bootstrap.yaml中注掉写死的地址,改为从Eureka中获取

# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
  cloud:
    config:
      # 配置config-server地址
      #uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  config-dev.yml
      label: master # 分支
      #从注册中心获取config-server地址
      discovery:
      	enabled:true
      	service-id:CONFIG-SERVER
posted @ 2021-01-25 17:12  60kmph  阅读(371)  评论(0编辑  收藏  举报