1. 程式人生 > >【Spring Cloud】第一篇 Service Discovery | 服務發現

【Spring Cloud】第一篇 Service Discovery | 服務發現

Eureka 提供服務的註冊,服務可以通過註冊到Eureka然後被其他應用呼叫。

看到Spring Cloud 的文件裡面是先講的是Spring Cloud Config ,為了方便,或者說參考其他大佬的教程,我也會把Config放到後面寫。

word & phrase

  • include 確實是包含的意思,但是我覺得翻譯(根據語境去翻譯)成使用是不是更舒服些
  • setting up 配置
  • provides 提供
  • meta-data 元資料
  • such as 例如
  • Note 注意

Spring Cloud Netflix

Netflix 是一家媒體提供商,應該很厲害!

官網文件中先列出的是Eureka客戶端,我嘗試先寫Eureka Client 發現會提示以下錯誤,應該是在請求 Eureka server的時候報錯, 因為我們根本沒有建立。所以我先看Eureka server

NFO 25402 --- [main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
ERROR 25402 --- [main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

說明

官方說明 Finchley的構建和工作環境應該是 Spring Boot 2.0.x, 預計不會在Spring Boot 1.5.x 中使用。

Finchley builds and works with Spring Boot 2.0.x, and is not expected to work with Spring Boot 1.5.x.

Spring Cloud 需要使用Spring Boot,如果不會建立Spring Boot專案的看這裡《Spring Boot | 使用Spring Initializr快速建立》!

建立專案

  • 新建好Spring Boot後,刪除src檔案

  • pom.xml 中新增 Spring Cloud 配置

<?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>

    <groupId>com.cyinfotech</groupId>
    <artifactId>sc-f-e-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-f-e-01</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- Spring Cloud -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</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>

Service Discovery: Eureka Server | 服務發現:Eureka服務端

How to Include Eureka Server | 專案中如何包含 Eureka Server

  • 右鍵專案 > New > Module 新建 eureka-server 子工程

  • 修改父工程pom.xml檔案, 新增子工程。

    <modules>
        <module>eureka-server</module>
    </modules>

To include Eureka Server in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-netflix-eureka-server. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

在專案中使用Eureka Server,需要配置 pom.xml, 根據你當前的Spring Cloud 版本在Spring Cloud Project page中可以檢視詳細說明。

eureka-serverpom.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>

    <groupId>com.cyinfotech</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cyinfotech</groupId>
        <artifactId>sc-f-e-01</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

</project>

How to Run a Eureka Server | 如何啟動 Eureka Server

在啟動類新增 註解 @EnableEurekaServer

  • Modify EurekaServerApplication
package com.cyinfotech.eurekaserver;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServerApplication.class).web(true).run(args);
    }
}

Standalone Mode | 單機模式

  • Add application.yml
server:
  port: 8761 # 埠

eureka:
  instance:
    hostname: localhost # 主機
  client:
    registerWithEureka: false #是否註冊自己
    fetchRegistry: false #是否註冊自己
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 服務地址
  • Start

  • Open http://127.0.0.1:8761/ in your browser

Instances currently registered with Eureka 是空的, 因為有沒有服務註冊!

Service Discovery: Eureka Clients | 服務發現:Eureka客戶端

Eureka客戶端可以理解為服務註冊實際專案中需要曝光的服務。

How to Include Eureka Client | 如何使用Eureka客戶端

  • 右鍵專案 > New > Module 新建 eureka-client 子工程,和新增 server 一樣

  • 修改父工程pom.xml檔案, 新增子工程。

    <module>eureka-client</module>

To include the Eureka Client in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-netflix-eureka-client. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

要使用Eureka Client 就要新增配置檔案,完整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>

    <groupId>com.cyinfotech</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cyinfotech</groupId>
        <artifactId>sc-f-e-01</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--Eureka-Client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

</project>

Registering with Eureka | 註冊 Eureka

When a client registers with Eureka, it provides meta-data about itself — such as host, port, health indicator URL, home page, and other details. Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.

當註冊Eureka時,它提供一些包括自己的源資料,例如:主機、埠、監聽(心跳)地址、主頁和其它詳細資訊。Eureka 通過每個例項的服務接受心跳訊息。 如果心跳在配置的時間失敗結束,那這個例項通常會刪除註冊。

修改啟動Eureka Client 啟動類

package com.cyinfotech.eurekaclient;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class EurekaClientApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
    }
}

Note that the preceding example shows a normal Spring Boot application. By having spring-cloud-starter-netflix-eureka-client on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:

注意前面的正常的 Spring boot 程式, 你的應用程式中新增spring-cloud-starter-netflix-eureka-client後將自動註冊到Eureka Server, 如下所示:

server:
  port: 8781
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

啟動步驟: 1.EurekaServerApplication 2.EurekaClientApplication

以上 Eureka 的服務端和客戶端(註冊與發現)就講完了,我也是第一次這麼認真看官方文件,其實看下來發現也不難,其他人的文件裡面也無非就直譯了官方文件,但是每個人的理解不一樣,所以還是推薦自己去看原版文件, 沒有想象的那麼難。