如何部署Eureka集群,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、晉安ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的晉安網(wǎng)站制作公司
建議先嚴(yán)格按照步驟來部署,不然容易出問題,可能出現(xiàn)的問題會(huì)在下邊說明
1、新建一個(gè)maven工程,pom示例如下
4.0.0 com.iceberg.eurekatest eureka-test 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE org.springframework.cloud spring-cloud-dependencies Greenwich.SR2 pom import org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-maven-plugin
2、在Application類上加上注解,開啟eureka
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
3、添加兩個(gè)配置文件 application-peer1.yaml
spring: application: name: eureka-server server: port: 8001 eureka: client: #是否將自己注冊到Eureka Server register-with-eureka: true #是否從Eureka Server獲取注冊信息 fetch-registry: true serviceUrl: defaultZone: "http://peer2:8002/eureka/" instance: prefer-ip-address: false hostname: "peer1"
application-peer2.yaml
spring: application: name: eureka-server server: port: 8002 eureka: client: #是否將自己注冊到Eureka Server register-with-eureka: true #是否從Eureka Server獲取注冊信息 fetch-registry: true serviceUrl: defaultZone: "http://peer1:8001/eureka/" instance: prefer-ip-address: false hostname: "peer2"
4、在host中添加兩條映射
127.0.0.1 peer1
127.0.0.1 peer2
5、在SpringBoot的啟動(dòng)參數(shù)上加上-Dspring.profiles.active=peer1,然后啟動(dòng)項(xiàng)目。
然后改成peer2,啟動(dòng)項(xiàng)目(共啟動(dòng)兩個(gè))
6、注意事項(xiàng)
(1)在啟動(dòng)第一個(gè)eureka的時(shí)候,會(huì)報(bào)下列異常
2019-07-29 15:49:57.335 WARN 15416 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.12.jar:1.9.12] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.12.jar:1.9.12] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.9.12.jar:1.9.12] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.12.jar:1.9.12] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.12.jar:1.9.12] at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:847) ~[eureka-client-1.9.12.jar:1.9.12] at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) ~[eureka-client-1.9.12.jar:1.9.12] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_212] at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [na:1.8.0_212] at java.util.concurrent.FutureTask.run(FutureTask.java) [na:1.8.0_212] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_212] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_212] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_212] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_212] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
這個(gè)異常的意思是配置的eureka-server未找到(因?yàn)槟氵€沒啟動(dòng)),當(dāng)?shù)诙€(gè)eureka-server啟動(dòng)好之后就正常了
(2)在eureka單機(jī)版配置中,register-with-eureka和fetch-registry 這兩個(gè)選項(xiàng)是false,但是集群版的eureka是利用服務(wù)發(fā)現(xiàn)來實(shí)現(xiàn)的,所以需要改成true來向服務(wù)端注冊自己并獲取客戶端信息
7、啟動(dòng)完成后,瀏覽器訪問localhost:8001和localhost:8002
看到下圖表示成功
1、不想加host,直接使用localhost
有些朋友可能不知道怎么改host或者干脆就是懶,直接用localhost取代之前的peer1和peer2,配置文件如下
application-peer1.yaml
spring: application: name: eureka-server server: port: 8001 eureka: client: #是否將自己注冊到Eureka Server register-with-eureka: true #是否從Eureka Server獲取注冊信息 fetch-registry: true serviceUrl: defaultZone: "http://localhost:8002/eureka/" instance: prefer-ip-address: false hostname: "localhost"
application-peer2.yaml
spring: application: name: eureka-server server: port: 8002 eureka: client: #是否將自己注冊到Eureka Server register-with-eureka: true #是否從Eureka Server獲取注冊信息 fetch-registry: true serviceUrl: defaultZone: "http://localhost:8001/eureka/" instance: prefer-ip-address: false hostname: "localhost"
這里分成兩種情況:
(1)單機(jī)部署兩個(gè)eureka,然后訪問http://localhost:8001/
可以看到registered-replicas那一欄為空了,為什么會(huì)這樣子?
我們來看下eureka的源碼中PeerEurekaNodes的resolvePeerUrls()方法,這個(gè)方法的作用是從配置的serviceUrl中獲取可用的地址
protected ListresolvePeerUrls() { InstanceInfo myInfo = applicationInfoManager.getInfo(); String zone = InstanceInfo.getZone(clientConfig.getAvailabilityZones(clientConfig.getRegion()), myInfo); List replicaUrls = EndpointUtils .getDiscoveryServiceUrls(clientConfig, zone, new EndpointUtils.InstanceInfoBasedUrlRandomizer(myInfo)); int idx = 0; while (idx < replicaUrls.size()) { //這個(gè)地方就是罪魁禍?zhǔn)? if (isThisMyUrl(replicaUrls.get(idx))) { replicaUrls.remove(idx); } else { idx++; } } return replicaUrls; } public boolean isThisMyUrl(String url) { final String myUrlConfigured = serverConfig.getMyUrl(); if (myUrlConfigured != null) { return myUrlConfigured.equals(url); } return isInstanceURL(url, applicationInfoManager.getInfo()); } //判斷url的hostnam和當(dāng)前的hostname是否一致,如果一致則直接忽略 public boolean isInstanceURL(String url, InstanceInfo instance) { String hostName = hostFromUrl(url); String myInfoComparator = instance.getHostName(); if (clientConfig.getTransportConfig().applicationsResolverUseIp()) { myInfoComparator = instance.getIPAddr(); } return hostName != null && hostName.equals(myInfoComparator); }
通過isInstanceURL()方法,eureka會(huì)把hostname相同的url移除掉,而恰好我們配置的都是localhost,所以雖然你啟動(dòng)了兩個(gè)eureka,但是它們不會(huì)把自己當(dāng)成集群
(2)多機(jī)(或者多個(gè)虛擬機(jī))部署
如果你把兩個(gè)eureka啟動(dòng)在了不同的主機(jī)上,并配置了localhost,那你會(huì)一直看到com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server這個(gè)錯(cuò)誤,因?yàn)樗鼰o法通過localhost找到另一個(gè)eureka
2、不使用域名,而是使用IP注冊
eureka提供了一個(gè)選項(xiàng),可以使eureka注冊的地址不使用域名而是IP,配置項(xiàng)是eureka.instance.prefer-ip-address=true
(1)單機(jī)多網(wǎng)卡部署
這里解釋一下什么是多網(wǎng)卡,我們安裝vmware之后,它會(huì)給我們創(chuàng)建一些虛擬網(wǎng)卡,比如我這邊它就給我創(chuàng)建了兩個(gè)虛擬地址
多網(wǎng)卡的意思是你使用ipconfig命令,能看到多個(gè)IP地址(只是我自己的定義,不代表學(xué)術(shù)定義)
這種情況下eureka注冊的IP跟你實(shí)際的IP可能是不同的,比如我剛才啟動(dòng)的eureka它的instance info顯示的IP就是 192.168.157.1,這種情況下他注冊的IP也是192.168.157.1,為了讓他注冊實(shí)際的IP,我們需要通過 eureka.instance.ip-address=10.60.44.136指定一下
然后問題就來了,假如你的peer1填的ip-address是10.60.44.136,peer2中的serviceUrl.defaultZone中的url也得是10.60.44.136,那peer2的ip-address用啥呢?也用10.60.44.136的話就會(huì)出現(xiàn)之前的問題,被eureka自己排除掉,所以你需要換一個(gè)IP且仍然表示本地的,比如127.0.0.1,配置文件如下所示: application-peer1.yaml
spring: application: name: eureka-server server: port: 8001 eureka: client: #是否將自己注冊到Eureka Server register-with-eureka: true #是否從Eureka Server獲取注冊信息 fetch-registry: true serviceUrl: defaultZone: "http://127.0.0.1:8002/eureka/" instance: prefer-ip-address: true hostname: "localhost" ip-address: "10.60.44.136"
application-peer2.yaml
spring: application: name: eureka-server server: port: 8002 eureka: client: #是否將自己注冊到Eureka Server register-with-eureka: true #是否從Eureka Server獲取注冊信息 fetch-registry: true serviceUrl: defaultZone: "http://10.60.44.136:8001/eureka/" instance: prefer-ip-address: true hostname: "localhost" ip-address: "127.0.0.1"
運(yùn)行后的結(jié)果如下圖
(2)單機(jī)單網(wǎng)卡部署
剛才是多網(wǎng)卡表示ipconfig有多個(gè)IP,單網(wǎng)卡就是只有一個(gè)IP啦,根據(jù)之前的描述,只有一個(gè)IP的情況下,你單機(jī)是無法部署集群成功的(會(huì)被忽略掉),所以就略過啦~
(3)多機(jī)部署
這個(gè)也沒啥好說的了,ip指定為能互相訪問的ip,只要正確配置就OK啦
看完上述內(nèi)容,你們掌握如何部署Eureka集群的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!