<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用 Netflix Eureka 進行 Spring Cloud 服務發現 > 原文: [https://howtodoinjava.com/spring-cloud/spring-cloud-service-discovery-netflix-eureka/](https://howtodoinjava.com/spring-cloud/spring-cloud-service-discovery-netflix-eureka/) 學習在 [Netflix Eureka](https://github.com/Netflix/eureka) 注冊表服務器上基于 [Spring cloud](https://projects.spring.io/spring-cloud) 創建[微服務](https://howtodoinjava.com/microservices/microservices-definition-principles-benefits/),以及其他微服務(Eureka 客戶端)如何使用它注冊和發現服務來調用他們的 API。 我們將使用基于 Spring Boot 的 Spring Cloud API。 我們將使用 Netflix Eureka 服務器來構建服務注冊服務器,并使用 Eureka 客戶端進行注冊并發現其他服務以調用 REST API。 ## 總覽 我們將為此 **Netflix Eureka 示例**創建三個微服務。 1. **Eureka 服務注冊表服務器**-此微服務將提供服務注冊表和發現服務器。 2. **學生微服務** – 這將提供一些基于學生實體的功能。 這將是一個基于 REST 的服務,最重要的是它將是一個 eureka 客戶服務,它將與 eureka 服務對話以在服務注冊表中注冊自己。 3. **學校微服務** – 與學生服務的類型相同 – 唯一增加的功能是它將使用服務查找機制調用學生服務。 我們不會使用學生服務的絕對 URL 與該服務進行交互。 這是上面列出的三個服務之間的交互圖。 ![](https://img.kancloud.cn/02/2c/022c4abde4cc828b7c9d12c8afe0a80e_743x312.jpg) 組件之間的交互 #### 技術棧和運行時 * Java 1.8 * Eclipse IDE * SpringCloud * SpringBoot * SpringRest * Maven ## 什么是 Netflix Eureka 服務器和客戶端? 眾所周知,如今,微服務的發展勢頭非常強勁。 從單片架構過渡到基于微服務的體系結構在可維護性,可伸縮性,高可用性等切面為將來帶來了許多好處。但是,與此同時,在進行這種遷移時也面臨許多挑戰。 其中之一是維護單個微服務地址。 這項任務可能非常復雜-取決于服務的數量及其動態性質。 如果整個基礎架構都是分布式的,并且還存在一些復制,那么維護該服務地址將變得更加困難。 為了解決這個問題,在分布式計算中有一個稱為“服務注冊和發現”的概念,其中一個專用服務器負責維護已部署和刪除的所有微服務的注冊表。 這就像所有其他應用程序/微服務的電話簿一樣。 可以將其視為微服務(客戶端)可以注冊自己并發現其他已注冊微服務的查找服務。 當客戶端微服務向 Eureka 注冊時,它會提供元數據,例如主機,端口和運行狀況指示器,從而允許其他微服務發現它。 發現服務器期望來自每個微服務實例的常規心跳消息。 如果實例開始始終無法發送心跳,發現服務器將從其注冊表中刪除該實例。 這樣,我們將擁有一個非常穩定的微服務生態系統,彼此協作,并且最重要的是,我們不必手動維護其他微服務的地址,如果頻繁進行規模擴大/縮減,則這是幾乎不可能完成的任務 ,根據需要,我們使用虛擬主機專門在云環境中托管服務。 ## Eureka 服務注冊表服務器 請按照以下步驟創建和運行 Eureka 服務器。 #### 創建 Eureka 服務器 從[ Spring Boot 初始化器頁面](https://start.spring.io/)創建一個具有兩個依賴項的 Spring Boot 項目,即`Eureka server`和`Actuator`。 給出其他 Maven GAV 坐標并下載項目。 ![](https://img.kancloud.cn/34/70/3470d82e538662c8201c6c7b38e571ea_1267x703.jpg) Eureka 服務器的服務項目生成 將項目解壓縮并將其作為現有的 maven 項目導入 Eclipse。 在此步驟中,將從 maven 存儲庫下載所有必需的依賴項。 現在打開在下載的項目中已經生成的`SpringEurekaServerApplication`類,并在該類上添加[`@EnableEurekaServer`](https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EnableEurekaServer.java)注解。 ```java package com.example.howtodoinjava.springeurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class SpringEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringEurekaServerApplication.class, args); } } ``` 再次構建項目。 使用此注解,此工件將充當微服務注冊表和發現服務器。 #### 服務器配置 在`src\main\resources`目錄中創建一個名為`application.yml`的文件。 添加這些屬性: ```java server: port: ${PORT:8761} # Indicate the default PORT where this service will be started eureka: client: registerWithEureka: false #telling the server not to register himself in the service registry fetchRegistry: false server: waitTimeInMsWhenSyncEmpty: 0 #wait time for subsequent sync ``` 在`src\main\resources`目錄中創建另一個名為`bootstrap.yml`的文件。 添加這些屬性: ```java spring: application: name: eureka cloud: config: uri: ${CONFIG_SERVER_URL:http://localhost:8888} ``` #### 測試 Eureka 服務器 作為 Spring Boot 應用程序啟動該應用程序。 打開瀏覽器并轉到`http://localhost:8761/`,您應該看到如下所示的 eureka 服務器主頁。 ![](https://img.kancloud.cn/d3/42/d342359b9f4674b30f70940ae786d180_1344x699.jpg) 沒有任何客戶端的 Eureka 控制臺 請注意,目前尚未在此處注冊任何服務,這是我們期望的,一旦我們啟動客戶端服務,此服務器將自動更新客戶端服務的詳細信息。 ## Eureka 客戶端 – 學生服務 請按照以下步驟創建并運行運行學生服務的 Eureka 客戶端。 #### 創建 Eureka 客戶端項目 從具有四個依賴項的初始化器頁面創建 Spring Boot 項目,即`Actuator`,`Web`,`Rest Repositories`和`Eureka Discovery`。 給出其他 Maven GAV 坐標并下載項目。 ![](https://img.kancloud.cn/d6/68/d668c7b2f8ec8eae9331907bfc995f73_1357x692.jpg) 客戶端項目生成:學生微服務 將項目解壓縮并將其作為現有的 maven 項目導入 Eclipse。 現在,在`src`文件夾中的 Spring 運行應用程序類上添加[`@EnableEurekaClient`](https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EnableEurekaClient.java)注解。 使用此注解,此工件將充當 Spring Discovery 客戶端,并在連接到此服務的 eureka 服務器中進行注冊。 ```java package com.example.howtodoinjava.springeurekaclientstudentservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class SpringEurekaClientStudentServiceApplication { public static void main(String[] args) { SpringApplication.run(SpringEurekaClientStudentServiceApplication.class, args); } } ``` #### 客戶端配置 在`src\main\resources`目錄中創建一個名為`application.yml`的文件,然后添加以下幾行。 ```java server: port: 8098 #default port where the service will be started eureka: #tells about the Eureka server details and its refresh time instance: leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 2 client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/ healthcheck: enabled: true lease: duration: 5 spring: application: name: student-service #current service name to be used by the eureka server management: security: enabled: false #disable the spring security on the management endpoints like /env, /refresh etc. logging: level: com.example.howtodoinjava: DEBUG ``` #### 添加 REST API 現在添加一個`RestController`并公開一個 REST 端點,以獲取特定學校的所有學生詳細信息。 在這里,我們公開`/getStudentDetailsForSchool/{schoolname}`端點來滿足業務目的。 為簡單起見,我們正在對學生詳細信息進行硬編碼。 ```java package com.example.howtodoinjava.springeurekaclientstudentservice.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.example.howtodoinjava.springeurekaclientstudentservice.domain.Student; @RestController public class StudentServiceController { private static Map<String, List<Student>> schooDB = new HashMap<String, List<Student>>(); static { schooDB = new HashMap<String, List<Student>>(); List<Student> lst = new ArrayList<Student>(); Student std = new Student("Sajal", "Class IV"); lst.add(std); std = new Student("Lokesh", "Class V"); lst.add(std); schooDB.put("abcschool", lst); lst = new ArrayList<Student>(); std = new Student("Kajal", "Class III"); lst.add(std); std = new Student("Sukesh", "Class VI"); lst.add(std); schooDB.put("xyzschool", lst); } @RequestMapping(value = "/getStudentDetailsForSchool/{schoolname}", method = RequestMethod.GET) public List<Student> getStudents(@PathVariable String schoolname) { System.out.println("Getting Student details for " + schoolname); List<Student> studentList = schooDB.get(schoolname); if (studentList == null) { studentList = new ArrayList<Student>(); Student std = new Student("Not Found", "N/A"); studentList.add(std); } return studentList; } } ``` `Student`類是一個簡單的 POJO。 ```java public class Student { private String name; private String className; public Student(String name, String className) { super(); this.name = name; this.className = className; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } } ``` #### 測試 Eureka 客戶端 作為 spring boot 應用程序啟動該項目。 現在,驗證此服務已在 Eureka 服務器中自動注冊。 轉到 Eureka 服務控制臺并刷新頁面。 現在,如果一切順利,我們將在 eureka 服務控制臺中看到**學生服務**的一項。 這表明 Eureka 服務器和客戶端都相互了解。 ![](https://img.kancloud.cn/49/a9/49a987d8b959d4abd263071a7a138695_1332x702.jpg) 已注冊學生服務的 Eureka 控制臺 現在,我們將驗證`/getStudentDetailsForSchool/{schoolname}`端點是否已啟動并正在運行。 轉到瀏覽器并轉到`http://localhost:8098/getStudentDetailsForSchool/abcschool`,它將為特定學校`abcschool`提供學生詳細信息。 ![](https://img.kancloud.cn/4a/fb/4afb63fca150b549eedc02f409f5f1ea_721x439.jpg) 學生服務響應 ## Eureka 客戶端 – 學校服務 現在,我們將創建學校服務,該服務將在 eureka 服務器中進行注冊-它將發現并調用沒有硬編碼 URL 路徑的學生服務。 遵循創建學生服務的確切步驟,以及創建并運行運行學校服務的 Eureka 客戶程序。 #### 創建 Eureka 客戶端項目 從具有四個依賴項的初始化器頁面創建 Spring Boot 項目,即`Actuator`,`Web`,`Rest Repositories`和`Eureka Discovery`。 給出其他 Maven GAV 坐標并下載項目。 將項目解壓縮并將其作為現有的 maven 項目導入 Eclipse。 現在,在`src`文件夾中的 Spring 運行應用程序類上添加`@EnableEurekaClient`注解。 使用此注解,此工件將充當 Spring 發現客戶端,并在連接到此服務的 eureka 服務器中進行注冊。 ```java package com.example.howtodoinjava.springeurekaclientschoolservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class SpringEurekaClientSchoolServiceApplication { public static void main(String[] args) { SpringApplication.run(SpringEurekaClientSchoolServiceApplication.class, args); } } ``` #### 客戶端配置 在`src\main\resources`目錄中創建一個名為`application.yml`的文件,然后添加以下幾行。 除了端口號和服務名稱之外,這些配置與學生服務非常相似。 ```java server: port: 9098 #port number eureka: instance: leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 2 client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/ healthcheck: enabled: true lease: duration: 5 spring: application: name: school-service #service name logging: level: com.example.howtodoinjava: DEBUG ``` #### 添加使用學生服務的 REST API 的 REST API 現在添加一個`RestController`,并公開一個 REST 點以獲取學校詳細信息。 該端點將使用帶有應用程序名稱的服務發現樣式 URL,而不是帶有`host:port`的完整 URL。 ```java package com.example.howtodoinjava.springeurekaclientschoolservice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class SchoolServiceController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/getSchoolDetails/{schoolname}", method = RequestMethod.GET) public String getStudents(@PathVariable String schoolname) { System.out.println("Getting School details for " + schoolname); String response = restTemplate.exchange("http://student-service/getStudentDetailsForSchool/{schoolname}", HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, schoolname).getBody(); System.out.println("Response Received as " + response); return "School Name - " + schoolname + " \n Student Details " + response; } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 這樣,我們可以擺脫特定的服務配置,并且可以賦予服務查找此處提供的 eureka 服務器和其余模板的責任。 如果多個實例正在為同一服務運行,我們也可以在此處應用負載平衡(請參閱`@LoadBalanced`注解)。 我們使用的 URL 是`http://student-service/getStudentDetailsForSchool/{schoolname}`。 顯然,我們只使用服務名稱`student-service`代替`host:port`。 這將由 spring 框架,eureka 服務器和 rest 模板在內部處理。 ## 服務發現和調用演示 現在也開始學校服務。 所有這三個服務都已啟動。 檢查 eureka 服務器控制臺。 學生和學校服務都必須在此注冊。 ![](https://img.kancloud.cn/6e/33/6e335796ed98947070d799364a5d9c96_1336x690.jpg) 兩種服務均已注冊的 Eureka 控制臺 轉到瀏覽器,然后轉到`http://localhost:9098/getSchoolDetails/abcschool`,它將提供特定學校`abcschool`的詳細信息。 **內部調用了學生服務**。 響應看起來像在瀏覽器中: ![](https://img.kancloud.cn/75/3f/753f3b23e3c0f72320ae6ba55ba01f9e_894x213.jpg) 學校服務響應 ## 檢查是否遇到任何錯誤的事情 1. 注解`@EnableEurekaServer`和`@EnableEurekaClient`是應用程序生態系統的核心。 沒有這兩個東西將根本無法工作。 2. 確保在啟動配置客戶端服務時,eureka 服務器服務已在運行,否則可能要花一些時間進行注冊,這可能會在測試時造成混亂。 ## 總結 我們看到了如何輕松高效地部署服務注冊表和發現服務器以及客戶端。 Spring 框架在內部維護著很多東西。 在這里,我們僅使用幾個注解和非常少的配置即可快速完成所有操作。 這就是創建 spring eureka 服務器和微服務注冊的全部內容。 如果您在執行本文時遇到任何困難,請添加評論。 我們將很樂意調查這個問題。 [下載這篇文章的源碼](https://howtodoinjava.com/wp-content/uploads/2017/07/discovery-server.zip) 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看