【Eureka】介绍与基本使用

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

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

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

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

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

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3 创建启动类,使用@EnableEurekaServer注解启用Eureka服务器:
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客户端依赖:
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
2 在application.properties或application.yml中添加Eureka客户端配置:
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    preferIpAddress: true
3 在启动类上使用@EnableDiscoveryClient注解来启用服务发现:
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服务器来发现和调用该客户端的服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yinying293

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值