Eureka的高可用
生活随笔
收集整理的這篇文章主要介紹了
Eureka的高可用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前我們講了如何將一個應用注冊到Eureka上來,現在已經注冊上來了,不知道大家有沒有注意到一個問題,就是我們的Eureka他是一個單點,是單臺服務器,假如這個Eureka他掛掉了怎么辦呢,那你后續的操作不是都做不了了,這服務器還是很脆弱的,人都會掛,更何況服務器呢,所以我們要實現Eureka的高可用,這是我們目前的一個情況,單個client,往單個Eureka上注冊,那我們要實現Eureka的高可用怎么做呢,大家自然而然的能夠想到,加一臺服務器唄,一個不行,我就搞兩個Eureka,如果你服務器只是多起了這么一個實例,那顯然也是不行的,你看你右邊孤零零的在那,跟左邊這個都沒有關系,那肯定得發生點關系才行,這里我們可以將兩個Eureka互相注冊,怎么個互相注冊法呢,我們來實際操作一下
互相注冊,就是你中有我,我中有你eureka8762client沒有修改,注冊地址就是8761eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekahttp://10.40.8.152:8762/我們看一下8762你會發現client也注冊上來,這是一個很神奇的事,明明client這邊只寫了8761,為什么也會注冊過來呢,我們將這兩個Eureka互相注冊了,互相注冊之后呢,他們的信息就會有所交換,雖然client只在Eureka1上面注冊,但是他會把信息同步到Eureka2上面去,所以你就會在Eureka2上看到Client服務,這樣子算是已經玩好了嗎,這個時候假如Eureka1掛掉了呢,會怎么樣呢,現在我把Eureka1給停掉,我們看一下這個時候沒了http://10.40.8.152:8761/Eureka2依然活著,并且client還是注冊在上面,說明我們這個做法起作用了,那假如我們這個時候client給重啟呢,我們重啟client,你看這邊都報錯了,都不能注冊上去,因為他地址只填了這一個,我們看Eureka2上的Client還是有顯示的,什么原因呢,還是一個心跳機制的問題,他還是顯示,我們可以把Eureka2給重啟一下,這個時候就沒有client了,那我們希望的肯定是Eureka1在掛掉之后,client也可以注冊上去,這怎么做呢,很簡單,你只要client每次往這兩個eureka注冊就好了,怎么做呢,你只需要在這個配置的地方給他配置兩個,61和62都配置上來eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka,http://admin:1234@10.40.8.152:8762/eureka我們現在也可以把Eureka1給啟動一下,Eureka1這邊也有client了,這個時候Eureka掛掉任何一臺,都不用擔心了,假如以后我面對很大很大的項目,服務很多,那我可能覺得兩個Eureka可能不夠用,我希望三個,那就更高可用了,三個怎么來做呢,如果是3個Eureka的話,應該讓他們兩兩注冊,注意看這個線,這個線已經變成兩端都是線頭,Eureka1要往2和3注冊,那么Client端呢,你就直接配置三個就好了,我們來演示一下
http://10.40.8.152:8761/其實我們也可以看下面,搭建集群其他的地址registered-replicas http://localhost:8762/eureka/, http://localhost:8763/eureka/往這兩個地址注冊了http://10.40.8.152:8762/registered-replicas http://localhost:8761/eureka/, http://localhost:8763/eureka/http://10.40.8.152:8763/registered-replicas http://localhost:8761/eureka/, http://localhost:8762/eureka/這是生產環境需要這樣子部署,開發環境我們還是簡單的來,部署一個就好了
server.port=8761
#eureka.instance.hostname=eureka1#spring.application.name=eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8762/eureka,http://admin:1234@localhost:8763/eureka
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false#eureka.server.enableSelfPreservation=truesecurity.basic.enabled=true
security.user.name=admin
security.user.password=1234eureka.datacenter=cloud
eureka.environment=product#eureka.server.enable-self-preservation=false
server.port=8762
#eureka.instance.hostname=eureka1#spring.application.name=eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka,http://admin:1234@localhost:8763/eureka
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false#eureka.server.enableSelfPreservation=truesecurity.basic.enabled=true
security.user.name=admin
security.user.password=1234eureka.datacenter=cloud
eureka.environment=product#eureka.server.enable-self-preservation=false
server.port=8763
#eureka.instance.hostname=eureka1#spring.application.name=eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka,http://admin:1234@localhost:8762/eureka
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false#eureka.server.enableSelfPreservation=truesecurity.basic.enabled=true
security.user.name=admin
security.user.password=1234eureka.datacenter=cloud
eureka.environment=product#eureka.server.enable-self-preservation=false
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><!-- Spring Boot進行單元測試的模塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=7900logging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%neureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka,http://admin:1234@10.40.8.152:8762/eureka,http://admin:1234@10.40.8.152:8763/eurekaspring.application.name=client
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
#eureka.instance.hostname=clientName
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);}
}
?
總結
以上是生活随笔為你收集整理的Eureka的高可用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eureka Client的使用
- 下一篇: 分布式下服务注册的地位和原理