1. 程式人生 > >SpringCloud系列使用Eureka進行服務治理

SpringCloud系列使用Eureka進行服務治理

### 1. 什麼是微服務? “微服務”一詞來自國外的一篇博文,網站:https://martinfowler.com/articles/microservices.html ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727145100303.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 如果您不能看懂英文文件,可以跳轉到搜簡體中文的文件 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727145121138.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 這是國人翻譯的文件,可以學習參考: ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727145139513.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 引用官方文件解釋: >簡單來說,微服務架構風格[1]是一種將一個單一應用程式開發為一組小型服務的方法,每個服務執行在自己的程序中,服務間通訊採用輕量級通訊機制(通常用HTTP資源API)。這些服務圍繞業務能力構建並且可通過全自動部署機制獨立部署。這些服務共用一個最小型的集中式的管理,服務可用不同的語言開發,使用不同的資料儲存技術。 ![](https://img-blog.csdnimg.cn/20200727144116198.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) ### 2. 什麼是Spring Cloud? * Spring Cloud是一個分散式的整體解決方案的框架。基於Spring Boot開發。Spring Cloud 為開發者提供了在分散式系統(配置管理,服務發現,負載,閘道器,訊息匯流排,叢集管理,安全管理,分散式鎖,分散式事務等等)中快速構建的工具,使用Spring Cloud的開發者可以快速的啟動服務或構建應用、同時能夠快速和雲平臺資源進行對接。 ### 3. 什麼是Spring Cloud Eureka? * Spring Cloud Eureka 是 Spring Cloud Netflix 微服務套件的一部分,基於 Netflix Eureka 做了二次封裝,主要負責實現微服務架構中的服務治理功能。 * Spring Cloud Eureka 是一個基於 REST 的服務,並且提供了基於 Java 的客戶端元件,能夠非常方便地將服務註冊到 Spring Cloud Eureka 中進行統一管理。 ### 4. Eureka服務註冊中心 Eureka server:建立服務註冊中心 環境準備: * JDK 1.8 * SpringBoot2.2.1 * SpringCloud(Hoxton.SR6) * Maven 3.2+ * 開發工具 * IntelliJ IDEA * smartGit 建立一個SpringBoot Initialize專案,詳情可以參考我之前部落格:[SpringBoot系列之快速建立專案教程](https://blog.csdn.net/u014427391/article/details/102870300) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200724181354348.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) pom,加上spring-cloud-starter-netflix-eureka-server ```xml org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
``` @EnableEurekaServer配置Eureka服務端: ```java package com.example.springcloud.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class SpringcloudEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudEurekaServerApplication.class, args); } } ``` eureka服務端配置: ```yaml spring: application: name: eurka-server server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka/ ``` ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727152002542.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 啟動專案,訪問:[http://localhost:8761](http://localhost:8761) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727124712964.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) ### 5. Eureka服務提供者 **在Eureka中,服務提供者和服務消費者是Eureka client提供的**,使用註解`@EnableEurekaClient`標明 新建SpringBoot Initializer專案 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727152558705.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) ```yaml server: port: 8081 spring: application: name: eureka-service-provider eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ register-with-eureka: true fetch-registry: true healthcheck: enabled: false instance: status-page-url-path: http://localhost:8761/actuator/info health-check-url-path: http://localhost:8761/actuator//health prefer-ip-address: true instance-id: eureka-service-provider8081 ``` ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727181353826.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 寫個例子,以github使用者為例: ```java package com.example.springcloud.provider.bean; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import java.io.Serializable; /** *
 *  Github User
 * 
* *
 * @author mazq
 * 修改記錄
 *    修改後版本:     修改人:  修改日期: 2020/07/27 17:38  修改內容:
 * 
*/ @JsonIgnoreProperties(ignoreUnknown = true) @Data public class User implements Serializable { private String name; private String blog; @Override public String toString() { return "User{" + "name='" + name + '\'' + ", blog='" + blog + '\'' + '}'; } } ``` 讀取github使用者資訊: ```java package com.example.springcloud.provider.service; import com.example.springcloud.provider.bean.User; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** *
 *  UserService
 * 
* *
 * @author mazq
 * 修改記錄
 *    修改後版本:     修改人:  修改日期: 2020/07/27 17:42  修改內容:
 * 
*/ @Service @Slf4j public class UserService { private final RestTemplate restTemplate; public UserService(RestTemplateBuilder restTemplateBuilder) { this.restTemplate = restTemplateBuilder.build(); } public User findUser(String user) throws InterruptedException { log.info("username[{}]" , user); String url = String.format("https://api.github.com/users/%s", user); User results = restTemplate.getForObject(url, User.class); return results; } } ``` `@EnableEurekaClient`指定eureka client: ```java package com.example.springcloud.provider; import com.example.springcloud.provider.bean.User; import com.example.springcloud.provider.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @RestController public class SpringcloudServiceProviderApplication { @Autowired UserService userService; public static void main(String[] args) { SpringApplication.run(SpringcloudServiceProviderApplication.class, args); } @GetMapping({"/api/users/{username}"}) @ResponseBody public User findUser(@PathVariable("username")String username) throws InterruptedException{ User user= userService.findUser(username); return user; } } ``` 部署後,註冊資訊釋出到eureka server: ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020072713300076.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 服務註冊資訊列印到控制檯 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727154823314.png) 訪問介面:http://localhost:8081/api/users/mojombo,能訪問就是註冊成功 提示:EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE. ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727153222735.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) >SpringCloud警告(Eureka):EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE. 警告!Eureka可能存在維護了錯誤的例項列表(當它們沒有啟動的時候,Eureka卻把它當成啟動的了);Renews值小於Threshold值,因此剩下未過期的都是安全的。 方法:參考部落格[https://www.cnblogs.com/gudi/p/8645370.html](https://www.cnblogs.com/gudi/p/8645370.html),可以關了eureka的自我保護模式`eureka.server.enableSelfPreservation=false` eureka也引用SpringBoot actuator監控管理,SpringBoot actuator可以參考我之前部落格: [SpringBoot系列之actuator監控管理極速入門與實踐](https://smilenicky.blog.csdn.net/article/details/107535188) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727153014252.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 具體可以進行配置,配置成eureka server的ip,加上actuator ```yaml eureka: instance: status-page-url-path: http://localhost:8761/actuator/info health-check-url-path: http://localhost:8761/actuator/health ``` 顯示ip和例項id配置: ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727181653458.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727181721863.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) ### 6. Eureka服務消費者 ```yaml server: port: 8082 spring: application: name: eureka-service-consumer eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ fetch-registry: true register-with-eureka: false healthcheck: enabled: false instance: status-page-url-path: http://localhost:8761/actuator/info health-check-url-path: http://localhost:8761/actuator//health prefer-ip-address: true instance-id: eureka-service-consumer8082 ``` ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200727181930244.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70) 關鍵點,使用SpringCloud的`@LoadBalanced`,才能調http://EUREKA-SERVICE-PROVIDER/api/users/? 介面的資料,瀏覽器是不能直接調的 ```java package com.example.springcloud.consumer; import com.example.springcloud.consumer.bean.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @RestController public class SpringcloudServiceConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpringcloudServiceConsumerApplication.class, args); } @GetMapping("/findUser/{username}") public User index(@PathVariable("username")String username){ return restTemplate().getForObject("http://EUREKA-SERVICE-PROVIDER/api/users/"+username,User.class); } } ``` 附錄: ok,本部落格參考官方教程進行實踐,僅僅作為入門的學習參考資料,詳情可以參考Spring Cloud官方文件[https://cloud.spring.io/spring-cloud-netflix/reference/html/#registering-with-eureka](https://cloud.spring.io/spring-cloud-netflix/reference/html/#registering-with-eureka) 程式碼例子下載:[code download](https://github.com/u014427391/springCloudExamples/tree/master/springcloud-eureka) 優質學習資料參考: * 方誌鵬大佬系列Spring Cloud部落格:[https://www.fangzhipeng.com/spring-cloud.html](https://www.fangzhipeng.com/spring-cloud.html) * 使用Spring Cloud與Docker實戰微服務:[https://eacdy.gitbooks.io/spring-cloud-book/content/](https://eacdy.gitbooks.io/spring-cloud-book/content/) * 程式設計師DD大佬系列Spring Cloud部落格:[http://blog.didispace.com/spring-cloud-learning/](http://blog.didispace.com/spring-cloud-lea