Spring Cloud Eureka使用

相关版本介绍

  • JDK : 1.8
  • Spring Boot : 2.1.6.RELEASE
  • Spring Cloud : Greenwich.SR2

在微服务架构中,服务注册与发现是一个重要的工作,其实现的类型有很多种,例如:Eureka、Zookeeper、Consul、Nacos。

本文就简单介绍下Eureka作为注册中心的服务端和客户端的使用。

1、Eureka Server使用

1.1 添加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

1.2 application.yaml文件

spring:
  application:
    name: eureka-server

server:
  port: 10010

# Eureka配置
eureka:
  client:
  	# 关闭注册自身
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:${server.port}/eureka

1.3 启动类

在启动类中增加 @EnableEurekaServer注解

启动该服务,在浏览器输入 localhost:10010 可查看eureka面板

2 客户端

2.1 添加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.2 application.yaml文件

server:
  port: 8088

spring:
  application:
    name: question-service

eureka:
  server:
    host: 127.0.0.1
    port: 10010
  client:
    serviceUrl:
      defaultZone: http://${eureka.server.host}:${eureka.server.port}/eureka
  # 自定义instanceId格式
  instance:
    instanceId: ${spring.application.name}:${server.port}

2.3 启动类

在启动类中增加 @EnableDiscoveryClient 激活服务发现

3 DiscoveryClient使用

在Spring Cloud中我们可以通过DiscoveryClient来获取当前所有注册的服务信息,提供API有

  • list getServices:获取所有的服务,返回结果如下
    在这里插入图片描述
  • List getInstances(String serviceId):通过服务名称获取服务实例列表,返回结果如下
    在这里插入图片描述

在项目启动时会将这个对象的实例放入Spring容器中,我们使用的时候直接从容器中获取即可

@Autowired
private DiscoveryClient discoveryClient;

3 Eureka的不足

  • 不支持动态端口
  • 本地维护服务列表,使用Applications来存储服务列表,每个客户端均维护一份

在客户端启动时,会调用如下的com.netflix.discovery.DiscoveryClient中的initScheduledTasks方法启动定时更新服务列表的定时任务。执行周期,由registryFetchIntervalSeconds参数指定,默认为30s。感兴趣的可以自己去查看源码。

private void initScheduledTasks() {
    if (clientConfig.shouldFetchRegistry()) {
        // registry cache refresh timer
        int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
        int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
        scheduler.schedule(
                new TimedSupervisorTask(
                        "cacheRefresh",
                        scheduler,
                        cacheRefreshExecutor,
                        registryFetchIntervalSeconds,
                        TimeUnit.SECONDS,
                        expBackOffBound,
                        new CacheRefreshThread()
                ),
                registryFetchIntervalSeconds, TimeUnit.SECONDS);
    }
    ...
    ...
}

Eureka的一些相关配置可以查看org.springframework.cloud.netflix.eureka.EurekaClientConfigBean类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值