1. 程式人生 > >Eureka高可用叢集環境搭建

Eureka高可用叢集環境搭建

註冊中心叢集

在微服務中,註冊中心非常核心,可以實現服務治理,如果一旦註冊出現故障的時候,可能會導致整個微服務無法訪問,在這時候就需要對註冊中心實現高可用叢集模式。

Eureka叢集相當簡單:相互註冊

Eureka高可用實際上將自己作為服務向其他服務註冊中心註冊自己,這樣就可以形成一組相互註冊的服務註冊中心,從而實現服務清單的互相同步,達到高可用效果。

 

叢集的服務名稱要統一,要相同!

 

啟動時候 報錯 正常! 啟動時候互相註冊  不會同時啟動成功的

啟動類都是一樣的

package com.toov5;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer //開啟註冊中心 @SpringBootApplication public class AppEureka { public static void main(String[] args) { SpringApplication.run(AppEureka.
class, args); } }

pom都一樣的:

<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.toov5</groupId>
  <artifactId>EurekaClaster</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!--SpringCloud eureka-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
	</dependencies>
	<!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
  
</project>

  

看不同的yml:

server:
  port: 9100
spring:
  application:
    name: app-toov5-member
#eureka:
#  client:
#    service-url:
#      defaultZone: http://localhost:8100/eureka
###叢集地址  或者不寫死 埠號改變 埠號改為對方的eureka埠號
eureka:
  client:
    service-url:
##           defaultZone: http://${eureka.instance.hostname}:8100/eureka/  ##其他eureka服務的地址
           defaultZone: http://127.0.0.1:8100/eureka/  ##其他eureka服務的地址
    register-with-eureka: true
    fetch-registry: true

  

###eureka 服務埠號
server:
  port: 8100
###服務註冊名稱
spring:
  application:
    name: app-toov5-member  ##############要相同
eureka:
  instance:
  ##註冊中心ip地址
    hostname: 127.0.0.1
###客戶端呼叫地址
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:9100/eureka/  ##其他eureka服務的地址     互相註冊 寫對方的地址+埠
###因為該應用為註冊中心,不會註冊自己 (叢集設為true)
    register-with-eureka: true
###因為自己為註冊中心 ,不會去在該應用中的檢測服務  
    fetch-registry: true