springboot配置文件抽离 git管理统 配置中心详解

作者:yu_fly 时间:2021-05-24 08:17:18 

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

标签:springboot,配置文件,git管理统,配置中心
0
投稿

猜你喜欢

  • CSS布局之浮动(二)三列浮动

    2008-08-19 12:47:00
  • 详解python string类型 bytes类型 bytearray类型

    2023-09-22 20:47:18
  • win10从零安装配置pytorch全过程图文详解

    2022-07-01 20:54:55
  • PHP json_encode中文乱码解决方法

    2023-07-12 20:20:14
  • Python Xml文件添加字节属性的方法

    2023-08-27 03:48:31
  • asp如何设定程序的执行次数?

    2010-05-18 18:31:00
  • Python使用reportlab模块生成PDF格式的文档

    2021-05-25 22:17:31
  • python接口测试返回数据为字典取值方式

    2021-09-27 07:37:00
  • 基于Keras的格式化输出Loss实现方式

    2021-10-20 20:44:00
  • 如何利用Python动态模拟太阳系运转

    2022-01-14 15:01:43
  • Python中bisect的使用方法

    2021-12-03 05:56:12
  • 关于python中time和datetime的区别与用法

    2022-07-15 00:54:17
  • Discuz!NT 论坛整合ASP程序论坛

    2011-03-31 10:40:00
  • PHP基于非递归算法实现先序、中序及后序遍历二叉树操作示例

    2023-08-16 04:46:47
  • Python实现操作Redis所有类型的方法详解

    2022-05-11 13:48:35
  • Python之列表的插入&替换修改方法

    2023-04-15 00:38:27
  • python编程之requests在网络请求中添加cookies参数方法详解

    2023-07-03 00:48:23
  • Python三元运算与lambda表达式实例解析

    2023-06-14 16:34:09
  • python工具之清理 Markdown 中没有引用的图片

    2022-02-14 11:44:39
  • 表格梳理解析python内置时间模块看完就懂

    2023-10-21 08:10:27
  • asp之家 网络编程 m.aspxhome.com