【Eureka】介绍与基本使用

Eureka介绍与基本使用

    • 一个简单的Eureka服务器的设置方法:
        • [1 在pom.xml中添加Eureka服务器依赖:](#1 在pom.xml中添加Eureka服务器依赖:)
        • [2 在application.properties或application.yml中添加Eureka服务器配置:](#2 在application.properties或application.yml中添加Eureka服务器配置:)
        • [3 创建启动类,使用@EnableEurekaServer注解启用Eureka服务器:](#3 创建启动类,使用@EnableEurekaServer注解启用Eureka服务器:)
    • 一个Eureka客户端的设置方法:
        • [1 在pom.xml中添加Eureka客户端依赖:](#1 在pom.xml中添加Eureka客户端依赖:)
        • [2 在application.properties或application.yml中添加Eureka客户端配置:](#2 在application.properties或application.yml中添加Eureka客户端配置:)
        • [3 在启动类上使用@EnableDiscoveryClient注解来启用服务发现:](#3 在启动类上使用@EnableDiscoveryClient注解来启用服务发现:)

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。

SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

Eureka是Netflix开发的一个用于实现服务注册和发现的服务。Spring Cloud集成了Eureka,使我们可以非常方便地将Eureka集成到Spring Cloud的微服务架构中。

一个简单的Eureka服务器的设置方法:

1 在pom.xml中添加Eureka服务器依赖:
xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
2 在application.properties或application.yml中添加Eureka服务器配置:
yaml 复制代码
server:
  port: 

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3 创建启动类,使用@EnableEurekaServer注解启用Eureka服务器:
java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

启动Eureka服务器后,就可以在http://localhost:8761/上看到Eureka的管理页面。

对于Eureka客户端,通常是指那些将自身服务注册到Eureka服务器,并从Eureka服务器获取其他服务信息的客户端。这通常是指微服务架构中的各个服务。

一个Eureka客户端的设置方法:

1 在pom.xml中添加Eureka客户端依赖:
xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
2 在application.properties或application.yml中添加Eureka客户端配置:
yaml 复制代码
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    preferIpAddress: true
3 在启动类上使用@EnableDiscoveryClient注解来启用服务发现:
java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

启动Eureka客户端后,它会自动将自己注册到Eureka服务器,其他服务则可以通过Eureka服务器来发现和调用该客户端的服务。

相关推荐
疯子的梦想@12 小时前
记录一次docker+k3s+防火墙规则冲突,导致服务无法正常启动的现象。
docker·容器·eureka
cuber膜拜17 小时前
Docker的简单介绍
docker·容器·eureka
铭keny2 天前
华为欧拉系统(openEuler)安装 Docker 容器完整教程
云原生·eureka
indexsunny2 天前
互联网大厂Java面试实战:从Spring Boot到微服务架构的三轮提问
java·spring boot·微服务·eureka·kafka·mybatis·spring security
溜达的大象3 天前
Navidrome 打造专属无损音乐库,加载cpolar局域网外访问也能超丝滑
阿里云·docker·云原生·eureka
hi_link3 天前
Docker 端口绑定 0.0.0.0,但 127.0.0.1 访问不到服务的问题总结
云原生·eureka
深入技术了解原理3 天前
引入eureka依赖但是无法注册:无法解析配置属性 ‘eureka.client.service-url.defaultZone‘
spring boot·spring cloud·云原生·eureka
fcm193 天前
导出已有的docker容器
docker·容器·eureka
若离学姐3 天前
Spring Cloud 零基础教程:Eureka 实战
spring·spring cloud·eureka
深入技术了解原理4 天前
eureka-client依赖爆红无法下载
spring cloud·云原生·eureka