Eureka简介及简单使用

一、Eureka简介


Eureka:服务注册与发现提供了一个服务注册中心、服务发现的客户端,还有一个方便查看所有注册的服务的界面。所有的服务使用Eureka的服务发现客户端来将自己注册到Eureka的服务器上。在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。

Eureka由两个组件组成: Eureka服务器和Eureka客户端。

Eureka Server提供服务注册服务
    各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。

EurekaClient通过注册中心进行访问
    它是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)。

二、简单搭建和使用步骤

1.Eureka服务器端

        在pom.xml文件依赖添加(还需要导入lombok依赖)

<!-- Eureka服务端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

        applicaltion.yaml配置文件

# 注册中心的端口
server:
  port: 6001
eureka:
  client:
    service-url:
      # 注册中心的地址
      defaultZone: http://127.0.0.1:6001/eureka
    # 注册中心无需拉去服务地址列表
    # 默认值是true
    fetch-registry: false
    # 注册中心无需注册自己到节点中
    # 默认值是true
    register-with-eureka: false

        在启动类EurekaServerApplication.java中

@SpringBootApplication
@EnableEurekaServer
@Slf4j
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
        log.info("Eureka服务端已经启动...");
    }
}

启动后通过:http://127.0.0.1:6001/

可以访问Eureka主页:

2.Eureka客户端(消费者和提供者)

提供者:pom.xml

<!-- 注册中心客户端启动依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

         applicaltion.yaml

server:
  port: 7001
spring:
  application:
    # 注册到注册中心的微服务名称
    name: eurekaClient_provider1
eureka:
  client:
    service-url:
      # 注册中心地址
      defaultZone: http://localhost:6001/eureka
  instance:
    # 使用ip地址注册服务
    prefer-ip-address: true

        在启动类EurekaClientProvider1Application.java中

@SpringBootApplication
@EnableEurekaClient
@Slf4j
public class EurekaClientProvider1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientProvider1Application.class, args);
        log.info("Eureka服务端提供者启动成功...");
    }

}

消费者:pom.xml

<!-- 注册中心客户端启动依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

        applicaltion.yaml

server:
  port: 8001
spring:
  application:
    # 注册到注册中心的微服务名称
    name: eurekaClient_consumer1
eureka:
  client:
    service-url:
      # 注册中心地址
      defaultZone: http://127.0.0.1:6001/eureka
    # 拉去注册中心服务列表
    fetch-registry: true
    # 注册到微注册中心服务节点中
    register-with-eureka: true
  instance:
    # 使用ip地址注册服务
    prefer-ip-address: true

        在启动类EurekaClientConsumer1Application.java中

@SpringBootApplication
@EnableEurekaClient
@Slf4j
public class EurekaClientConsumer1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientConsumer1Application.class, args);
        log.info("Eureka服务端消费者启动成功...");
    }
}

启动两个服务后可以看到: 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值