SpringCloud實戰之路 | 應用篇(一)服務註冊中心Eureka
註冊中心介紹
在分佈式環境的場景下,通常每一個服務都是會部署多個實例,服務的提供者數量往往是會動態變化的,爲了保證能夠彈性的動態擴容縮容,靜態的LB是不適用的,這時候就需要引入註冊中心進行對服務的提供者和消費者進行註冊發現。
作用: 主要就是用於對服務提供者與服務消費者的解耦。註冊中心用於存儲服務提供者的地址等相關信息,服務消費者通過主動查詢或者被動通知從註冊中心中獲取到服務提供者的信息,而不再需要通過硬編碼的方式獲取提供者的地址信息
基本原理
消費者獲取提供者的註冊信息有兩種模式
poll模式: 服務消費者主動從註冊中心中拉取服務提供者的信息
push模式: 服務消費者以訂閱的方式獲取提供者的信息,當服務提供者有變化時,註冊中心會制動推送給服務消費者
註冊中心也需要完成服務提供者的健康監控,當發現服務提供者失效時需要及時剔除;
常見註冊中心對比
註冊中心 | 語言 | CAP | 對外暴露接口 |
---|---|---|---|
Eureka | java | AP | HTTP |
Consul | go | CP | HTTP/DNS |
Zookeeper | java | CP | 客戶端 |
Nacos | java | 支持AP/CP切換 | HTTP |
Eureka實現原理
Eureka中包含兩個組件:
- Eureka Server: 負責服務發現,服務啓動時會通過Eureka Client向Eureka Server中註冊自己的信息。(需要自己創建工程引入jar包)
- Eureka Client: 用於與Eureka Server的交互。(在已有的微服務中引入client端相關jar包,進行配置與Eureka Server產生聯繫)
1.服務提供者向Eureka Server中註冊服務,Eureka Server接受到註冊事件會在集羣和分區中進行同步,消費端可以從Eureka Server中獲取服務註冊信息,進行服務調用。
2.服務啓動後,會週期性向Eureka Server發送心跳(默認30s)完成續約
3.Eureka Server在一定時間內沒有接收到某個微服務節點的心跳會註銷該服務節點(默認90s)
4.Eureka Client會緩存Eureka Server中的信息,即使Eureka Server的節點都宕掉,服務消費者也可以從緩存中找到服務提供者
Eureka搭建
parent父pom中引入spring cloud依賴
<dependencyManagement>
<dependencies>
<!--spring cloud依賴管理,引入了Spring Cloud的版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
(一)構建Eureka Server服務
引入maven依賴
<?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">
<parent>
<artifactId>cloud-parent</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server-8761</artifactId>
<dependencies>
<!--Eureka server依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
創建入口啓動類
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8761 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer8761.class,args);
}
}
創建配置文件yml
#eureka server服務端口
server:
port: 8761
spring:
application:
name: cloud-eureka-server # 應用名稱,應用名稱會在Eureka中作爲服務名稱
訪問 http://localhost:8761/
(二)服務提供者引入Eureka Client
每個微服務引入maven依賴
<?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">
<parent>
<artifactId>cloud-parent</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-service-user</artifactId>
<dependencies>
<!--eureka client 客戶端依賴引入-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
修改微服務application.yml配置⽂件,
server:
port: 8080
#註冊到Eureka服務中心
eureka:
client:
service-url:
# 註冊到集羣,就把多個Eurekaserver地址使用逗號連接起來即可;註冊到單實例(非集羣模式),那就寫一個就ok
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-service-user
datasource:
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免將駝峯命名轉換爲下劃線命名
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細節
endpoint:
health:
show-details: always
刷新Eureka地址:http://localhost:8761/
至此完成服務提供者註冊到Eureka
Eureka實現高可用
Eureka創建集羣只需要將eureka也作爲服務註冊到自身即可
1.修改本機host
由於是在個人計算機測試,很難模擬多主機情況,Eureka配置server集羣時候需要執行host地址,所以需要修改個人電腦的host地址
127.0.0.1 EurekaServerA
127.0.0.1 EurekaServerB
2.修改eureka server配置
#eureka server服務端口
server:
port: 8761
spring:
application:
name: cloud-eureka-server # 應用名稱,應用名稱會在Eureka中作爲服務名稱
# eureka 客戶端配置(和Server交互),Eureka Server 其實也是一個Client
eureka:
instance:
hostname: EurekaServerA # 當前eureka實例的主機名
client:
service-url:
# 配置客戶端所交互的Eureka Server的地址(Eureka Server集羣中每一個Server其實相對於其它Server來說都是Client)
# 集羣模式下,defaultZone應該指向其它Eureka Server,如果有更多其它Server實例,逗號拼接即可
defaultZone: http://EurekaServerB:8762/eureka
register-with-eureka: true # 集羣模式下可以改成true
fetch-registry: true # 集羣模式下可以改成true
dashboard:
enabled: true
修改client端配置
server:
port: 8080
#註冊到Eureka服務中心
eureka:
client:
service-url:
# 註冊到集羣,就把多個Eurekaserver地址使用逗號連接起來即可;註冊到單實例(非集羣模式),那就寫一個就ok
defaultZone: http://EurekaServerA:8761/eureka,http://EurekaServerB:8762/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-service-user
datasource:
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免將駝峯命名轉換爲下劃線命名
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細節
endpoint:
health:
show-details: always