1. 程式人生 > 其它 >leetcode86——分隔連結串列——java實現

leetcode86——分隔連結串列——java實現

技術標籤:Spring Cloud

對於一些簡單的專案來說,我們一般都是直接把相關配置放在單獨的配置檔案中,以 properties 或者 yml 的格式出現,更省事兒的方式是直接放到 application.properties 或 application.yml 中。但是這樣的方式有個明顯的問題,那就是,當修改了配置之後,必須重啟服務,否則配置無法生效。

一、實現最簡單的配置中心

最簡單的配置中心,就是啟動一個服務作為服務方,之後各個需要獲取配置的服務作為客戶端來這個服務方獲取配置。

現在github中建立配置檔案,我這裡使用的是碼雲:https://gitee.com/

新建一個倉庫SpringCloudConfig,在倉庫根路徑下建立一個資料夾config,目錄結構如下:

配置檔案的內容大致如下,用於區分,略有不同。

data:
  env: config-single-dev
  user:
    username: single-client-user
    password: 1291029102

注意檔案的名稱不是亂起的,例如上面的 config-single-client-dev.yml 和 config-single-client-prod.yml 這兩個是同一個專案的不同版本,專案名稱為 config-single-client(spring.application.name配置的值), 一個對應開發版,一個對應正式版。

建立配置中心服務端

1、新建 Spring Boot 專案,引入 config-server 和 starter-web

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

<!-- spring cloud config 服務端包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2、配置 config 相關的配置項

bootstrap.yml 檔案:

spring:
  application:
    name: config-single-server  # 應用名稱
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/linhongwei/SpringCloudConfig #配置檔案所在倉庫
          username: github 登入賬號
          password: github 登入密碼
          default-label: master #配置檔案分支
          search-paths: config  #配置檔案所在根目錄

application.yml:

server:
  port: 3301

3、在啟動類上增加相關注解 @EnableConfigServer

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}