服務注冊組件——Eureka高可用集群搭建


什么是Eureka?

服務注冊組件:將微服務注冊到Eureka中。


為什么需要服務注冊?

微服務開發重點在一個"微"字,大型應用拆分成微型服務,意味着服務的數量不可能少。


服務之間存在調用關系,假設沒有服務注冊,微服務之間的調用關系就會是這個樣子:

1566142251076

微服務的部署可能不會在同一台服務器上,而是需要通過遠程調用,然后就涉及到IP地址了。理論上來說,直接通過IP地址直接通信也沒有什么問題。 但是如果服務出問題,需要換一台服務器部署,ip地址就需要更改了。同時如果該服務被多個其他服務依賴,那么每一個IP地址都需要重置。


服務注冊可以形象的理解為一張表,表的左邊寫着服務名稱,而右側對應的是IP地址。服務的調用使用名稱來替代IP地址,那么當IP地址發生改變,直接修改服務注冊中心的名稱與IP的映射關系。其他服務由於是用名稱來遠程調用,所以並不需要更改。

1566142633779

Eureka與Zookeeper的區別

Eureka滿足Ap原則,而Zookeeper滿足CP原則。

(CAP定理,指的是在一個分布式系統中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分區容錯性),三者最多同時滿足倆)


Eureka三個角色

Eureka Server 提供服務注冊和發現

Service Provider服務提供方將自身服務注冊到Eureka,從而使服務消費方能夠找到

Service Consumer服務消費方從Eureka獲取注冊服務列表,從而能夠消費服務


消費方與提供方:調用另一個服務稱為消費。被另一個服務調用稱為提供。

所以,同一個服務可能既是消費方也是提供方。

【注】:Eureka隱藏IP地址的功能體現在整個微服務的內部。


搭建Eureka集群


創建工程

  • 創建文件夾Eureka Cluster,用IDEA打開:

1566144926784


  • 創建msc-eureka-6001,msc-eureka-6002,msc-eureka-6003工程【jdk1.8】,

1566145227822

1566145305544

1566145472114


導入pom依賴

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

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-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>

配置application.yaml

  • 配置地址映射

C:\Windows\System32\drivers\etc

1566147466705

1566147557267


msc-eureka-6001,

server:
  port: 6001

eureka:
  instance:
    hostname: eureka6001.com  #eureka服務端的實例名稱
  client:
    register-with-eureka: false   #false表示不向注冊中心注冊自己。
    fetch-registry: false      #false表示自己是注冊中心,職責是維護服務,而不是調用服務
    service-url:
      defaultZone: http://eureka6003.com:6003/eureka/,http://eureka6002.com:6002/eureka/ # 注冊到eureka6003和eureka6002里

msc-eureka-6002,

server:
  port: 6002

eureka:
  instance:
    hostname: eureka6002.com  #eureka服務端的實例名稱
  client:
    register-with-eureka: false   #false表示不向注冊中心注冊自己。
    fetch-registry: false      #false表示自己是注冊中心,職責是維護服務,而不是調用服務
    service-url:
      defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6003.com:6003/eureka/

msc-eureka-6003

server:
  port: 6003

eureka:
  instance:
    hostname: eureka6003.com  #eureka服務端的實例名稱
  client:
    register-with-eureka: false   #false表示不向注冊中心注冊自己。
    fetch-registry: false      #false表示自己是注冊中心,職責是維護服務,而不是調用服務
    service-url:
      defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/

創建主啟動類並添加注解@EnableEurekaServer

package zkrun.top;


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

@EnableEurekaServer // EurekaServer服務器端啟動類,接受其它微服務注冊進來
@SpringBootApplication
public class App_msc_eureka_6001
{
    public static void main(String[] args)
    {
        SpringApplication.run(App_msc_eureka_6001.class, args);
    }
}

啟動Eureka集群

1566148589864


訪問任意一個端口,都會顯示另外兩個Eureka服務

1566148651864


創建注冊服務測試Eureka集群的高可用性

  • 新建工程msc-provider-5001

  • 導入pom依賴

     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    <!--        Eureka客戶端啟動需要依賴web模塊-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    

  • 配置application.yaml
server:
  port: 5001
spring:
  application:
    name: msc-provider  #應用名稱
eureka:
  client:
    service-url:
      defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/,http://eureka6003.com:6003/eureka/
  instance:
    instance-id: msc-provider-5001
    prefer-ip-address: true     #訪問路徑可以顯示IP地址

  • 創建主啟動類添加注解@EnableEurekaClient
package zkrun.top;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

1566150379362


  • 測試Eureka集群高可用

手動停掉eureka6001,

1566150405399


eureka6001無法訪問:

1566150455835


6002和6003仍然提供注冊服務:

1566150489889

1566150527456



小結:

  • 三個Eureka和注冊服務都沒有繼承父工程,其pom依賴都是獨立的。其中注冊服務依賴spring-boot-starter-web。今后在設計父工程的pom時,應該考慮到每個子工程的依賴。
  • 與Eureka同級別的服務注冊組件還有Zookeeper,Consul等。根據具體的應用場景和組件的優勢來決定技術選取是軟件開發更應該注意的。(除了Eureka之外,同級別的其他組件也需要了解)



代碼參考:https://github.com/Noneplus/JavaDev-Note/tree/master/SpringCloud代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM