Spring Cloud 學(xué)習(xí)筆記02----服務(wù)治理
簡介
上一篇《Spring Cloud 學(xué)習(xí)筆記01----微服務(wù)簡介》我們簡單的對Spring Cloud做了個(gè)介紹。接下來我們接著學(xué)習(xí)Spring Cloud 的一個(gè)核心組件 Spring Cloud Eureka。Spring Cloud Eureka主要負(fù)責(zé)完成微服務(wù)架構(gòu)中的服務(wù)治理功能,其包括 服務(wù)注冊中心,服務(wù)注冊與發(fā)現(xiàn)機(jī)制。
我們將從如下幾個(gè)方面學(xué)習(xí)Eureka
- 構(gòu)建服務(wù)注冊中心
- 服務(wù)注冊與服務(wù)發(fā)現(xiàn)
- Eureka的基礎(chǔ)架構(gòu)
- Eureka的服務(wù)治理機(jī)制
- Eureka的配置
環(huán)境
基于Spring Cloud Finchley.RELEASE 版本
Spring Boot 2.1.2.RELEASE
構(gòu)建服務(wù)注冊中心
- 創(chuàng)建一個(gè)基礎(chǔ)的SpringBoot 項(xiàng)目,命名為eureka-server,引入必要的依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
- 通過
@EnableEurekaServer
注解啟動一個(gè)服務(wù)注冊中心提供給其他應(yīng)用進(jìn)行對話
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 默認(rèn)情況下該注冊中心也會將自己作為客戶端來嘗試注冊它自己,所以我們需要禁用它的客戶端注冊行為,只需要在application.yml中增加如下配置:
server:
port: 1111
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
eureka.client.registerWithEureka: 由于該應(yīng)用為注冊中心,所以設(shè)置為false,代表不向注冊中心注冊自己
eureka.client.fetchRegistry :由于注冊中心的職責(zé)就是維護(hù)服務(wù)實(shí)例,它并不需要去檢索服務(wù),所以也設(shè)置為false。
eureka.client.serviceUrl.defaultZone : 定義注冊中心地址
5. 啟動服務(wù)訪問http://localhost:1111/ 得到結(jié)果如下:
注冊服務(wù)提供者
- 新建一個(gè)普通的Spring Boot 項(xiàng)目,增加Spring Cloud Eureka模塊的依賴。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<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>
<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>
- 接著改造OrderController類,通過注入接口
DiscoveryClient
對象,在控制臺打印調(diào)用服務(wù)的服務(wù)名。
@RestController
public class OrderController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/dc")
public String dc() {
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}
}
- 然后在主類中通過加上
@EnableDiscoveryClient
注解,激活Eureka中的DiscoveryClient
實(shí)現(xiàn)(自動化配置,創(chuàng)建DiscoveryClient接口針對Eureka客戶端的EurekaDiscoveryClient實(shí)例),才能實(shí)現(xiàn)上述Controller中對服務(wù)信息的輸出
@SpringBootApplication
@EnableDiscoveryClient
public class OrderProviderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderProviderApplication.class, args);
}
}
- 然后在application.yml 中加入如下配置
spring:
application:
# 給服務(wù)命名
name: order-service
eureka:
client:
service-url:
# 用于指定注冊中心的地址
defaultZone: http://localhost:1111/eureka/
server:
port: 2001
啟動服務(wù)
- 啟動order-provider 服務(wù),在order-provider服務(wù)控制臺中,Tomcat啟動之后,
com.netflix.discovery.DiscoveryClient
對象打印了該服務(wù)的注冊信息。表示服務(wù)注冊成功
- 此時(shí)在注冊中心的控制臺中,可以看到類似下面的輸出,名為order-service 的服務(wù)被注冊成功了。
- 通過訪問Eureka的信息面板,可以看到服務(wù)被注冊成功。
- 通過訪問http://localhost:2001/dc 直接向給服務(wù)發(fā)起請求,在控制臺中可以看到如下輸出:
Eureka的基礎(chǔ)架構(gòu)
整個(gè)Eureka服務(wù)治理基礎(chǔ)架構(gòu)有三個(gè)核心要素。
- 服務(wù)注冊中心
Eureka 提供的服務(wù)端,提供服務(wù)注冊與發(fā)現(xiàn)的功能,也就是我們示例中的eurka-server。
- 服務(wù)提供者
提供服務(wù)的應(yīng)用,可以使Spring Boot應(yīng)用,也可以是其他技術(shù)平臺且遵循Eureka通信機(jī)制的應(yīng)用。它將自己提供的服務(wù)注冊到Eureka,以供其他應(yīng)用發(fā)現(xiàn),也就是我們示例中的order-service。
- 服務(wù)消費(fèi)者
消費(fèi)者應(yīng)用從服務(wù)注冊中心獲取服務(wù)列表,從而是消費(fèi)者可以知道去何處調(diào)用其所需要的服務(wù)。我們可以使用Ribbon 來實(shí)現(xiàn)服務(wù)消費(fèi),或者使用Feign 的消費(fèi)方式。
源碼下載
https://github.com/XWxiaowei/SpringCloud-Learning/tree/master/2-Finchley版教程示例/Chapter3-1
參考引用
《Spring Cloud 微服務(wù)實(shí)戰(zhàn)》第3章
https://springcloud.cc/spring-cloud-dalston.html
作者:碼農(nóng)飛哥
微信公眾號:碼農(nóng)飛哥