SpringCloud Git配置管理

本文详细介绍了如何使用SpringCloud配置中心进行微服务配置管理,包括创建远程仓库、配置文件命名规则、配置中心微服务工程搭建及依赖添加。通过具体步骤说明了如何实现不同环境下的配置分离,以及如何通过配置中心获取配置文件信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringCloud Git配置管理

码云访问地址:https://gitee.com/

创建远程仓库

​ 首先要使用码云上的私有远程git仓库需要先注册帐号;请先自行访问网站并注册帐号,然后使用帐号登录码云控制 台并创建公开仓库。

创建配置文件

命名方式:

{application}-{profile}.yml

application为应用名称

profile用于区分开发环境,测试环境、生产环境等

实例:user-dev.yml 来表示用户微服务开发环境下使用的配置文件。

将service工程的application.yml配置内容拷贝到user-dev.yml中

创建配置中心微服务工程:

依赖:

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

创建配置中心工程 config-server 的启动类

@SpringBootApplication
@EnableConfigServer //开启配置服务
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
	}
}
server:
	port: 12000
spring:
	application:
		name: config-server
	cloud:
		config:
		server:
			git:
				uri: https://gitee.com/*****
eureka:
	client:
		service-url:
			defaultZone: http://127.0.0.1:10086/eureka

https://gitee.com/ 是在码云创建的仓库地址

可以通过访问localhost:12000/user-dev.yml来得到配置文件的信息

添加依赖

在 user-service 工程中的pom.xml文件中添加如下依赖:

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

修改配置

将service层的application.yml配置文件删除,新建bootstrap.yml配置文件。

spring:
    cloud:
        config:
        # 要与仓库中的配置文件的application保持一致
        name: user
        # 要与仓库中的配置文件的profile保持一致
        profile: dev
        # 要与仓库中的配置文件所属的版本(分支)一样
        label: master
        discovery:
        # 使用配置中心
        enabled: true
        # 配置中心服务名
        service-id: config-server
eureka:
    client:
        service-url:
            defaultZone: http://127.0.0.1:10086/eureka

​ bootstrap.yml文件也是Spring Boot的默认配置文件,而且其加载的时间相比于application.yml更早。

​ bootstrap.yml文件相当于项目启动时的引导文件,内容相对固定。application.yml文件是微服务 的一些常规配置参数,变化比较频繁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值