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
  • 配置一个安全认证
目录
相关文章
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
51 0
|
1月前
|
Linux 开发工具 数据安全/隐私保护
【Linux】—— git的管理以及使用
【Linux】—— git的管理以及使用
|
1月前
|
负载均衡 Java API
Spring Cloud 面试题及答案整理,最新面试题
Spring Cloud 面试题及答案整理,最新面试题
133 1
|
1月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
199 0
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
143 0
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
93 0
|
1月前
|
消息中间件 JSON Java
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
470 0
|
7天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
7天前
|
Java API 对象存储
对象存储OSS产品常见问题之使用Spring Cloud Alibaba情况下文档添加水印如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
24 2
|
10天前
|
人工智能 监控 安全
Springcloud数字化物联网智慧工地综合平台源码 劳务管理、设备管理、绿色施工
Springcloud数字化物联网智慧工地综合平台源码 劳务管理、设备管理、绿色施工
14 3

热门文章

最新文章