天天看點

2. 暢購商城之Eureka微服務搭建第2章 Eureka微服務搭建

第2章 Eureka微服務搭建

1. Eureka微服務搭建

1.1 Eureka介紹

1.1.1 Eureka是什麼?

Eureka是基于REST服務,主要以AWS雲服務為支撐,提供服務發現并實作負載均衡和故障轉移,功能類似于Dubbo的注冊中心Zookeeper,是以在 Spring Cloud微服務架構中通常用作注冊中心。我們稱這個服務為 Eureka Server還有一個與之互動的用戶端稱之為 Eureka Client

1.1.2 Eureka實作原理
2. 暢購商城之Eureka微服務搭建第2章 Eureka微服務搭建

Eureka采用C-S的設計架構,即包括了Eureka Server(服務端),Eureka client(用戶端)

  • Eureka Server提供服務注冊,在各個節點啟動後,會在Eureka Server中進行注冊
  • Eureka Client是一個Java用戶端,用于和服務端進行互動,同時用戶端也是一個内置的預設使用輪詢負載均衡算法的負載均衡器。在應用啟動後會向Eureka Server發送心跳(預設30秒)。如果Eureka Server在多個心跳周期内沒有接受到某個節點的心跳,Eureka Server将會從服務系統資料庫中将這個服務移出(預設90秒)。
  • 服務發現有兩種模式:一種是用戶端發現模式,一種是服務端發現模式。Eureka采用的是用戶端發現模式

1.2 Eureka微服務搭建

1.2.1 pom.xml配置

建立子產品

thankson-springcloud-eureka

并修改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">
    <parent>
        <artifactId>changgou</artifactId>
        <groupId>com.thankson.springcloud</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>thankson-springcloud-eureka</artifactId>
    <packaging>jar</packaging>
    <description>注冊中心</description>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>
           

1.2.2 appliation.yml配置

在resources目錄下建立配置檔案application.yml

server:
  port: 8761

spring:
  application:
    name: eureka
eureka:
  instance:
    #設定目前執行個體的主機名稱
    hostname: localhost
    #定義服務失效的時間,機關:秒
    lease-expiration-duration-in-seconds: 20
    #定義服務續約任務(心跳)的調用間隔,機關:秒
    lease-renewal-interval-in-seconds: 10
    #不使用主機名來定義注冊中心的位址,而使用IP位址的形式,如果設定了屬性,則使用該屬性配置的IP,否則自動擷取除環路IP外的第一個IP位址
    prefer-ip-address: false
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    #IP位址
    ip-address: localhost
    #狀态頁面的URL,相對路徑,預設使用 HTTP 通路,如果需要使用 HTTPS則需要使用絕對路徑配置
    status-page-url-path: /info
    #健康檢查頁面的URL,相對路徑,預設使用 HTTP 通路,如果需要使用 HTTPS則需要使用絕對路徑配置
    health-check-url-path: /health
    #健康檢查頁面的URL,絕對路徑
    health-check-url: /
  client:
    #是否注冊為服務 服務端必配,禁止自生注冊
    register-with-eureka: false
    #是否檢索服務 服務端必配,表示自己就是注冊中心,不需要去檢索服務
    fetch-registry: false
    #eureka預設空間的位址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    healthcheck:
      enabled: true
    registry-fetch-interval-seconds: 5
  server:
    #關閉自我保護(生産時打開該選項) 關閉注冊中心的保護機制,Eureka 會統計15分鐘之内心跳失敗的比例低于85%将會觸發保護機制,不剔除服務提供者,如果關閉服務注冊中心将不可用的執行個體正确剔除
    enable-self-preservation: false
    #掃描失效服務的間隔時間(預設為60*1000ms)
    eviction-interval-timer-in-ms: 10000
           

1.2.3 添加啟動類

com.thankson.springcloud.eureka

檔案夾下建立EurekaApplication.java

public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}
           

1.2.4 測試

啟動EurekaApplication.java,通路連結http://localhost:8761

2. 暢購商城之Eureka微服務搭建第2章 Eureka微服務搭建

2. common子產品搭建

thankson-springcloud-common

為父工程建立出

thankson-springcloud-common-util

thankson-springcloud-common-component

兩個子產品。

2.1 功能介紹

common子產品結構如下:

2. 暢購商城之Eureka微服務搭建第2章 Eureka微服務搭建

thankson-springcloud-common-util

工具類公共子產品  包含DateUtils、RandomUtils、ReflectionUtil、DateUtils等
           

thankson-springcloud-common-component

元件公共子產品:主要包括canal、redis、rabbitMQ等
           

2.2 util子產品

在src/main/java目錄下建立檔案夾

com.thankson.common.util

,并添加需要用到的工具類

2. 暢購商城之Eureka微服務搭建第2章 Eureka微服務搭建

2.3 component子產品

在src/main/java目錄下建立檔案夾

com.thankson.common.component

,并添加需要用到的工具類

2. 暢購商城之Eureka微服務搭建第2章 Eureka微服務搭建

3. 結束語

至此,eureka與common子產品已開發完畢。

  • Github位址:https://github.com/Thankson2020/SpringCloud-ChangGou
  • 碼雲位址:https://gitee.com/thankson2020/SpringCloud-ChangGou