微服務學習之Eureka註冊中心集羣環境構建【Hoxton.SR1版】

目錄

 

1 註冊中心集羣原理

2 註冊中心集羣配置

2.1 修改host文件

2.2 pom.xml、application.yml、application-7002.yml

3 分別啓動註冊中心


1 註冊中心集羣原理

Eureka Server集羣原理:互相註冊,相互守望。如果一個微服務項目中只有一個註冊中心,當註冊中心出故障,那麼對於系統來說就是災難性的。註冊中心一定要保證高可用,集羣就是保障的手段。通過Eureka Server集羣,實現負載均衡、故障容錯。

2 註冊中心集羣配置

2.1 修改host文件

找到C:\Windows\System32\drivers\etc下的hosts文件,添加如下配置:

127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com

2.2 pom.xml、application.yml、application-7002.yml

pom.yml

<dependencies>
        <!-- Eureka Server -->
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 引入自定義的api通用包 -->
        <dependency>
            <groupId>com.bighuan.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
         <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.bighuan.springcloud.EurekaMain7001</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    </dependencies>

application.yml

eureka-server7001的yml文件:

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com  #eureka服務端的實例名稱
  client:
    # false表示不向註冊中心註冊自己
    register-with-eureka: false
    # false表示自己就是註冊中心,職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      # 設置與Eureka Server交互的地址,查詢服務和註冊服務都需要依賴這個地址
      defaultZone: http://eureka7002.com:7002/eureka

application-7002.yml

eureka-server7002的yml文件:

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com  #eureka服務端的實例名稱
  client:
    # false表示不向註冊中心註冊自己
    register-with-eureka: false
    # false表示自己就是註冊中心,職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      # 設置與Eureka Server交互的地址,查詢服務和註冊服務都需要依賴這個地址
      defaultZone: http://eureka7001.com:7001/eureka

兩個Eureka Server互相註冊,作爲彼此的註冊中心。如果有三個註冊中心,那麼就是一個Eureka Server向另外兩個Eureka Server註冊,以此類推。

3 分別啓動註冊中心

打包:

mvn clean package  -Dmaven.test.skip=true

分別以eureka7001.com、eureka7002.com配置信息啓動Eureka

java -jar cloud-eureka-server7001-1.0-SNAPSHOT.jar --spring.profiles.active=7001

java -jar cloud-eureka-server7001-1.0-SNAPSHOT.jar --spring.profiles.active=7002

分別訪問http://eureka7001.com:7001/http://eureka7002.com:7002/,從下圖可以看到,可以看到Eureka Server7001的相鄰節點爲Eureka Server7002,而後者的相鄰節點爲前者。

 

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