欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

快速搭建Eureka注册中心

程序员文章站 2022-12-08 19:47:35
一、步骤1、创建 SpringBoot 项目引入starter:Spring WebEureka Server2、在项目的主启动类上添加注解 @EnableEurekaServer@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.ru....




一、步骤

1、创建 SpringBoot 项目

引入starter:

  • Spring Web
  • Eureka Server

2、在项目的主启动类上添加注解 @EnableEurekaServer

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

3、增加 Eureka 配置

# 基本配置,应用名称
spring.application.name=eureka-server

# Eureka Server 配置
# 服务端口
server.port=8761 eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false # 注册中心地址,client 向这个地址进行注册服务
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ 

4、运行注册中心

在IDEA中直接运行,或者直接用 java -jar 命令运行。


注:在本地运行多个 Eureka 注册中心,需要改变服务运行端口
java -jar -Dserver.port=xxxx

5、验证注册中心是否启动

访问:localhost:8761,如果成功启动会跳转到 Eureka 注册中心页面。

二、Eureka client 配置

1、引入依赖 starter

  • Eureka Discovery Client
  • Spring Web

2、@EnableDiscoveryClient


在需要注册的 Client 的启动类上添加注解 @EnableDiscoveryClient

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

3、添加 Eureka 配置


在配置文件 application.properties 中添加:

# 应用名称
spring.application.name=eureka-client-test

# Eureka client 配置
# Client 服务地址
server.port=1234 # 注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ 

4、查看服务是否注册成功

  • 进入 Eureka 配置中心:http://localhost:8761
  • 可以发现 服务 EUREKA-CLIENT-TEST 已经注册成功,可以从下面看到该服务的名称,IP地址和端口号以及服务的数量。

Instances currently registered with Eureka

Application AMIs Availability Zones Status
EUREKA-CLIENT-TEST n/a (1) (1) UP (1) - 192.168.43.17:eureka-client-test:1234
HELLOCLIENT n/a (1) (1) UP (1) - 192.168.43.17:HelloClient:7211
HELLOSERVER n/a (2) (2) UP (2) - 192.168.43.17:HelloServer:7111 , 192.168.43.17:HelloServer:7011


如果有下面的报错信息,则表明Eureka 注册中心没有启动,或者注册中心地址输入错误。
com.netflix.discovery.shared.transport.TransportException: There is no known eureka server; cluster server list is empty
	at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:108) ~[eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:873) ~[eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) [eureka-client-1.9.25.jar:1.9.25] at com.netflix.discovery.InstanceInfoReplicator$1.run(InstanceInfoReplicator.java:101) [eureka-client-1.9.25.jar:1.9.25] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_251] at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [na:1.8.0_251] at java.util.concurrent.FutureTask.run(FutureTask.java) [na:1.8.0_251] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_251] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_251] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_251] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_251] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_251] 

三、参考

[1] Eureka 官方wiki
[2] Spring Cloud Netflix DOC

本文地址:https://blog.csdn.net/IMBA_09/article/details/108034836