SpringCloud 配置文件交给 git 管理

简介: SpringCloud config git
  1. gitHub中创建项目并存放配置文件
  2. 搭建一个注册中心
  3. 搭建一个服务git仓库进行连接
  4. 搭建一个服务通过仓库连接服务调用配置文件

架构图

SpringCloudConfigGit

gitHub中创建项目并存放配置文件

SpringCloudConfig

搭建一个注册中心 :: 服务注册中的地址

eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/

搭建一个服务git仓库进行连接

pom 文件中添加依赖

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

添加application.yaml配置文件

server:
  port: 8000

spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/jiangruyi/SpringCloud.git
          search-paths: spring-cloud-config-core
          username: 2491920818@qq.com
          password: xxx

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

编写SpringBoot启动类

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

搭建一个服务通过仓库连接服务调用配置文件

添加 pom 依赖

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

编写 bootstrap.yaml 配置文件

spring:
  cloud:
    config:
      name: application
      profile: dev
      uri: http://localhost:8000/
      label: master

server:
  port: 9000

spring.cloud.config.uri :: 与git连接的服务地址

编写基本配置文件 application.yaml

spring:
  application:
    name: config-client-git

server:
  port: 9000

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

编写 SpringBoot 启动类读取git上的配置文件

@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class Application {

    @Value("${com.znsd.config}")
    private String gitValue;
    
    public void setGitValue(String gitValue) {
        this.gitValue = gitValue;
    }
    
    @GetMapping("hello")
    public String hello () {
        return gitValue;
    }
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

配置热部署

在调用配置服务端 pom 文件中添加依赖

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

在要动态热部署的配置类中添加: @RefreshScope 注解

POST 方式访问URL http://localhost:9000/refresh刷新配置

注意:

返回消息中包含: Full authentication is required to access this resource.

解决方案:

  • 将安全认证关掉: management.security.enabled=false
  • 配置一个安全认证
目录
相关文章
|
2月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
2月前
|
Java 开发工具 git
实现基于Spring Cloud的配置中心
实现基于Spring Cloud的配置中心
|
2月前
|
设计模式 监控 Java
解析Spring Cloud中的断路器模式原理
解析Spring Cloud中的断路器模式原理
|
2月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14613 24
|
2月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
290 15
|
2月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
61 3
|
2月前
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
145 5
|
2月前
|
Java API 开发工具
Spring Boot与Spring Cloud Config的集成
Spring Boot与Spring Cloud Config的集成
|
2月前
|
存储 安全 Java
实现基于Spring Cloud的分布式配置管理
实现基于Spring Cloud的分布式配置管理