springcloud 搭建 eureka 集羣

概述

高可用集羣配置、當註冊中心扛不住高併發的時候,這時候 要用集羣來扛;一個掛了還能有其他的eureka 來頂。

集羣搭建

普通操作

1、這裏我們先建3個 module microservice-eureka-server-2001microservice-eureka-server-2002microservice-eureka-server-2003
在這裏插入圖片描述
2、三個項目的pom.xml 文件都一樣 這裏就填一份 microservice-eureka-server-2001 的其他的只有對應的項目名稱變了

<?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.cpc</groupId>
        <artifactId>cpc-spring-cloud</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>

        <!-- actuator監控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、在每個項目的啓動類上加 @EnableEurekaServer 這個註解
在這裏插入圖片描述
4、前面單機的時候 eureka註冊中心實例名稱 是localhost,現在是集羣,不能三個實例都是localhost,這裏複雜的辦法是搞三個虛擬機,麻煩,這裏有簡單辦法,直接配置本機hosts,來實現本機域名映射;
找到 C:\Windows\System32\drivers\etc 打開hosts,加配置
在這裏插入圖片描述
配置好了後執行下面這個命裏來讓配置生效(在命令行窗口執行)

// 刪除DNS緩存內容,從而達到更新DNS目的 
ipconfig /flushdns

在這裏插入圖片描述

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

server:
  port: 2001
  context-path: /

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

還有一點就是我們的被調用方服務,也就是客戶端這邊,原來是單機eureka所以它的配置文件中只配置了一個eureka的鏈接,現在是弄集羣:
在這裏插入圖片描述

eureka:
  instance:
    #eureka客戶端主機實例名稱
    hostname: localhost
    #客戶端服務名
    appname: microservice-student
    #客戶端實例名稱
    instance-id: microservice-student:1001
    #顯示IP
    prefer-ip-address: true
  client:
    service-url:
      # 單機
      # defaultZone: http://localhost:2001/eureka   #把服務註冊到eureka註冊中心
      # 集羣
      defaultZone: http://eureka2001.cpc.com:2001/eureka/,http://eureka2002.cpc.com:2002/eureka/,http://eureka2003.cpc.com:2003/eureka/

最後我們測試下:啓動三個註冊中心,以及服務提供者項目;
然後瀏覽器地址欄輸入:

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

這裏貼其中的一個頁面:
在這裏插入圖片描述

騷操作

上面eureka服務搭建,除了yml文件不一樣,其他文件都一樣,那麼我們有什麼辦法能夠將多個eureka服務集合到一個工程中去呢?

創建一個microservice-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.cpc</groupId>
        <artifactId>cpc-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-eureka-server</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>

        <!-- actuator監控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Yml文件(關鍵)
這是利用springboot多配置的特點,在啓動的時候激活指定的配置

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

idea中配置一下
在這裏插入圖片描述
在這裏插入圖片描述
跑起來的效果跟普通操作的效果是一致的;

Eureka自我保護機制

下面的紅字出現的原因是因爲eureka的自我保護機制,因爲服務沒有被調用的原因,要解決這個問題
在這裏插入圖片描述
某時刻某一個微服務不可用了,eureka不會立刻清理,依舊會對該微服務的信息進行保存
在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述
不推薦禁用.建議要更改也是改註銷實例的時間:
eureka.instance.lease-expiration-duration-in-seconds

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