springboot配置文件抽离--git管理统一配置中心

springboot配置文件抽离,便于服务器读取对应配置文件,避免项目频繁更改配置文件,影响项目的调试与发布

1.创建统一配置中心项目conifg

1)pom配置依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>

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

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

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

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

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2)yml文件配置
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/XXXX/XXXXXX.git
username: XXXXXXX
password: XXXXXXXXX
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
management:
endpoints:
web:
expose: "*"

2.创建git私有项目config-repo 用于存放配置文件

3.配置项目 可以看到对应的配置文件内容
http://localhost:8002/XXXXX/user-dev.yml

4.配置客户端读取配置文件
1)客户端配置pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2)客户端yml文件配置
spring:
application:
name: XXXXXX
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: dev



eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1 # 单机时关闭eureka 保护模式
lease-expiration-duration-in-seconds: 2


转载于:https://www.cnblogs.com/flyyu1/p/11445723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值