eureka 集羣

Eureka集羣搭建

先講一下集羣有什麼用?

比如說:一共有a,b,c三個服務註冊中心,
三個分別對應這a模塊,b模塊,c模塊
如果不是集羣的話,此時aEureka掛了,那麼對應的a模塊就訪問不了,即使a模塊沒有問題。
如果是集羣的話,此時aEureka掛了後,bEureka和cEureka都可以繼承好他的任務(分服務給a模塊)
所以說一個多層次的集羣可以很好的阻止服務功能失效。

簡單來說:當註冊中心扛不住高併發的時候,這時候要用集羣來扛;
俗話說的好:團結力量大

這裏有兩種集羣搭建,我們來一個個講解
在這裏插入圖片描述

我們先來說說第一種:

我們要再之前的基礎上新建兩個module

  1. microservice-eureka-server-2002
  2. microservice-eureka-server-2003

2002和2003的pom.xml與2001的差不多,主要是注意修改下面這個

<artifactId>microservice-eureka-server-2001</artifactId>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lst</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-eureka-server-2001</artifactId>

    <properties>
        <java.version>1.8</java.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>
        <!--  修改後立即生效,熱部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

在2002啓動類MicroserviceEurekaServer2002Application 與
2003啓動類MicroserviceEurekaServer2003Application 里加入以下註解

@EnableEurekaServer

前面只有一個的時候 eureka註冊中心實例名稱 是localhost,現在是集羣,不能三個實例都是localhost,這裏複雜的辦法是搞三個虛擬機,麻煩,這裏有簡單辦法,我們要直接配置本機hosts,就是欺騙IP來實現本機域名映射;
找到 C:\Windows\System32\drivers\etc 打開hosts,加配置

127.0.0.1  eureka2001.lst.com
127.0.0.1  eureka2002.lst.com
127.0.0.1  eureka2003.lst.com

在這裏插入圖片描述

接下來修改三個項目的application.yml文件,主要是修改 hostname和defaultZone,
2001:

server:
  port: 2001
  context-path: /

eureka:
  instance:
    # 單機 hostname: localhost #eureka註冊中心實例名稱
    hostname: eureka2001.lst.com # 集羣
  client:
    register-with-eureka: false     #false 由於該應用爲註冊中心,所以設置爲false,代表不向註冊中心註冊自己。
    fetch-registry: false     #false 由於註冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置爲false
    service-url:
      defaultZone: http://eureka2002.lst.com:2002/eureka/,http://eureka2003.lst.com:2003/eureka/ # 集羣
      #單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設置與Eureka註冊中心交互的地址,查詢服務和註冊服務用到


2002:

server:
  port: 2002
  context-path: /

eureka:
  instance:
    # 單機 hostname: localhost #eureka註冊中心實例名稱
    hostname: eureka2002.lst.com # 集羣
  client:
    register-with-eureka: false     #false 由於該應用爲註冊中心,所以設置爲false,代表不向註冊中心註冊自己。
    fetch-registry: false     #false 由於註冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置爲false
    service-url:
      defaultZone: http://eureka2001.lst.com:2001/eureka/,http://eureka2003.lst.com:2003/eureka/ # 集羣
      #單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設置與Eureka註冊中心交互的地址,查詢服務和註冊服務用到


2003:

server:
  port: 2003
  context-path: /

eureka:
  instance:
    # 單機 hostname: localhost #eureka註冊中心實例名稱
    hostname: eureka2003.lst.com # 集羣
  client:
    register-with-eureka: false     #false 由於該應用爲註冊中心,所以設置爲false,代表不向註冊中心註冊自己。
    fetch-registry: false     #false 由於註冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置爲false
    service-url:
      defaultZone: http://eureka2001.lst.com:2001/eureka/,http://eureka2002.lst.com:2002/eureka/ # 集羣
      #單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設置與Eureka註冊中心交互的地址,查詢服務和註冊服務用到


效果展示:
瀏覽器地址欄輸入:

http://eureka2001.lst.com:2001/
http://eureka2002.lst.com:2002/
http://eureka2003.lst.com:2003/

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

第二種:

上面eureka服務搭建,除了yml文件不一樣,其他文件都一樣,那麼我們有什麼辦法能夠將多個eureka服務集合到一個工程中去呢?
創建一個microservice-eureka-server(三合一)子工程
pom依賴與啓動類Application是一樣的操作
配置yml文件

---
server:
  port: 2001
  context-path: /
eureka:
  instance:
    hostname: eureka2001.lst.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2002.lst.com:2002/eureka/,http://eureka2003.lst.com:2003/eureka/
spring:
  profiles: eureka2001
---
server:
  port: 2002
  context-path: /
eureka:
  instance:
    hostname: eureka2002.lst.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.lst.com:2001/eureka/,http://eureka2003.lst.com:2003/eureka/
spring:
  profiles: eureka2002
---
server:
  port: 2003
  context-path: /
eureka:
  instance:
    hostname: eureka2003.lst.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.lst.com:2001/eureka/,http://eureka2002.lst.com:2002/eureka/
spring:
  profiles: eureka2003

然後配置啓動
在這裏插入圖片描述
效果和第一種的效果一樣,這裏就不展示了

Eureka自我保護機制

在這裏插入圖片描述
開發環境,我們經常會遇到這個紅色的警告;
當我們長時間爲訪問服務以及變更服務實例名稱的時候,就會出現這個紅色警告;

默認情況,如果服務註冊中心再一段時間內沒有接收到某個微服務實例的心跳,服務註冊中心會註銷該實例(默認90秒)。

由於正式環境,經常會有網絡故障,網絡延遲問題發生,服務和註冊中心無法正常通信,此時服務是正常的,不應該註銷該服務,Eureka這時候,就通過“自我保護模式”來解決問題,當短時間和服務失去通信時,保留服務信息,當恢復網絡和通信時候,退出“自我保護模式”;
通過“自我保護模式”,使Eureka集羣更加的健壯和穩定;

over。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章