Spring Cloud 配置文件管理 - Config

spring cloud 配置csrf spring cloud 配置文件管理工具_spring

  • Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护。
  • 好处:
    集中管理配置文件
    不同环境不同配置,动态化的配置更新
    配置信息改变时,不需要重启即可更新配置信息到服务

2.2-config-快速入门

2.2.1-gitee搭建远程仓库

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

spring cloud 配置csrf spring cloud 配置文件管理工具_spring_02

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

spring cloud 配置csrf spring cloud 配置文件管理工具_spring_03

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

spring cloud 配置csrf spring cloud 配置文件管理工具_spring cloud 配置csrf_04

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

spring cloud 配置csrf spring cloud 配置文件管理工具_git_05

spring cloud 配置csrf spring cloud 配置文件管理工具_spring_06

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>
  1. 编写配置,设置 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. 测试访问远程配置文件

spring cloud 配置csrf spring cloud 配置文件管理工具_配置文件_07

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>
  1. 配置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 # 分支
  1. 获取配置值
@Value("${itheima}")
    private String itheima;
  1. 启动测试

spring cloud 配置csrf spring cloud 配置文件管理工具_spring_08

2.2.4-config client刷新

  1. 在 config 客户端引入 actuator 依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 获取配置信息类上,添加 @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

spring cloud 配置csrf spring cloud 配置文件管理工具_git_09

2.3-config集成Eureka

spring cloud 配置csrf spring cloud 配置文件管理工具_spring cloud 配置csrf_10

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