天天看點

Spring Cloud實戰系列(一) - 服務注冊與發現Eureka前言正文參考

前言

Spring Cloud

封裝了

Netflix

公司開發的

Eureka

子產品來實作 服務注冊和發現。

Eureka

采用了

C-S

的 設計架構。

Eureka Server

作為 服務注冊中心,系統中的 其他微服務,使用

Eureka

的 用戶端 連接配接到

Eureka Server

,并通過 心跳連接配接 檢測服務的 存活狀态。

正文

  • Eureka Server: 作為 服務注冊中心,提供 服務注冊和發現。
  • Eureka Client: 所有注冊到 服務中心 的服務。
    • Service Provider: 把 自身的服務 注冊到

      Eureka Server

      ,進而使 服務消費方 能夠找到。
    • Service Consumer: 從

      Eureka Server

      擷取 服務注冊清單,進而能夠 消費服務。

1. 建立服務注冊中心

建立

2

個項目

Module

,一個

Module

(即

Spring Boot

)工程作為 服務注冊中心,即

Eureka Server

,另一個作為

Eureka Client

Eureka Server

建立完後的工程

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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.ostenant.github.springcloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
</project>
           

2. 啟動服務注冊中心

Eureka

是一個 高可用 的元件,它沒有 後端緩存。每一個 執行個體 注冊之後,需要 定時 向 注冊中心 發送 心跳(是以可以在記憶體中完成)。在預設情況下

Eureka Server

也是一個

Eureka Client

,必須要指定一個

Server

。在啟動之前,首先對

Eureka Server

配置

application.yml

檔案。

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
           
  • eureka.client.register-with-eureka:

設定是否将自己作為

Eureka Client

注冊到

Eureka Server

,預設為

true

  • eureka.client.fetch-registry

設定是否從

Eureka Server

擷取 注冊資訊,預設為

true

。因為本例是一個 單點 的

Eureka Server

,不需要 同步 其他

Eureka Server

節點的資料,是以設定為

false

  • eureka.client.service-url.defaultZone

設定的是與

Eureka Server

的 互動位址,查詢 和 注冊服務 都依賴這個位址,如果有多個可以使用 英文逗号分隔。

然後再把注解

@EnableEurekaServer

加在

Spring Boot

工程的啟動類

Application

上面:

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}
           

Eureka Server

是有界面的,啟動項目後,打開浏覽器通路

http://localhost:8761

即可檢視。

3. 建立服務提供者

當一個

Eureka Client

Eureka Server

發起 注冊 時,它會提供一些 中繼資料,例如 主機 和 端口 等等。

Eureka Server

從每個

Eureka Client

執行個體接收 心跳消息。 如果 心跳逾時,則通常将該執行個體從

Eureka Server

中删除。

建立一個

service-hi

Module

,建立完成後的

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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.ostenant.github.springcloud</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-cloud-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
</project>
           

通過 注解

@EnableEurekaClient

表明自己是一個

Eureka Client

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
    @Value("${server.port}")
    private String port;

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

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "Hi " + name + ", I am from port: "  + port;
    }
}
           

僅僅

@EnableEurekaClient

是不夠的,還需要在 配置檔案 中注明的 服務注冊中心 的位址,

application.yml

配置檔案如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8763
spring:
  application:
    name: service-hi
           

Eureka

用戶端 需要指明

spring.application.name

,用于服務的 唯一辨別,服務之間 互相調用會基于這個

name

啟動并通路

Eureka Server

的位址

http://localhost:8761

,會發現服務名稱為

SERVICE-HI

,端口為

7862

的服務,已注冊到

Eureka Server

的清單上。

3. 高可用Eureka Server

在一個 分布式系統 中,服務注冊中心 是最重要的基礎部分,必須處于 可以提供服務 的狀态。為了維持其 可用性,使用 叢集 是很好的解決方案。

Eureka

通過節點 對等注冊 的方式實作 高可用的部署,是以隻需要為每一個

Eureke Server

配置 其他可用的

Eureke Server

serviceUrl

,就能實作高可用部署。

spring:
  profiles:
    active: peer1 #peer2

---

spring:
  profiles: peer1
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/

---

spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
           

更改

Eureka Server

的配置檔案,再分别以

spring.profiles.active=peer1

spring.profiles.active=peer2

作為參數,啟動兩次

Eureka Server

即可。

  • 通路

    http://localhost:8761/

    。可以發現,

    Eureka Client

    已經向 端口号 為

    8761

    Eureka Server

    發起注冊。
  • 服務提供者 的 配置檔案 并沒有向 端口号 為

    8762

    Eureka Server

    注冊。通路

    http://localhost:8762/

    。可以發現,服務提供者 的資訊已經向

    8762

    發起注冊了,即

    8761

    的 注冊資訊 已經同步到

    8762

    節點。

參考

  • 方志朋《深入了解Spring Cloud與微服務建構》

歡迎關注技術公衆号: 零壹技術棧

Spring Cloud實戰系列(一) - 服務注冊與發現Eureka前言正文參考

本帳号将持續分享後端技術幹貨,包括虛拟機基礎,多線程程式設計,高性能架構,異步、緩存和消息中間件,分布式和微服務,架構學習和進階等學習資料和文章。

繼續閱讀