2019獨角獸企業重金招聘Python工程師標準>>>
關于springboot的學習請參考前面的文章
接下來我們會開啟一系列關于springcloud的學習文章。
一、概念
? ? 首先我們看下官方的解釋
Service Discovery is one of the key tenets of a microservice-based architecture. Trying to hand-configure each client or some form of convention can be difficult to do and can be brittle. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.服務發現是基于微服務架構的關鍵原則之一。 嘗試手動配置每個客戶端或某種形式的約定可能很難做到,并且可能很脆弱。 Eureka是Netflix服務發現服務器和客戶端。 服務器可以配置和部署為高可用性,每臺服務器將注冊服務的狀態復制到其他服務器。
????Spring Cloud Eureka,Eureka是個什么呢,他主要是用來做服務治理的,我們知道,如果系統的服務少的情況下,通過靜態配置就行了。如果服務數量特別多,靜態配置如果修改維護起來就相當麻煩,Eureka就是解決這個事兒的。
? ? 進一步解釋
The Eureka server does not have a backend store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory). Clients also have an in-memory cache of Eureka registrations (so they do not have to go to the registry for every request to a service).
Eureka服務器沒有后端存儲,但注冊表中的服務實例必須發送心跳信號以保持其注冊是最新的(所以這可以在內存中完成)。 客戶端還有一個Eureka注冊的內存緩存(因此他們不必每次請求注冊服務都去注冊中心)。
二.首先從spring官網,下載一個關于springboot的項目
? ? 1.訪問 https://start.spring.io/
? ? 2.按照截圖中的內容進行操作
????????
????? ?3.將工程下載下來后,導入到idea,idea會根據pom中的配置自動構建項目的依賴
二、在src/main/resources目錄中的application.properties文件中填寫一些配置
????? ?
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
????? ? 1.配置解釋
????????? ? server.port :?服務器的ip
? ? ? ? ? ? eureka.instance.hostname : eureka的主機名
????????? ? eureka.client.register-with-eureka :?這個項目是注冊中心,這個配置代表不向注冊中心注冊自己
????????? ? eureka.client.fetch-registry :?注冊中心的職務就是維護服務示例,他不需要去檢索服務。所以設置成false
三、主類配置啟用EurekaServer
????????在 ?com/myspringboot/eurekaserver?包下的?EurekaServerApplication?上面加入
????????@EnableEurekaServer ,代表啟用eureka服務
? ? ? ?
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
?四、啟動項目
????訪問? http://localhost:1111/? ? ? ? ?,我們看到這個界面,就等于配置成功了
????
五、總結
? ? 本章介紹了注冊中心的概念以及如何搭建單節點注冊中心。
? ? 如果大家對此章有什么疑問歡迎留言提問。
????