二.服務(wù)注冊(cè)中心Eureka

Eureka是Netflix開(kāi)發(fā)的服務(wù)發(fā)現(xiàn)組件,本身是一個(gè)基于REST的服務(wù)。Spring Cloud將它集成在其子項(xiàng)目spring-cloud-netflix中,以實(shí)現(xiàn)Spring Cloud的服務(wù)發(fā)現(xiàn)功能

服務(wù)注冊(cè)

在服務(wù)治理框架中, 通常都會(huì)構(gòu)建一個(gè)注冊(cè)中心, 每個(gè)服務(wù)單元向注冊(cè)中心登記自己提供的服務(wù), 將主機(jī)與端口號(hào)、 版本號(hào)、 通信協(xié)議等一些附加信息告知注冊(cè)中心, 注冊(cè)中心按服務(wù)名分類(lèi)組織服務(wù)清單。

服務(wù)發(fā)現(xiàn)

由于在服務(wù)治理框架下運(yùn)作, 服務(wù)間的調(diào)用不再通過(guò)指定具體的實(shí)例地址來(lái)實(shí)現(xiàn), 而是通過(guò)向服務(wù)名發(fā)起請(qǐng)求調(diào)用實(shí)現(xiàn)。 所以, 服務(wù)調(diào)用方在調(diào)用服務(wù)提供方接口的時(shí)候, 并不知道具體的服務(wù)實(shí)例位置。 因此, 調(diào)用方需要向服務(wù)注冊(cè)中心咨詢服務(wù), 并獲取所有服務(wù)的實(shí)例清單, 以實(shí)現(xiàn)對(duì)具體服務(wù)實(shí)例的訪問(wèn)。

1.創(chuàng)建服務(wù)注冊(cè)中心(IntelliJ IDEA)
File->New->Module->Spring Initializr

image.png

Next->Cloud Discovery->Eureka Server->Next->Finish
image.png

pom.xml引入了必要的依賴

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>

依賴項(xiàng)注意點(diǎn):
https://github.com/spring-projects/spring-cloud/wiki/Spring-Cloud-Edgware-Release-Notes
spring-cloud-starter-eureka-server要替換為spring-cloud-starter-netflix-eureka-server,要不然會(huì)報(bào)錯(cuò),找不到j(luò)ar

通過(guò)@EnableEurekaServer注解啟動(dòng)服務(wù)注冊(cè)中心,這個(gè)注解需要在springboot工程的啟動(dòng)application類(lèi)上加:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

在默認(rèn)設(shè)置下,該服務(wù)注冊(cè)中心也會(huì)將自己作為客戶端來(lái)嘗試注冊(cè)它自己,所以我們需要禁用它的客戶端注冊(cè)行為,只需在 application.properties 中增加如下配置

server.port=8761

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  • eureka.client.register-with-eureka: 由于該應(yīng)用為注冊(cè)中心,所以設(shè)置為 false, 代表不向注冊(cè)中心注冊(cè)自己
  • eureka.client.fetch-registry:由于注冊(cè)中心的職責(zé)就是維護(hù)服務(wù)實(shí)例,它并不需要去檢索服務(wù), 所以也設(shè)置為 false。
    eureka server 是有界面的,啟動(dòng)工程,打開(kāi)瀏覽器訪問(wèn):http://localhost:8761界面如下:
    image.png

    可以看到如下圖所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 欄是
No application available 

沒(méi)有服務(wù)被發(fā)現(xiàn),因?yàn)闆](méi)有注冊(cè)服務(wù)當(dāng)然不可能有服務(wù)被發(fā)現(xiàn)了。

  1. 創(chuàng)建服務(wù)提供者 (eureka client)
    主要處理服務(wù)的注冊(cè)與發(fā)現(xiàn)??蛻舳朔?wù)通過(guò)注解和參數(shù)配置的方式,嵌入在客戶端應(yīng)用程序的代碼中,在應(yīng)用程序運(yùn)行時(shí),Eureka客戶端向注冊(cè)中心注冊(cè)自身提供的服務(wù)并周期性地發(fā)送心跳來(lái)更新它的服務(wù)租約,如果心跳超時(shí),則通常將該實(shí)例從注冊(cè)server中刪除。同時(shí),它也能從服務(wù)端查詢當(dāng)前注冊(cè)的服務(wù)信息并把它們緩存到本地并周期性地刷新服務(wù)狀態(tài).。

創(chuàng)建過(guò)程同服務(wù)注冊(cè)中心類(lèi)似
pom.xml中依賴如下

<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

通過(guò)注解@EnableEurekaClient或@EnableDiscoveryClient 表明自己是一個(gè)eurekaclient.
@EnableEurekaClient與@EnableDiscoveryClient的區(qū)別
https://stackoverflow.com/questions/31976236/whats-the-difference-between-enableeurekaclient-and-enablediscoveryclient
There are multiple implementations of "Discovery Service" (eureka, consul, zookeeper).@EnableDiscoveryClient lives in spring-cloud-commons and picks the implementation on the classpath. @EnableEurekaClient lives in spring-cloud-netflix and only works for eureka. If eureka is on your classpath, they are effectively the same.
因我們的注冊(cè)中心是Eureka,所以使用@EnableEurekaClient,其他注冊(cè)中心則使用@EnableDiscoveryClient

@SpringBootApplication
@EnableEurekaClient
@RestController
public class HiApplication implements ApplicationListener<WebServerInitializedEvent> {

    public static void main(String[] args) {
        SpringApplication.run(HiApplication.class, args);
    }

    private int serverPort;
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi " + name + ",serverPort:" + serverPort;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }
}

還需要在 application.properties配置文件中,通過(guò) spring.application.name屬性來(lái)為服務(wù)命名,比如命名為hello-service。 再通過(guò)eureka.client.service-url.defaultZone屬性來(lái)指定服務(wù)注冊(cè)中心的地址

eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
server.port=8762
spring.application.name=hello-service

服務(wù)之間相互調(diào)用一般是根據(jù)spring.application.name。

分別啟動(dòng)服務(wù)注冊(cè)中心,以及hello-service,打開(kāi)瀏覽器訪問(wèn):http://localhost:8761界面如下

image.png

可以看到一個(gè)服務(wù)已經(jīng)注冊(cè)在服務(wù)中了,服務(wù)名為hello-service,端口為8762

  1. 高可用注冊(cè)中心
    Eureka Server的高可用實(shí)際上就是將自己作為服務(wù)向其他服務(wù)注冊(cè)中心注冊(cè)自己,這樣就可以形成一組互相注冊(cè)的服務(wù)注冊(cè)中心, 以實(shí)現(xiàn)服務(wù)清單的互相同步, 達(dá)到高可用的效果
  • 2.1 在剛創(chuàng)建的Eureka 服務(wù)注冊(cè)中心,resources文件夾下,創(chuàng)建配置文件application-peer1.properties,作為peer1服務(wù)中心的配置,并將serviceurl指向peer2
spring.application.name=eureka-server
server.port=8761
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:8861/eureka/
  • 2.2.創(chuàng)建另外一個(gè)配置文件application-peer2.properties,將serviceUrl指向peer1`
spring.application.name=eureka-server
server.port=8861
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/
  • 2.3 在/etc/hosts文件中添加對(duì)peerl 和peer2的轉(zhuǎn)換, 讓上面配置的host形式的serviceUrl能在本地正確訪間到; Windows系統(tǒng)路徑為C:\Windows\System32\drivers\etc\hosts。添加如下兩行
127.0.0.1 peer1
127.0.0.1 peer2
  • 2.4Maven Projects->對(duì)應(yīng)server項(xiàng)目->Lifecycle->clean,package,來(lái)生成相應(yīng)的jar包,命令窗口cd到包的路徑,通過(guò)spring.profiles.active屬性來(lái)分別啟動(dòng)peerl和peer2:
java -jar  server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar  server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
 

此時(shí)訪問(wèn)注冊(cè)中心http://peer1:8761/,可以看到已經(jīng)有peer2節(jié)點(diǎn)的eureka-server了,同樣http://peer2:8861/,也可以看到peer1節(jié)點(diǎn)的eureka-server

image.png

  • 2.5 在設(shè)置了多節(jié)點(diǎn)的服務(wù)注冊(cè)中心之后,再修改hello-service的application.properties 的配置
eureka.client.serviceUrl.defaultZone= http://peer1:8761/eureka/,http://peer2:8861/eureka/
server.port=8762
spring.application.name=hello-service

將eureka.client.serviceUrl.defaultZone指向了peer1跟peer2,啟動(dòng)服務(wù)后,在http://peer1:8761/http://peer2:8861/,可以看到都注冊(cè)了hello-service

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容