Spring Boot - 雲配置伺服器



Spring Cloud 配置伺服器是一個集中式應用程式,用於管理所有與應用程式相關的配置屬性。在本節中,您將詳細瞭解如何建立 Spring Cloud 配置伺服器。

建立 Spring Cloud 配置伺服器

首先,從 Spring Initializer 頁面下載 Spring Boot 專案,並選擇 Spring Cloud Config Server 依賴項。請觀察下面的螢幕截圖:

Creating Spring Cloud Configuration Server

現在,按照如下說明在您的構建配置檔案中新增 Spring Cloud Config 伺服器依賴項:

Maven 使用者可以將以下依賴項新增到 pom.xml 檔案中。

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

Gradle 使用者可以將以下依賴項新增到 build.gradle 檔案中。

compile('org.springframework.cloud:spring-cloud-config-server')

現在,在您的主 Spring Boot 應用程式類檔案中新增 `@EnableConfigServer` 註解。`@EnableConfigServer` 註解使您的 Spring Boot 應用程式充當配置伺服器。

主 Spring Boot 應用程式類檔案如下所示:

ConfigserverApplication.java

package com.tutorialspoint.configserver;

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);
   }
}

現在,將以下配置新增到您的 application.properties 檔案中。請觀察以下程式碼:

application.properties

server.port = 8888
spring.cloud.config.server.git.uri=file:///E:/Dev/config/

配置伺服器執行在 Tomcat 埠 8888 上,應用程式配置屬性從基於 Git 的本地檔案系統搜尋位置載入。

建立 Git 倉庫

初始化 Git 倉庫

轉到 **E:/Dev/config/** 資料夾,並執行以下 git 命令將其初始化為 git 倉庫。

git init

新增屬性檔案

現在,在 **E:/Dev/config/** 中,放置您的客戶端應用程式 - application.properties 檔案。例如,您的客戶端應用程式名稱為 **config-client**,然後將您的 application.properties 檔案重新命名為 **config-client.properties** 並將屬性檔案放置在 **E:/Dev/config/** 路徑上。

config-client 屬性檔案的程式碼如下所示:

welcome.message = Welcome to Spring cloud config server

執行以下 git 命令來暫存所有更改。

E:\Dev\config> git add .

提交更改

執行以下 git 命令來提交更改。

E:\Dev\config> git commit -m "First Checkin"

驗證更改

執行以下 git 命令來檢查已提交的更改。

E:\Dev\config> git log
commit 8081e552232ca5b1af29cef56e6acc6e1a5bd2e3 (HEAD -> master)
Author: maheshparashar84 <mahesh.kumar@tutorialspoint.com>
Date:   Thu Sep 12 11:38:28 2024 +0530

    First Checkin

E:\Dev\config>

完整的構建配置檔案如下所示:

Maven 使用者可以使用以下 **pom.xml**:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.3.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>configserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>configserver</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>21</java.version>
      <spring-cloud.version>2023.0.3</spring-cloud.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </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>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Gradle 使用者可以使用以下 build.gradle 檔案:

build.gradle

<scope>import</scope>
</dependency>
</dependencies>
buildscript {
   ext {
      springBootVersion = '3.3.3'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = '2023.0.3'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-config-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

編譯和執行

現在,建立一個可執行 JAR 檔案,並使用以下 Maven 或 Gradle 命令執行 Spring Boot 應用程式:

對於 Maven,使用以下命令:

mvn clean install

“BUILD SUCCESS”後,您可以在 target 目錄下找到 JAR 檔案。

對於 Gradle,使用以下命令:

gradle clean build

“BUILD SUCCESSFUL”後,您可以在 build/libs 目錄下找到 JAR 檔案。

使用以下命令執行 JAR 檔案:

 java –jar <JARFILE> 

現在,應用程式已在 Tomcat 埠 8888 上啟動,如下所示:

 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.3.3)[0;39m

[2m2024-09-12T11:42:15.720+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mc.t.c.ConfigserverApplication           [0;39m [2m:[0;39m Starting ConfigserverApplication using Java 21.0.3 with PID 13108 (E:\Dev\configserver\target\classes started by Tutorialspoint in E:\Dev\configserver)
[2m2024-09-12T11:42:15.724+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mc.t.c.ConfigserverApplication           [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2024-09-12T11:42:16.619+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.s.cloud.context.scope.GenericScope    [0;39m [2m:[0;39m BeanFactory id=f7967b62-068c-32b4-9d6a-b8da96911a03
[2m2024-09-12T11:42:16.896+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8888 (http)
[2m2024-09-12T11:42:16.918+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2024-09-12T11:42:16.918+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine   [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.28]
[2m2024-09-12T11:42:16.978+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2024-09-12T11:42:16.978+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1197 ms
[2m2024-09-12T11:42:17.716+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8888 (http) with context path '/'
[2m2024-09-12T11:42:17.739+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mc.t.c.ConfigserverApplication           [0;39m [2m:[0;39m Started ConfigserverApplication in 2.497 seconds (process running for 4.133)
[2m2024-09-12T11:42:43.468+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2024-09-12T11:42:43.468+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2024-09-12T11:42:43.468+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 0 ms
[2m2024-09-12T11:42:47.289+05:30[0;39m [33m WARN[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36m.c.s.e.MultipleJGitEnvironmentRepository[0;39m [2m:[0;39m Could not merge remote for master remote: null
[2m2024-09-12T11:42:47.364+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.s.c.c.s.e.NativeEnvironmentRepository [0;39m [2m:[0;39m Adding property source: Config resource 'file [E:\Dev\config\config-client.properties]' via location 'file:/E:/Dev/config/'

現在,在您的 Web 瀏覽器上訪問 URL **https://:8888/config-client/default/master**,您將看到您的 config-client 應用程式配置屬性,如下所示。

Config-Client Application
廣告
© . All rights reserved.