欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-25 21:52:33
springboot配置文件抽离,便于服务器读取对应配置文件,避免项目频繁更改配置文件,影响项目的调试与发布 1.创建统一配置中心项目conifg 1)pom配置依赖...

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

以上就是本次介绍的关于springboot配置文件抽离 git管理统 配置中心全部知识点内容,感谢大家对的支持。