Eureka 入門(mén)案例

eureka-demo 聚合工程。SpringBoot 2.2.4.RELEASESpring Cloud Hoxton.SR1。

創(chuàng)建項(xiàng)目

我們創(chuàng)建聚合項(xiàng)目來(lái)講解 Eureka,首先創(chuàng)建一個(gè) pom 父工程。

添加依賴(lài)

pom.xml

<?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>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n21" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<groupId>com.example</groupId>

<artifactId>eureka-demo</artifactId>

<version>1.0-SNAPSHOT</version>



<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>

<properties>

<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>


<dependencyManagement>
<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement></pre>

</project>

注冊(cè)中心 eureka-server

在剛才的父工程下創(chuàng)建 eureka-server 注冊(cè)中心的項(xiàng)目。

創(chuàng)建項(xiàng)目

添加依賴(lài)

pom.xml

<?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>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n58" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>


<parent>
<groupId>com.example</groupId>
<artifactId>eureka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>


<dependencies>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies></pre>

</project>

配置文件

application.yml

server: port: 8761 # 端口

spring: application: name: eureka-server # 應(yīng)用名稱(chēng)

配置 Eureka Server 注冊(cè)中心

eureka: instance: hostname: localhost # 主機(jī)名,不配置的時(shí)候?qū)⒏鶕?jù)操作系統(tǒng)的主機(jī)名來(lái)獲取 client: register-with-eureka: false # 是否將自己注冊(cè)到注冊(cè)中心,默認(rèn)為 true fetch-registry: false # 是否從注冊(cè)中心獲取服務(wù)注冊(cè)信息,默認(rèn)為 true service-url: # 注冊(cè)中心對(duì)外暴露的注冊(cè)地址 defaultZone: http://{eureka.instance.hostname}:{server.port}/eureka/

此時(shí)如果直接啟動(dòng)項(xiàng)目是會(huì)報(bào)錯(cuò)的,錯(cuò)誤信息:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

,這是因?yàn)?Eureka 默認(rèn)開(kāi)啟了將自己注冊(cè)至注冊(cè)中心從注冊(cè)中心獲取服務(wù)注冊(cè)信息的配置,如果該應(yīng)用的角色是注冊(cè)中心并是單節(jié)點(diǎn)的話(huà),要關(guān)閉這兩個(gè)配置項(xiàng)。

啟動(dòng)類(lèi)

EurekaServerApplication.java

package com.example;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication // 開(kāi)啟 EurekaServer 注解 @EnableEurekaServer public class EurekaServerApplication {

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n86" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}</pre>

}

訪(fǎng)問(wèn)

訪(fǎng)問(wèn):http://localhost:8761/

今天要說(shuō)的Eureka 入門(mén)案例 篇暫時(shí)先說(shuō)這么多,了解更多技術(shù)干貨,關(guān)注公眾號(hào)【樂(lè)字節(jié)發(fā)送123可了解,我們一起學(xué)習(xí)吖】,我是哩哩,一個(gè)有趣的靈魂!下期見(jiàn)!

?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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