二、Spring Cloud之註冊中心 Eureka
前言
算是正式開始學習 spring cloud 的專案知識了,大概的知道Springcloud 是由眾多的微服務組成的,所以我們現在一個一個的來學習吧。
註冊中心,在微服務中算是核心了。所有的服務都會註冊到註冊中心,請求服務的時候,並不會直接去請求服務地址,而是先通過註冊中心再轉到目的地址。雖然Eureka 已經停止維護了,但是我們暫時使用起來還是沒有問題的。
Eureka 主要有服務註冊中心、服務提供者和服務消費。很多時候服務消費者也是服務提供者。所以就 Eureka 而言,分為 Eureka 服務端和Eureka 客戶端,服務端就是註冊中心,客戶端就是服務提供者和消費者。
單機模式
好了,我們動手搭建一個Eureka 的服務端吧先,服務端有單機模式和叢集模式,我們先來單機模式。
更具上篇文章講的,我們使用maven 模組化開發,我們建立一個父級maven專案,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 http://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>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.quellanan</groupId> <artifactId>SpringCloud</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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> <modules> <module>eureka-server-8000</module> <module>eureka-server-8001</module> <module>eureka-server-8002</module> <module>zlflovemm</module> </modules> </project>
可以看到檔案中指定了spring boot 和Spring cloud 等基礎依賴的版本,這樣保證各個模組版本的一致性。
子模組
接下來我們建立一個eureka-server-8000 的子模組。
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>cn.quellanan</groupId> <artifactId>SpringCloud</artifactId> <version>1.0.0</version> </parent> <groupId>com.quellanan.springcloud</groupId> <artifactId>eureka-server-8000</artifactId> <version>1.0.0</version> <name>eureka-server-8000</name> <description>eureka project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
可以看到繼承了父級pom,額外的增加了 spring-cloud-starter-netflix-eureka-server 的依賴。
@EnableEurekaServer
在啟動類中增加@EnableEurekaServer 註解,表示啟用Eureka 服務端。
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8000Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer8000Application.class, args);
}
}
application.properties
配置檔案中增加如下配置
spring.application.name=spring-cloud-eureka-server-8000
server.port=8000
#表示是否將自己註冊到Eureka Server,預設為true。
eureka.client.register-with-eureka=true
# 表示是否從Eureka Server獲取註冊資訊,預設為true。
eureka.client.fetch-registry=true
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。多個地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
現在我們就可以啟動專案看看
可以看到我們將自身註冊到了服務中。
Eureka 客戶端
前面說了,服務提供者和服務消費者都是客戶端,其實就是我們具體的某一業務的專案。所以我們再建立一個子模組。我這裡分開吧,我們分別建立服務提供者和服務消費者。
服務提供者
我們建立一個eureka-client-provider的子模組,pom 檔案中引入spring-cloud-starter-netflix-eureka-client。
<parent>
<groupId>cn.quellanan</groupId>
<artifactId>SpringCloud</artifactId>
<version>1.0.0</version>
</parent>
groupId>com.quellanan.springcloud</groupId>
<artifactId>eureka-client-provider</artifactId>
<version>1.0.0</version>
<name>eureka-client-provider</name>
<description>eureka-client-provider 服務提供者</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
啟動類中加入@EnableEurekaClient註解或者@EnableDiscoveryClient註解都可以。
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientProviderApplication.class, args);
}
}
application.properties 中增加如下配置
server.port=9000
#服務名,在註冊時所用
spring.application.name=eureka-client-provider
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
這裡指定的eureka的服務中心的地址為8000。如上配置就可以將服務註冊到註冊中心啦。
我們在寫一個測試介面。
建立一個IndexController 類,內容如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world ";
}
}
服務消費者
我們一樣的建立一個 eureka-client-consumer的模組。pom檔案如下:
<parent>
<groupId>cn.quellanan</groupId>
<artifactId>SpringCloud</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.quellanan.springcloud</groupId>
<artifactId>eureka-client-consumer</artifactId>
<version>1.0.0</version>
<name>eureka-client-consumer</name>
<description>eureka-client-consumer 服務消費者</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
相對於服務提供者,我們增加了Feign 依賴,主要用來發現服務和實現客戶端負載均衡,我們這裡用來發現服務就可以了。
在啟動類中@EnableDiscoveryClient 用來發現服務,並注入RestTemplate 的例項bean 用來對服務提供的介面進行呼叫。@LoadBalanced 是開啟客戶端負載均衡的,最開始我沒有加這個註解,但是發現不加的話,服務消費者就不能通過服務名來獲取可用的服務提供者的例項。所以這裡大家可以試驗一下。
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerApplication.class, args);
}
}
我們接下寫一個介面,呼叫服務消費者,我們建立一個IndexController,內容如下:
@RestController
public class IndexController {
private static final String applicationName = "eureka-client-provider";
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/index")
public String getHello(){
String url = "http://"+ applicationName +"/hello";
return restTemplate.getForObject(url,String.class);
}
}
這裡我們可以看到applicationName 就是服務提供者的服務名。實際中,一種型別的服務可能有好幾臺伺服器,可能實體地址和ip不一樣,但是保證他們的服務名一樣就可以了,這樣服務消費者就可以通過服務名獲取可用的列表,再通過複雜均衡策略選擇其中一個例項訪問。
最後我們在application中加上如下配置:
server.port=9001
#服務名,在註冊時所用
spring.application.name=eureka-client-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
測試
現在我們啟動註冊中心和客戶端這兩個專案來看下。啟動後,我們輸入
http://localhost:8000
可以發現我們的客戶端已經註冊到註冊中心啦。服務提供者和服務消費者都已經註冊到了註冊中心啦。我們再來調介面試試。我們輸入如下:
http://localhost:9001/index
可以看到其實獲取了9000服務提供者的介面。這樣就實現了服務的註冊和發現啦,並實現遠端呼叫。
叢集模式(高可用)
上面我們我們搭建的註冊中心只是單機模式,只有一個Eureka 服務端,單實際應用中註冊中心其實是尤為重要的,所以就需要搭建叢集環境,其實Eureka 對叢集模式是天然的支援的,我們搭建也非常簡單。
為什麼這麼說呢,我們前面可以看到只要配置了eureka.client.serviceUrl.defaultZone 就就會被對應的註冊中線檢測到,所以我們程式碼完全一樣,只需要將eureka.client.serviceUrl.defaultZone相互指引就可以了,就就可以簡單的搭建一個高可用的環境。
下面我們來搭建一個,因為我們就一臺伺服器,所以就用不同的埠,其實程式碼完全一樣的,只是配置檔案中配置不一樣,我們分別把三個分配置檔案貼出來。
8000埠的
spring.application.name=spring-cloud-eureka-server-8000
server.port=8000
#表示是否將自己註冊到Eureka Server,預設為true。
eureka.client.register-with-eureka=true
# 表示是否從Eureka Server獲取註冊資訊,預設為true。
eureka.client.fetch-registry=true
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。多個地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/,http://localhost:8002/eureka/
8001埠:
spring.application.name=spring-cloud-eureka-server-8001
server.port=8001
#表示是否將自己註冊到Eureka Server,預設為true。
eureka.client.register-with-eureka=true
# 表示是否從Eureka Server獲取註冊資訊,預設為true。
eureka.client.fetch-registry=true
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。多個地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8002/eureka/
8002 埠
spring.application.name=spring-cloud-eureka-server-8002
server.port=8002
#表示是否將自己註冊到Eureka Server,預設為true。
eureka.client.register-with-eureka=true
# 表示是否從Eureka Server獲取註冊資訊,預設為true。
eureka.client.fetch-registry=true
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。多個地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/
我們現在分別啟動這個這三個配置檔案,你們可以用profile 來指向,我這為了分明,直接建立了三個模組。啟動後,我們分別訪問
http://localhost:8000/
http://localhost:8001/
http://localhost:8002/
這裡可以看到其實已經相互監控了。我們瞭解一下這兩個配置引數。
#定義服務續約任務的呼叫時間間隔,預設30s
eureka.instance.lease-renewal-interval-in-seconds=30
#定義服務失效的時間預設90s
eureka.instance.lease-expiration-duration-in-seconds=90
我們現在再將我們的服務提供者和服務消費者註冊進來,但是這裡,需要修改的地方也是eureka.client.serviceUrl.defaultZone。將服務註冊到叢集中。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/,http://localhost:8002/eureka/
然後啟動專案可,可以看到註冊到了註冊中心,並且可以呼叫服務提供者提供的介面。
總結
最後畫了一張圖來說明整個註冊中心的架構圖。
可以看到註冊服務端可以是一個叢集。相互註冊監控。服務消費者和服務提供者都是服務客戶端,都會將服務註冊到服務中心,同時這些服務也都可以使是叢集或者分散式的。服務提供者會從服務端獲取服務提供者可用的服務例項列表,通過負載均衡策略選擇其中某一例項進行呼叫。這個算是Eureka 的總結吧哈哈
番外
好啦,總是是寫完了,這篇文章真是是卡了我好幾天,有的地方寫的不是很好,歡迎大家指點。
程式碼上傳到github:
https://github.com/QuellanAn/SpringCloud
後續加油♡
歡迎大家關注個人公眾號 "程式設計師愛酸奶"
分享各種學習資料,包含java,linux,大資料等。資料包含視訊文件以及原始碼,同時分享本人及投遞的優質技術博文。
如果大家喜歡記得關注和分享喲❤
相關推薦
二、Spring Cloud之註冊中心 Eureka
前言 算是正式開始學習 spring cloud 的專案知識了,大概的知道Springcloud 是由眾多的微服務組成的,所以我們現在一個一個的來學習吧。 註冊中心,在微服務中算是核心了。所有的服務都會註冊到註冊中心,請求服務的時候,並不會直接去請求服務地址,而是先通過註冊中心再轉到目的地址。雖然Eureka
第二章、spring cloud服務註冊中心eureka---服務提供與呼叫
服務提供與呼叫 案例中有三個角色:服務註冊中心、服務提供者、服務消費者,其中服務註冊中心就是我們上一篇的eureka單機版啟動既可,流程是首先啟動註冊中心,服務提供者生產服務並註冊到服務中心中,消費者從服務中心中獲取服務並執行。 02eureka-pro
第一章、spring cloud服務註冊中心eureka---概念
註冊中心Eureka 背景介紹 服務中心 服務中心又稱註冊中心,管理各種服務功能包括服務的註冊、發現、熔斷、負載、降級等,比如dubbo admin後臺的各種功能。 有了服務中心呼叫關係會有什麼變化,畫幾個簡圖來幫忙理解 專案A呼叫專案B 正常呼叫專案A請求專案B
3、spring cloud服務註冊中心eureka---基於feign的負載均衡(第二章)
基於feign的負載均衡 spring-cloud-producer-one修改,將其中的controller改動如下: @RestController public class HelloController { @RequestMapping("/hello")
2、spring cloud服務註冊中心eureka---服務消費者(第二章)
服務呼叫 1、pom包配置 和服務提供者一致 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww
1、spring cloud服務註冊中心eureka---服務提供者(第二章)
服務提供 我們假設服務提供者有一個hello方法,可以根據傳入的引數,提供輸出“hello xxx,this is first messge”的服務 1、pom包配置 建立一個springboot專案,pom.xml中新增如下配置: <?xml version="1.0"
3、spring cloud服務註冊中心eureka---叢集配置(第一章)
eureka叢集配置 在生產中我們可能需要三臺或者大於三臺的註冊中心來保證服務的穩定性,配置的原理其實都一樣,將註冊中心分別指向其它的註冊中心。這裡只介紹三臺叢集的配置情況,其實和雙節點的註冊中心類似,每臺註冊中心分別又指向其它兩個節點即可,使用application.yml來配置。
2、spring cloud服務註冊中心eureka—雙節點配置(第一章)
叢集 註冊中心這麼關鍵的服務,如果是單點話,遇到故障就是毀滅性的。在一個分散式系統中,服務註冊中心是最重要的基礎部分,理應隨時處於可以提供服務的狀態。為了維持其可用性,使用叢集是很好的解決方案。Eureka通過互相註冊的方式來實現高可用的部署,所以我們只需要將Eureke Server配
1、spring cloud服務註冊中心eureka---單節點配置(第一章)
Eureka Server—單節點配置 spring cloud已經幫我實現了服務註冊中心,我們只需要很簡單的幾個步驟就可以完成。 1、pom中新增依賴 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=
Spring Cloud的註冊中心Eureka
大家如果覺得我寫的好,可以關注這個專案,之後我的學習過程中都會把學習筆記放入這個專案中。 https://github.com/IndustriousSnail/learning-notes Spring Cloud的註冊中心Eureka 目錄 一、Eureka基本概念
2.spring cloud服務註冊中心eureka server---新增Hystrix Dashboard(第四章)
Hystrix Dashboard 我們在熔斷示例專案spring-cloud-consumer-hystrix的基礎上更改,重新命名為:spring-cloud-consumer-hystrix-dashboard。 1、新增依賴 org.springframework.boot 版本 :
1.spring cloud服務註冊中心eureka server---新增Security使用者認證(第四章)
為服務註冊中心eureka server—新增Security使用者認證 在spring-cloud-eureka服務註冊中心專案的基礎上增加使用者認證。 1、新增依賴 <dependency> <groupId>org.springframew
Spring Cloud:註冊中心Eureka(02)
Eureka是Netflix開源的一款提供服務註冊和發現的產品,它提供了完整的Service Registry和Service Discovery實現。也是springcloud體系中最重要最核心的元件之一。 背景介紹 服務中心 服務中心又稱註冊中心,管理各種服務功能包
孰能巧用 Spring Cloud 服務註冊中心Eureka
Eureka介紹 在Spring Cloud Netflix 整合技術棧中,Eureka既可以作為服務註冊中心也可以用於服務發現對整個微服務架構起著最核心的整合作用。 Eureka是基於REST(Representational State Transfer)服務,主要以AWS雲服務為支撐,提供服務發現並實現
Spring Cloud與微服務之註冊中心Eureka
文章目錄 Eureka Eureka Service Eureka Client 微服務註冊與發現 Eureka註冊中心原始碼 springcloud-eureka註冊中心 spring
四、Spring cloud之服務發現/註冊(Eureka)
一、Spring Cloud Eureka (一)Eureka伺服器 Eureka Server 是 Eureka Client 的註冊服務中心,管理所有註冊服務、以及其例項資訊和狀態。 1、引入Maven依賴 <dependency>
菜鳥學Spring Cloud——建立註冊中心(Eureka)
文章目錄扯一扯軟體環境步驟效果下集預告 扯一扯 以下內容將引起極度舒適,請在女朋友的陪同下觀看。什麼?你沒有女朋友?哦,不好意思,我忘了,程式設計師是沒有女朋友的。那就好好學習吧,女朋友總會有的。 軟體環境 Windows 10 JDK 1.8 IDEA 20
二、Spring Cloud—Eureka(Greenwich.SR1)
注:本系列文章所用工具及版本如下:開發工具(IDEA 2018.3.5),Spring Boot(2.1.3.RELEASE),
idea搭建spring cloud服務註冊中心
1.先建立一個maven主專案,file->new->project,選擇Maven,next 2.建立Eureka服務端,在建立好的maven專案裡面右鍵專案名,new->module,選擇Spring Initialzr,next 然後next再finis
三、spring cloud config 配置中心
1、 當一個系統中的配置檔案發生改變的時候,經常的做法是重新啟動該服務,才能使得新的配置檔案生效,spring cloud config可以實現微服務中的所有系統的配置檔案的統一管理,而且還可以實現當配置檔案發生變化的時候,系統會自動更新獲取新的配置。 將配置檔案放入git或者svn等