這篇文章主要介紹如何在Eureka注冊中心內(nèi)注冊多個EurekaServer服務(wù)端
建項目
創(chuàng)建一個Maven項目,在里面創(chuàng)建三個小的Maven空項目,具體結(jié)構(gòu)如下。
EurekaServer3,即外面的大模塊,為了清楚的觀察項目結(jié)構(gòu),我將其src文件夾刪除了,因為這個沒有用。
導(dǎo)Pom依賴
因為里面的Eureka1-3是同一級別的在EurekaServer3模塊內(nèi)部,因此我們不需要挨個模塊去導(dǎo)入依賴,只需在最外層模塊中的pom文件中導(dǎo)入即可。具體依賴如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
創(chuàng)建啟動類
啟動類,即我在項目里創(chuàng)建的EurekaApplication1-3.因為都是Eureka服務(wù)端,故他們?nèi)齻€模塊的啟動類,除了名字不同,其余的內(nèi)容均一致。
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
System.out.println("注冊中心啟動?。?!");
}
}
后面兩個模塊的啟動類也和這個類似。
yml文件
Eureka1,即注冊中心的application.yml
# 服務(wù)器端口配置
server:
port: 6869
# Spring Boot 應(yīng)用配置
spring:
application:
# 應(yīng)用名稱
name: eureka
# Eureka Server 配置
eureka:
instance:
# Eureka 實例的主機(jī)名
hostname: EurekaServer
client:
# Eureka 客戶端配置
service-url:
# Eureka 服務(wù)器的地址配置
defaultZone: http://localhost:${server.port}/eureka/
# 是否將該實例注冊到 Eureka 服務(wù)器,默認(rèn)為 false
register-with-eureka: false
具體代碼即解釋已給出,但對于yml文件來說一定要注意格式?。?!
Eureka2,即服務(wù)端1的application.yml
# 服務(wù)器端口配置
server:
port: 6870
# Spring Boot 應(yīng)用配置
spring:
application:
# 應(yīng)用名稱
name: eureka1
# Eureka Server 配置
eureka:
instance:
# Eureka 實例的主機(jī)名
hostname: EurekaServer1
client:
# Eureka 客戶端配置
service-url:
# Eureka 服務(wù)器的地址配置
defaultZone: http://localhost:6869/eureka/
Eureka3,即服務(wù)端1的application.yml
server:
port: 6871
spring:
application:
name: eureka2
eureka:
instance:
hostname: EurekaServer2
client:
service-url:
defaultZone: http://localhost:6869/eureka/
運行
從上到下依次運行即可,注意要等上一個程序運行成功后再運行下一個。文章來源:http://www.zghlxwxcb.cn/news/detail-789845.html
測試
在瀏覽器上輸入[http://127.0.0.1:6869/],即可跳轉(zhuǎn)到Eureka注冊中心。
在只運行注冊中心的時候會顯示如下界面
在都運行成功的時候:文章來源地址http://www.zghlxwxcb.cn/news/detail-789845.html
到了這里,關(guān)于在Eureka中注冊多個服務(wù)(根據(jù)本地主機(jī)端口號區(qū)分)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!