Eureka Server
生活随笔
收集整理的這篇文章主要介紹了
Eureka Server
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接下來我們就來展開對注冊中心的實踐,注冊中心就好比老師手中的名單,上面記錄班上所有同學的名字,在他要點名的時候,就掏出那份名單,那么到了微服務這兒呢,注冊中心也是類似的,他記錄著所有的信息,狀態,應用叫什么名字啊,在哪臺服務器上,目前是不是正常工作啊,在微服務架構里面,我們習慣把一個應用稱之為一個服務,以后我提到某某應用,或者某某服務,那么你要知道我指的是同一個東西,我們先來看一下Eureka這個單詞,我們看到這個單詞很有意思,他有兩層意思,一個作為感嘆詞,是有"有了,找到了!"的意思,那么作為名詞呢,是美國西北部的一個城市,注冊中心他所提供的就是,服務的注冊與發現,而注冊發現你看,有個找到了的意思,所以非常的吻合,非常合情合理,接下來我就去構建一個注冊中心,我們來看一下SpringCloud的官網https://spring.io/這邊有一個版本圖,上班就是SpringCloud的版本,最左邊這一列我們可以看到,它是各種組件,這些都是SpringCloud的組件,比如我們這里有一個config,之后我們會講到了配置中心,還有其他的gateway,netflix,你可以看到這邊有版本對應,我們剛剛用到的Eureka,就是在這個包下,你可以看到這邊的版本,上面的采用的是倫敦地鐵站的名字,倫敦地鐵站的名字還不是隨便取的,其實是按照字母的順序給排列起來的,C,D,E,F,版本是依次的遞增,這就是SpringCloud版本命名的一種方式,大家了解一下知道就行,以后你要找對應的版本呢,到官網上來找就好了
你在啟動的主類上要加一個注解@EnableEurekaServer,加上這個注解才能說明這個項目有注冊中心,Instances currently registered with Eureka當然現在發現是一個都沒有的DS Replicas這一欄是注冊中心,Server端有哪些應用,有幾個應用,目前只有這一個,下面這些就是系統的信息了,General Info可以看到這一欄registered-replicas http://localhost:8761/eureka/是注冊地址,默認的是localhost:8761端口,我們起的是8080,由于我們的應用雖然他是server端,但是同時他也是一個client端,他也需要找一個注冊中心,給注冊上去,我們可以點開這個注解看一下這里還有一個@EnableDiscoveryClient/*** Annotation to activate Eureka Server related configuration {@link EurekaServerAutoConfiguration}** @author Dave Syer* @author Biju Kunjummen**/@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {}他包含了Client,也就是我剛剛說的,既是Server,也是Client,為了使他不報錯,我們不妨來配置一下,配置一下他要注冊的地址,其實就是往自己身上注冊eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka/*** Map of availability zone to list of fully qualified URLs to communicate with eureka* server. Each value can be a single URL or a comma separated list of alternative* locations.** Typically the eureka server URLs carry protocol,host,port,context and version* information if any. Example:* http://ec2-256-156-243-129.compute-1.amazonaws.com:7001/eureka/** The changes are effective at runtime at the next service url refresh cycle as* specified by eurekaServiceUrlPollIntervalSeconds.*/
private Map<String, String> serviceUrl = new HashMap<>();{this.serviceUrl.put(DEFAULT_ZONE, DEFAULT_URL);
}它會往map里面put內容public static final String DEFAULT_ZONE = "defaultZone";public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX+ "/";/*** @author Spencer Gibb*/
public interface EurekaConstants {String DEFAULT_PREFIX = "/eureka";}啟動的時候仍舊報錯,其實這是個正常的問題,因為你服務端既是server,又是client,服務端還沒有起好的時候,客戶端他自己先去找,那肯定找不到,但是Eureka服務端和客戶端采用的是心跳的方式,它會隔一段時間再去檢測,你可以看到這邊,這個時候他已經注冊上去了,Application是UNKOWN,他不知道這個應用叫什么名字,但是上面已經有他的信息了,UNKONW始終讓人覺得不爽,怎么改一下呢spring.application.name=eureka他采用心跳的方式它會隔一段時間間隔,再去注冊,我們等一會,現在跳出來了,可以看到已經注冊上去了,EUREKA名字也已經出來了http://localhost:8761/這就已經注冊上來了,但是我們這個應用本身,他就是一個注冊中心,所以我們根本不需要讓他出現在這個上面,eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false可以看到我們等了一段的時間之后,可以看到這一串日志,跟之前的明顯不一樣,提示已經注冊上去,我們再來界面上看一下,同樣的也沒有注冊上去,這就是我們的注冊中心,由于我們后面的應用會有很多,JAVA默認是8080端口,這樣子到時不太好管理,我先把端口給改一下,改成8761,同樣項目的端口也改一下,8761,注冊中心項目相對比較簡單,因為他就是一張白紙,供大家往上面注冊,往上面寫東西的,所以本身東西也不多,以上就是Eureka Server端的內容
<?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.cloud</groupId><artifactId>eureka</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.2.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server.port=8761
#eureka.instance.hostname=eureka1#spring.application.name=eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/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
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}
?
總結
以上是生活随笔為你收集整理的Eureka Server的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sping Cloud Eureka
- 下一篇: Eureka Client的使用