溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Boot中配置外部化配置中心

發(fā)布時間:2024-10-05 13:05:00 來源:億速云 閱讀:81 作者:小樊 欄目:編程語言

在 Spring Boot 中,可以通過以下步驟配置外部化配置中心(例如:Spring Cloud Config Server):

  1. 引入依賴

在 Spring Boot 項(xiàng)目的 pom.xml 文件中,添加 Spring Cloud Config Server 的依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 配置文件

在 Spring Boot 項(xiàng)目的 application.propertiesapplication.yml 文件中,添加以下配置:

對于 application.properties 文件:

spring.cloud.config.server.git.uri=https://github.com/your-repo/your-config-repo.git

對于 application.yml 文件:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-repo/your-config-repo.git

https://github.com/your-repo/your-config-repo.git 替換為你的 Git 倉庫地址。

  1. 啟動類

確保你的 Spring Boot 項(xiàng)目的主類上添加了 @EnableConfigServer 注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  1. 配置客戶端

在客戶端 Spring Boot 項(xiàng)目中,添加以下依賴:

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

在客戶端項(xiàng)目的 bootstrap.propertiesbootstrap.yml 文件中,添加以下配置:

對于 bootstrap.properties 文件:

spring.cloud.config.uri=http://localhost:8888

對于 bootstrap.yml 文件:

spring:
  cloud:
    config:
      uri: http://localhost:8888

http://localhost:8888 替換為你的 Config Server 地址。

現(xiàn)在,客戶端項(xiàng)目將從外部化配置中心獲取配置信息。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI