<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 集成 Git 的 Spring Cloud 配置服務器 > 原文: [https://howtodoinjava.com/spring-cloud/spring-cloud-config-server-git/](https://howtodoinjava.com/spring-cloud/spring-cloud-config-server-git/) 現在,微服務方法已成為任何新 API 開發的行業標準,并且幾乎所有組織都在推廣這種方法。 Spring Cloud 提供了出色的工具,可以在 Spring 啟動框架之上構建這些微服務。 在本 **Spring Cloud 配置**教程中,我們將討論稱為**配置服務器**的特定微服務功能。 配置服務器是存儲和維護所有微服務的所有可配置參數的位置。 這更像是將屬性/資源文件從項目代碼庫外部完全外部化到外部服務,以便對任何給定屬性的任何更改都不需要重新部署使用該屬性的服務。 所有此類屬性更改都將反映出來,而無需重新部署微服務。 ## 1\. 為什么要使用 Spring Cloud 配置服務器 配置服務器的思想來自[ 12 因子應用程序](https://12factor.net/config)宣言,該宣言與開發現代云原生應用程序的最佳實踐指南有關。 它建議**將屬性或資源文件從服務器**外部化,在服務器中,這些資源或資源的值在運行時會發生變化 – 通常情況下,不同的配置在每種環境中都會有所不同。 例如,假設一個服務依賴于另一服務(針對特定業務場景調用),并且該依賴服務的 URL 是否更改為其他內容。 然后,通常我們需要使用更新的 URL 構建和部署我們的服務。 現在,如果我們采用 12 要素應用程序方法,并且從外部服務讀取了這些配置屬性,那么我們只需要更新配置服務器中的 URL 并刷新該客戶端服務配置即可使用更新的 URL。 因此,該想法是顯而易見且有效的。 現在讓我們來看如何創建 Spring Cloud 配置服務器。 ## 2\. 技術棧 我們將使用基于 spring boot 的 spring cloud API,該 API 隨時可用且非常受歡迎。 在 Spring 框架命名中,它稱為配置服務器。 另外,我們將使用 git 配置來托管屬性文件。 因此,最后,此演示的技術棧為: 1. [Java 1.8](https://howtodoinjava.com/java-8-tutorial/) 2. Eclipse IDE 3. Spring Cloud 4. [Spring Boot](https://projects.spring.io/spring-boot/) 5. Spring Rest 6. GitHub 作為資源庫 7. [Maven](https://howtodoinjava.com/maven/) 8. REST 客戶端 首先,我們將使用 spring boot 開發兩個微服務。 1. 一個是**配置服務器服務**,在運行時提供配置 2. 一種是**配置客戶端服務**,使用作為配置服務器公開的配置。 ## 3\. 服務器配置 首先,按照給定的步驟構建配置服務器部分: 1. #### 生成項目結構 從[ spring boot 初始化器頁面](https://start.spring.io)開始,這是創建任何基于 spring boot 的應用程序的一個很好的起點。 在這里,我們將僅選擇**配置服務器**啟動器 pom。 屏幕截圖是這樣的。 使用此配置,一旦生成項目,便會下載一個 zip 文件,解壓縮后將其導入 eclipse 中。 ![Generate Server Project with Config Server Starter POM](https://img.kancloud.cn/3a/4a/3a4a9131f2a731adfd13f9211ba9e569_1209x525.jpg) 使用配置服務器啟動器 POM 生成服務器項目 2. #### 在 Eclipse 中導入項目 從 spring 初始化器頁面獲得 zip 文件后,我們需要將其解壓縮到我們選擇的目錄中,然后將其導入作為 Eclipse 作為 Maven 項目。 `pom.xml` ```java <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>Hoxton.RELEASE</spring-cloud.version> </properties> <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> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` 3. #### 啟動 Eclipse 下一步將是從命令提示符處或從 eclipse 運行`mvn clean install`,無論您喜歡什么。 在這一步中,所有必需的依賴項將從 Maven 存儲庫中下載。 確保從沒有下載限制的任何網絡上嘗試該操作。 非常需要成功完成此步驟才能繼續進行下一步。 4. #### 添加`@EnableConfigServer`注解 現在,打開 spring 已經提供的`SpringApplication`類,并在類之前添加[`@EnableConfigServer`](https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnableConfigServer.java)注解,然后再次構建項目。 有了此注解,此工件將充當 spring 配置服務器。 添加此注解后,類將如下所示 – 根據您在生成時提供的項目名稱,類名稱可以不同。 您也可以手動將類名稱更改為您喜歡的名稱。 `SpringConfigServerApplication.java` ```java package com.howtodoinjava.example.springconfigserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class SpringConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringConfigServerApplication.class, args); } } ``` 5. #### git 存儲庫中的客戶端屬性 下一步是創建本地 git 存儲庫。 通過稍后在屬性文件中配置其 URL,可以輕松地將其轉換為遠程存儲庫。 我們將放置外部屬性文件`configuration`,配置服務器微服務將使用該文件來提供屬性的外部配置。 我們需要按照以下步驟創建本地 git 存儲庫并簽入示例屬性文件。 1. 確保您的計算機中安裝了 git shell,并且可以從命令提示符運行 git bash。 要驗證它,請打開命令提示符并鍵入`git`,如果它可以識別該命令,則可能安裝了 git 提示符;如果沒有,請訪問 git 網站,按照說明下載并安裝。 2. 現在,在桌面中創建目錄`config-server-repo`。 3. 然后在`config-server-repo`目錄中創建文件`config-server-client.properties`文件,并在其中添加消息`msg = Hello world - this is from config server`。 4. 然后在`config-server-repo`目錄中創建另一個文件`config-server-client-development.properties`文件,并在其中添加消息`msg = Hello world - this is from config server – Development environment.` 5. 然后在`config-server-repo`目錄中創建另一個文件`config-server-client-production.properties`文件,并在其中添加消息`msg = Hello world - this is from config server – Production environment.` 6. 在這里,我們為不同的環境維護相同的屬性名稱,因為我們通常為不同的環境(例如 url,憑據,數據庫詳細信息等)維護屬性。這里最重要的一點是,我們需要在每個屬性中在環境名稱后附加連字符(`-`) 以便配置服務器可以理解。 另外,我們需要使用將在此之后創建的配置客戶端服務名稱來命名屬性文件。 7. 現在從`config-server-repo`目錄中打開命令提示符,然后運行命令`git init`以將該目錄作為 git 存儲庫。 8. 現在運行`git add .`將所有內容添加到此倉庫中。 9. 最后,我們需要通過運行命令`git commit –m "initial checkin"`來提交屬性文件。 這應該檢入 git 存儲庫中的所有文件。 這是相同的命令提示符屏幕截圖。 ![Property Check-in in Git](https://img.kancloud.cn/5e/9e/5e9e36f98e5ea7eca0e95c3821623525_695x657.jpg) Git 中的屬性檢入 `client-config.properties` ```java msg = Hello world - this is from config server - default profile ``` `client-config-development.properties` ```java msg = Hello world - this is from config server - Development Environment ``` `client-config-production.properties` ```java msg = Hello world - this is from config server - Prodcution Environment ``` 在`properties`文件夾中執行的 Git 命令 ```java $ git init $ git add . $ git commit -m "initial commit" ``` 6. #### 從配置服務器指向 Git 存儲庫 在`spring-config-sever`項目的`src\main\resources`目錄中創建一個名為`bootstrap.properties`的文件,然后添加以下行。 `bootstrap.properties` ```java #Server port server.port = 8888 #Git repo location spring.cloud.config.server.git.uri=E:\\devsetup\\gitworkspace\\spring-cloud\\config-git-repo #Verify any repository issue in service startup spring.cloud.config.server.git.cloneOnStart=true #Disable security of the Management endpoint management.security.enabled=false ``` 現在讓我們了解這些屬性。 * `server.port`定義嵌入式服務器將在其上啟動的端口。 * `spring.cloud.config.server.git.uri`將綁定 git 位置以查找配置。 在這里,我們使用本地 git 倉庫,但只需更改此位置即可將其切換到遠程位置。 * `management.security.enabled=false`將在`/env`,`/refresh`等管理點上禁用 Spring Security。這是用于開發設置的,應啟用生產安全性。 因此,此步驟將指向 git 位置和服務器端口。 以上所有步驟都是我們需要在配置服務器端執行的,現在在此項目上執行最后的`mvn clean install`命令,以便正確編譯所有內容并將其打包在目標文件夾以及本地 Maven 存儲庫中。 準備好客戶端部分后,我們將啟動配置服務器服務,然后我們將最終測試該功能。 7. #### 驗證配置 在嵌入式模式下運行服務的命令是`java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar`,但是我們將在測試部分中重新訪問它。 要檢查配置服務器是否可以識別屬性,請首先使用項目代碼庫位置的命令提示符下的給定命令,從命令提示符下運行配置服務器微服務。 ```java java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar ``` 現在打開瀏覽器并檢查下面的 URL,它將返回 JSON 輸出,在`propertySources`部分中,我們可以看到我們在屬性中添加的所有屬性。 這可以確保`config-server`成功運行,它已經識別了 git 位置,并且正在為不同環境提供配置。 * `http://localhost:8888/client-config/development` * `http://localhost:8888/client-config/production` 另外,要檢查服務器是否反映了屬性文件中的任何更改而無需重新啟動,請更改任何環境的屬性和簽入屬性文件的值。 然后運行該特定環境的終結點,并驗證更改后的屬性值應立即反映出來而無需重新啟動服務器。 要進行 git 簽入,請在完成更改并通過任何文本編輯器保存文件后,運行命令`git add .`和`git commit -m "test"` ## 4\. 客戶端配置 現在,我們將進行客戶端實現,在此我們將使用來自單獨的微服務的那些屬性,這是我們的最終目標 – 將配置外部化為不同的服務。 1. #### 創建 Maven 項目 轉到 [https://start.spring.io/](https://start.spring.io/) Web 頁面,并使用以下選定的工件生成客戶端項目: 1. 執行器 2. 配置客戶端 3. 網頁 4. 其余存儲庫 生成前的屏幕如下所示; 單擊生成后,我們將獲得`.zip`文件下載選項。 像`Spring-Config-Server`一樣,將文件解壓縮到某個目錄中,然后將其導入 eclipse 中。 `pom.xml` ```java <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>Hoxton.RELEASE</spring-cloud.version> </properties> <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> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` ![Generate Client Project with Listed Dependencies](https://img.kancloud.cn/31/a2/31a297ab7ef119836b617363a0aa6bb7_1250x613.jpg) 生成具有列出依賴項的客戶端項目 2. #### 創建 REST 資源 添加一個[`RestController`](https://howtodoinjava.com/spring/spring-boot/spring-boot-tutorial-with-hello-world-example/)以查看響應中的服務器端屬性值。 為此,請打開已生成的[`@SpringBootApplication`](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html)類文件,并將以下小類添加到該文件的末尾。 這非常簡單明了,我們只是在`/message` URL 處公開一種方法,我們將只返回由配置服務器微服務提供的`msg`的屬性值,該屬性值已配置到本地 git 存儲庫(將會遷移到生產環境中的遠程 git 存儲庫!)。 `SpringConfigClientApplication.java` ```java package com.howtodoinjava.example.springconfigclient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class SpringConfigClientApplication { public static void main(String[] args) { SpringApplication.run(SpringConfigClientApplication.class, args); } @Autowired public void setEnv(Environment e) { System.out.println(e.getProperty("msg")); } } @RefreshScope @RestController class MessageRestController { @Value("${msg:Config Server is not working. Please check...}") private String msg; @GetMapping("/msg") public String getMsg() { return this.msg; } } ``` 3. #### 與配置服務器綁定 在`src\main\resources`目錄中創建一個名為`bootstrap.properties`的文件,并添加以下屬性以與配置服務器以及一些必需的配置連接。 `bootstrap.properties` ```java spring.application.name=client-config #Active Profile - will relate to development properties file in the server. #If this property is absent then,default profile will be activated which is #the property file without any environment name at the end. spring.profiles.active=development # N.B. this is the default: spring.cloud.config.uri=http://localhost:8888 management.security.enabled=false ``` 現在讓我們了解屬性。 * `spring.application.name`只是將要部署的微服務的應用程序名稱。 * `spring.cloud.config.uri`是提及配置服務器 URL 的屬性。 請注意,我們的配置服務器在端口`8888`上運行; 通過打開 spring 配置服務器代碼庫的`application.properties`文件進行驗證,然后檢查`server.port=8888`。 * `management.security.enabled=false`將在諸如`/env`,`/refresh`等管理端點上禁用 Spring Security。這是用于開發設置的,應啟用生產安全性。 4. #### 驗證客戶端配置 這是我們需要在配置客戶端執行的,而不是在此項目上執行最終的`mvn clean install`命令,以便正確編譯所有內容并將其打包在目標文件夾以及本地 Maven 存儲庫中。 我們將與服務器端一起啟動配置客戶端服務,最后我們將測試該功能。 ## 5\. 演示 讓我們測試配置服務器應用程序。 * 生成并運行配置服務器項目 從`spring-config-server`文件夾中打開命令提示符,然后運行`mvn clean install`命令。 一旦構建完成,就可以通過`java -jar`命令(例如`java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar`)從該命令提示符本身運行應用程序。 這將在本地主機的 8888 端口中啟動配置服務器服務。 * 生成并運行配置客戶端項目 同樣,從`spring-config-client`文件夾中打開命令提示符,然后運行`mvn clean install`命令。 一旦構建完成,就可以通過`java -jar`命令(例如`java -jar target\spring-config-client-0.0.1-SNAPSHOT.jar`)從該命令提示符本身運行應用程序。 這將在`localhost`的 8080 端口中啟動配置客戶端服務。 * 測試 REST 端點 現在,在瀏覽器中,通過瀏覽 URL `http://localhost:8080/msg`打開`/msg` REST 端點。 它應該返回`config-server-client-development.properties`文件中提到的`Hello world - this is from config server`。 ![Test REST End Point](https://img.kancloud.cn/77/ee/77eebdc4c298feee14f1ec79453b3e78_526x103.jpg) REST 端點 * 測試屬性更改 現在,我們將進行屬性更改,并測試該更改是否可以反映在配置客戶端服務中,而無需重新啟動任何微服務。 進行一些更改,在`config-server-client-development.properties` 中的`msg`屬性的值中,并在本地 git 中簽入,然后在瀏覽器中再次單擊`http://localhost:8080/msg`,您將只使用舊值。 為了反映新值,我們需要通過從任何 REST 客戶端使用`POST`方法命中`http://localhost:8080/refresh`端點來**刷新配置**。 成功刷新配置客戶端服務后,新值應反映在服務響應中。 這是因為[`@RefreshScope`](https://cloud.spring.io/spring-cloud-static/docs/1.0.x/spring-cloud.html#_refresh_scope)注解了我們已經公開的 Rest 控制器。 ## 6\. 檢查是否遇到任何錯誤的東西 * 屬性文件名稱和客戶端模塊服務名稱`spring.application.name = config-server-client`應該完全相同,否則,將不會檢測到屬性。 實際上,配置服務器在屬性文件名的端點公開屬性,如果瀏覽URL`http://localhost:8888/config-server-client/development`,它將返回所有 Dev 環境值。 ![All Dev Properties View](https://img.kancloud.cn/be/cb/becbf07dc6e9ee130ab0e782c7a06afc_886x444.jpg) 所有 Dev 屬性視圖 * 確保如上所述使用`git init/add/commit`命令在 git 倉庫中簽入屬性文件。 * 通過任何 REST 客戶端調用`http://localhost:8080/refresh`的`POST`方法,確保已刷新客戶端服務環境。 否則,更改后的值將不會反映在客戶端服務中。 * 確保在啟動配置客戶端服務時,配置服務器服務已在運行。 否則,注冊可能會花費一些時間,這可能會在測試時造成混亂。 這就是為微服務創建配置服務器。 如果您在配置本文中提到的所有要點時遇到任何困難,請添加評論,我們很樂意調查問題。 [配置服務器源碼](https://github.com/lokeshgupta1981/spring-cloud/tree/master/spring-config-server) [配置客戶端源碼](https://github.com/lokeshgupta1981/spring-cloud/tree/master/spring-config-client) [下載 Git 倉庫](https://github.com/lokeshgupta1981/spring-cloud/tree/master/config-git-repo) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看